diff --git a/docs/docs.go b/docs/docs.go index 0500691..f620a1e 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -50,40 +50,16 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.DocumentBlock" - } - } - } - ] + "$ref": "#/definitions/entity.DocumentBlock" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -117,10 +93,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } + "description": "OK" }, "404": { "description": "文档块不存在", @@ -164,30 +137,15 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } + "description": "OK" }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -227,40 +185,16 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" - } - } - } - ] + "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -304,22 +238,10 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.DocumentDTO" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.DocumentDTO" + } } }, "403": { @@ -353,7 +275,7 @@ const docTemplate = `{ "parameters": [ { "type": "integer", - "description": "集��ID", + "description": "集合ID", "name": "id", "in": "path", "required": true @@ -363,19 +285,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" - } - } - } - ] + "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" } }, "404": { @@ -420,40 +330,16 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" - } - } - } - ] + "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -529,40 +415,16 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/dto.DocumentDTO" - } - } - } - ] + "$ref": "#/definitions/dto.DocumentDTO" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "403": { @@ -606,22 +468,10 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/entity.DocumentBlock" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/entity.DocumentBlock" + } } }, "404": { @@ -666,40 +516,16 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.DocumentBlock" - } - } - } - ] + "$ref": "#/definitions/entity.DocumentBlock" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -737,19 +563,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/dto.DocumentDTO" - } - } - } - ] + "$ref": "#/definitions/dto.DocumentDTO" } }, "403": { @@ -800,40 +614,16 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.Document" - } - } - } - ] + "$ref": "#/definitions/entity.Document" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -867,10 +657,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } + "description": "OK" }, "404": { "description": "文档不存在", @@ -898,22 +685,10 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/entity.Workspace" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/entity.Workspace" + } } } } @@ -945,40 +720,16 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.Workspace" - } - } - } - ] + "$ref": "#/definitions/entity.Workspace" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } } } @@ -1010,19 +761,7 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.Workspace" - } - } - } - ] + "$ref": "#/definitions/entity.Workspace" } }, "404": { @@ -1056,10 +795,7 @@ const docTemplate = `{ ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } + "description": "OK" }, "404": { "description": "工作空间不存在", @@ -1071,6 +807,42 @@ const docTemplate = `{ } }, "/workspaces/{id}/members": { + "get": { + "description": "根据工作空间ID获取工作空间的成员", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Workspace" + ], + "summary": "获取工作空间的成员", + "parameters": [ + { + "type": "integer", + "description": "工作空间ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/entity.Workspace" + } + }, + "404": { + "description": "工作空间不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + }, "post": { "description": "向工作空间添加新成员", "consumes": [ @@ -1105,40 +877,16 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.WorkspaceMember" - } - } - } - ] + "$ref": "#/definitions/entity.WorkspaceMember" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -1227,22 +975,10 @@ const docTemplate = `{ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" + } } }, "404": { @@ -1270,7 +1006,6 @@ const docTemplate = `{ "dto.CreateBlockRequest": { "type": "object", "required": [ - "content", "type" ], "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 69afac0..b90b785 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -41,40 +41,16 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.DocumentBlock" - } - } - } - ] + "$ref": "#/definitions/entity.DocumentBlock" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -108,10 +84,7 @@ ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } + "description": "OK" }, "404": { "description": "文档块不存在", @@ -155,30 +128,15 @@ ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } + "description": "OK" }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -218,40 +176,16 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" - } - } - } - ] + "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -295,22 +229,10 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.DocumentDTO" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.DocumentDTO" + } } }, "403": { @@ -344,7 +266,7 @@ "parameters": [ { "type": "integer", - "description": "集��ID", + "description": "集合ID", "name": "id", "in": "path", "required": true @@ -354,19 +276,7 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" - } - } - } - ] + "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" } }, "404": { @@ -411,40 +321,16 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" - } - } - } - ] + "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -520,40 +406,16 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/dto.DocumentDTO" - } - } - } - ] + "$ref": "#/definitions/dto.DocumentDTO" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "403": { @@ -597,22 +459,10 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/entity.DocumentBlock" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/entity.DocumentBlock" + } } }, "404": { @@ -657,40 +507,16 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.DocumentBlock" - } - } - } - ] + "$ref": "#/definitions/entity.DocumentBlock" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -728,19 +554,7 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/dto.DocumentDTO" - } - } - } - ] + "$ref": "#/definitions/dto.DocumentDTO" } }, "403": { @@ -791,40 +605,16 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.Document" - } - } - } - ] + "$ref": "#/definitions/entity.Document" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -858,10 +648,7 @@ ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } + "description": "OK" }, "404": { "description": "文档不存在", @@ -889,22 +676,10 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/entity.Workspace" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/entity.Workspace" + } } } } @@ -936,40 +711,16 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.Workspace" - } - } - } - ] + "$ref": "#/definitions/entity.Workspace" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } } } @@ -1001,19 +752,7 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.Workspace" - } - } - } - ] + "$ref": "#/definitions/entity.Workspace" } }, "404": { @@ -1047,10 +786,7 @@ ], "responses": { "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } + "description": "OK" }, "404": { "description": "工作空间不存在", @@ -1062,6 +798,42 @@ } }, "/workspaces/{id}/members": { + "get": { + "description": "根据工作空间ID获取工作空间的成员", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Workspace" + ], + "summary": "获取工作空间的成员", + "parameters": [ + { + "type": "integer", + "description": "工作空间ID", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/entity.Workspace" + } + }, + "404": { + "description": "工作空间不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + }, "post": { "description": "向工作空间添加新成员", "consumes": [ @@ -1096,40 +868,16 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.WorkspaceMember" - } - } - } - ] + "$ref": "#/definitions/entity.WorkspaceMember" } }, "400": { "description": "参数验证失败", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } } }, "404": { @@ -1218,22 +966,10 @@ "200": { "description": "OK", "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" - } - } - } - } - ] + "type": "array", + "items": { + "$ref": "#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection" + } } }, "404": { @@ -1261,7 +997,6 @@ "dto.CreateBlockRequest": { "type": "object", "required": [ - "content", "type" ], "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index c426061..43c7a76 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -15,7 +15,6 @@ definitions: type: type: string required: - - content - type type: object dto.CreateCollectionRequest: @@ -222,8 +221,6 @@ paths: responses: "200": description: OK - schema: - $ref: '#/definitions/dto.Response' "404": description: 文档块不存在 schema: @@ -253,23 +250,13 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/entity.DocumentBlock' - type: object + $ref: '#/definitions/entity.DocumentBlock' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array "404": description: 文档块不存在 schema: @@ -299,19 +286,12 @@ paths: responses: "200": description: OK - schema: - $ref: '#/definitions/dto.Response' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array "404": description: 文档块不存在 schema: @@ -337,23 +317,13 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' - type: object + $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array "404": description: 工作空间不存在 schema: @@ -382,14 +352,9 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.DocumentDTO' - type: array - type: object + items: + $ref: '#/definitions/dto.DocumentDTO' + type: array "403": description: 无权访问 schema: @@ -431,7 +396,7 @@ paths: - application/json description: 根据集合ID获取集合详情 parameters: - - description: 集��ID + - description: 集合ID in: path name: id required: true @@ -442,12 +407,7 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' - type: object + $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' "404": description: 集合不存在 schema: @@ -477,23 +437,13 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' - type: object + $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array "404": description: 集合不存在 schema: @@ -519,23 +469,13 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/dto.DocumentDTO' - type: object + $ref: '#/definitions/dto.DocumentDTO' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array "403": description: 无权访问 schema: @@ -564,14 +504,9 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/entity.DocumentBlock' - type: array - type: object + items: + $ref: '#/definitions/entity.DocumentBlock' + type: array "404": description: 文档不存在 schema: @@ -601,23 +536,13 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/entity.DocumentBlock' - type: object + $ref: '#/definitions/entity.DocumentBlock' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array "404": description: 文档不存在 schema: @@ -641,8 +566,6 @@ paths: responses: "200": description: OK - schema: - $ref: '#/definitions/dto.Response' "404": description: 文档不存在 schema: @@ -666,12 +589,7 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/dto.DocumentDTO' - type: object + $ref: '#/definitions/dto.DocumentDTO' "403": description: 无权访问 schema: @@ -705,23 +623,13 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/entity.Document' - type: object + $ref: '#/definitions/entity.Document' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array "404": description: 文档不存在 schema: @@ -740,14 +648,9 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/entity.Workspace' - type: array - type: object + items: + $ref: '#/definitions/entity.Workspace' + type: array summary: 列出工作空间列表 tags: - Workspace @@ -768,23 +671,13 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/entity.Workspace' - type: object + $ref: '#/definitions/entity.Workspace' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array summary: 创建工作空间 tags: - Workspace @@ -804,8 +697,6 @@ paths: responses: "200": description: OK - schema: - $ref: '#/definitions/dto.Response' "404": description: 工作空间不存在 schema: @@ -829,12 +720,7 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/entity.Workspace' - type: object + $ref: '#/definitions/entity.Workspace' "404": description: 工作空间不存在 schema: @@ -843,6 +729,30 @@ paths: tags: - Workspace /workspaces/{id}/members: + get: + consumes: + - application/json + description: 根据工作空间ID获取工作空间的成员 + parameters: + - description: 工作空间ID + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/entity.Workspace' + "404": + description: 工作空间不存在 + schema: + $ref: '#/definitions/dto.Response' + summary: 获取工作空间的成员 + tags: + - Workspace post: consumes: - application/json @@ -865,23 +775,13 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/entity.WorkspaceMember' - type: object + $ref: '#/definitions/entity.WorkspaceMember' "400": description: 参数验证失败 schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object + items: + $ref: '#/definitions/dto.ValidateError' + type: array "404": description: 工作空间不存在 schema: @@ -940,14 +840,9 @@ paths: "200": description: OK schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' - type: array - type: object + items: + $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' + type: array "404": description: 工作空间不存在 schema: diff --git a/internal/api/grpc/interceptor/auth.go b/internal/api/grpc/interceptor/auth.go index ced97a3..cd356a3 100644 --- a/internal/api/grpc/interceptor/auth.go +++ b/internal/api/grpc/interceptor/auth.go @@ -2,16 +2,15 @@ package interceptor import ( "context" - - "leafdev.top/Leaf/leaf-library-3/internal/base/conf" - "leafdev.top/Leaf/leaf-library-3/internal/base/logger" - authService "leafdev.top/Leaf/leaf-library-3/internal/services/auth" - "leafdev.top/Leaf/leaf-library-3/internal/types/constants" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" + "leafdev.top/Leaf/leaf-library-3/internal/constants" + "leafdev.top/Leaf/leaf-library-3/internal/errs" authInterceptor "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/auth" "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" "google.golang.org/grpc" + "leafdev.top/Leaf/leaf-library-3/internal/base/conf" + "leafdev.top/Leaf/leaf-library-3/internal/base/logger" + authService "leafdev.top/Leaf/leaf-library-3/internal/services/auth" ) var ignoreAuthApis = map[string]bool{ diff --git a/internal/api/http/controller/collection.go b/internal/api/http/controller/collection.go index 6121dcd..8b836a9 100644 --- a/internal/api/http/controller/collection.go +++ b/internal/api/http/controller/collection.go @@ -1,14 +1,17 @@ package controller import ( + "leafdev.top/Leaf/leaf-library-3/internal/pkg/response" + "net/http" + "github.com/gofiber/fiber/v2" + "leafdev.top/Leaf/leaf-library-3/internal/dto" _ "leafdev.top/Leaf/leaf-library-3/internal/entity" + "leafdev.top/Leaf/leaf-library-3/internal/errs" "leafdev.top/Leaf/leaf-library-3/internal/pkg/validator" "leafdev.top/Leaf/leaf-library-3/internal/services/auth" "leafdev.top/Leaf/leaf-library-3/internal/services/collection" "leafdev.top/Leaf/leaf-library-3/internal/services/workspace" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" ) type CollectionController struct { @@ -36,38 +39,38 @@ func NewCollectionController( // @Accept json // @Produce json // @Param request body dto.CreateCollectionRequest true "创建集合请求" -// @Success 200 {object} dto.Response{data=entity.Collection} -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 {object} entity.Collection +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Failure 404 {object} dto.Response "工作空间不存在" // @Router /collections [post] func (c *CollectionController) CreateCollection(ctx *fiber.Ctx) error { var req dto.CreateCollectionRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if validationErrors, ok, err := validator.Struct(req); !ok { if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Message((*validationErrors)[0].Message).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Message((*validationErrors)[0].Message).Status(fiber.StatusBadRequest).Send() } // 检查工作空间是否存在 exists, err := c.workspaceService.Exists(ctx.Context(), req.WorkspaceID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !exists { - return dto.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() } - collection, err := c.collectionService.Create(ctx.Context(), req.WorkspaceID, req.Name) + collectionEntity, err := c.collectionService.Create(ctx.Context(), req.WorkspaceID, req.Name) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(collection).Send() + return response.Ctx(ctx).Success(collectionEntity).Send() } // GetCollection 获取集合 @@ -76,30 +79,30 @@ func (c *CollectionController) CreateCollection(ctx *fiber.Ctx) error { // @Tags Collection // @Accept json // @Produce json -// @Param id path int true "集��ID" -// @Success 200 {object} dto.Response{data=entity.Collection} +// @Param id path int true "集合ID" +// @Success 200 {object} entity.Collection // @Failure 404 {object} dto.Response "集合不存在" // @Router /collections/{id} [get] func (c *CollectionController) GetCollection(ctx *fiber.Ctx) error { var req dto.GetCollectionRequest if err := ctx.ParamsParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } exists, err := c.collectionService.Exists(ctx.Context(), req.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !exists { - return dto.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() } - collection, err := c.collectionService.Get(ctx.Context(), req.ID) + collectionEntity, err := c.collectionService.Get(ctx.Context(), req.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(collection).Send() + return response.Ctx(ctx).Success(collectionEntity).Send() } // ListCollections 列出工作空间下的所有集合 @@ -109,30 +112,30 @@ func (c *CollectionController) GetCollection(ctx *fiber.Ctx) error { // @Accept json // @Produce json // @Param workspace_id path int true "工作空间ID" -// @Success 200 {object} dto.Response{data=[]entity.Collection} +// @Success 200 {object} []entity.Collection // @Failure 404 {object} dto.Response "工作空间不存在" // @Router /workspaces/{workspace_id}/collections [get] func (c *CollectionController) ListCollections(ctx *fiber.Ctx) error { var req dto.ListCollectionsRequest if err := ctx.ParamsParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - // 检���工作空间是否存在 + // 检查工作空间是否存在 exists, err := c.workspaceService.Exists(ctx.Context(), req.WorkspaceID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !exists { - return dto.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() } collections, err := c.collectionService.List(ctx.Context(), req.WorkspaceID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(collections).Send() + return response.Ctx(ctx).Success(collections).Send() } // UpdateCollection 更新集合 @@ -143,59 +146,59 @@ func (c *CollectionController) ListCollections(ctx *fiber.Ctx) error { // @Produce json // @Param id path int true "集合ID" // @Param request body dto.UpdateCollectionRequest true "更新集合请求" -// @Success 200 {object} dto.Response{data=entity.Collection} -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 {object} entity.Collection +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Failure 404 {object} dto.Response "集合不存在" // @Router /collections/{id} [put] func (c *CollectionController) UpdateCollection(ctx *fiber.Ctx) error { var params dto.CollectionIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } var req dto.UpdateCollectionRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if validationErrors, ok, err := validator.Struct(req); !ok { if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() } // 检查集合是否存在 - collection, err := c.collectionService.Get(ctx.Context(), params.ID) + collectionEntity, err := c.collectionService.Get(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - if collection == nil { - return dto.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() + if collectionEntity == nil { + return response.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该集合 user := c.authService.GetUser(ctx) - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } // 更新集合 - collection, err = c.collectionService.Update(ctx.Context(), params.ID, req.Name) + collectionEntity, err = c.collectionService.Update(ctx.Context(), params.ID, req.Name) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(collection).Send() + return response.Ctx(ctx).Success(collectionEntity).Send() } // DeleteCollection 删除集合 @@ -211,38 +214,38 @@ func (c *CollectionController) UpdateCollection(ctx *fiber.Ctx) error { func (c *CollectionController) DeleteCollection(ctx *fiber.Ctx) error { var params dto.CollectionIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } // 检查集合是否存在 - collection, err := c.collectionService.Get(ctx.Context(), params.ID) + collectionEntity, err := c.collectionService.Get(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - if collection == nil { - return dto.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() + if collectionEntity == nil { + return response.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该集合 user := c.authService.GetUser(ctx) - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } // 删除集合 err = c.collectionService.Delete(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(nil).Send() + return response.Ctx(ctx).Success(nil).Send() } diff --git a/internal/api/http/controller/document.go b/internal/api/http/controller/document.go index 6285e38..0ed8baa 100644 --- a/internal/api/http/controller/document.go +++ b/internal/api/http/controller/document.go @@ -2,13 +2,15 @@ package controller import ( "github.com/gofiber/fiber/v2" + "leafdev.top/Leaf/leaf-library-3/internal/dto" + "leafdev.top/Leaf/leaf-library-3/internal/errs" + "leafdev.top/Leaf/leaf-library-3/internal/pkg/response" "leafdev.top/Leaf/leaf-library-3/internal/pkg/validator" "leafdev.top/Leaf/leaf-library-3/internal/services/auth" "leafdev.top/Leaf/leaf-library-3/internal/services/collection" "leafdev.top/Leaf/leaf-library-3/internal/services/document" "leafdev.top/Leaf/leaf-library-3/internal/services/workspace" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" + "net/http" ) type DocumentController struct { @@ -39,76 +41,76 @@ func NewDocumentController( // @Accept json // @Produce json // @Param request body dto.CreateDocumentRequest true "创建文档请求" -// @Success 200 {object} dto.Response{data=dto.DocumentDTO} -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 {object} dto.DocumentDTO +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Failure 403 {object} dto.Response "无权访问" // @Failure 404 {object} dto.Response "集合不存在或父文档不存在" // @Router /documents [post] func (c *DocumentController) CreateDocument(ctx *fiber.Ctx) error { var req dto.CreateDocumentRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if validationErrors, ok, err := validator.Struct(req); !ok { if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() } // 检查集合是否存在 - collection, err := c.collectionService.Get(ctx.Context(), req.CollectionID) + collectionEntity, err := c.collectionService.Get(ctx.Context(), req.CollectionID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - if collection == nil { - return dto.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() + if collectionEntity == nil { + return response.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该集合 user := c.authService.GetUser(ctx) - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - if workspace == nil { - return dto.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() + if workspaceEntity == nil { + return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } // 如果是子文档,检查父文档是否存在且属于同一个集合 if req.ParentID != nil { parentDoc, err := c.documentService.Get(ctx.Context(), *req.ParentID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if parentDoc == nil { - return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() } if parentDoc.CollectionId != req.CollectionID { - return dto.Ctx(ctx).Error(errs.ErrInvalidParentDocument).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Error(errs.ErrInvalidParentDocument).Status(fiber.StatusBadRequest).Send() } } doc, err := c.documentService.Create(ctx.Context(), req.Name, req.WorkspaceID, req.CollectionID, req.ParentID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } docDTO, err := c.documentService.ToDTO(ctx.Context(), doc) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(docDTO).Send() + return response.Ctx(ctx).Success(docDTO).Send() } // GetDocument 获取文档 @@ -118,49 +120,49 @@ func (c *DocumentController) CreateDocument(ctx *fiber.Ctx) error { // @Accept json // @Produce json // @Param id path int true "文档ID" -// @Success 200 {object} dto.Response{data=dto.DocumentDTO} +// @Success 200 {object} dto.DocumentDTO // @Failure 403 {object} dto.Response "无权访问" // @Failure 404 {object} dto.Response "文档不存在" // @Router /documents/{id} [get] func (c *DocumentController) GetDocument(ctx *fiber.Ctx) error { var params dto.DocumentIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } doc, err := c.documentService.Get(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if doc == nil { - return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该文档 user := c.authService.GetUser(ctx) - collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + collectionEntity, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } docDTO, err := c.documentService.ToDTO(ctx.Context(), doc) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(docDTO).Send() + return response.Ctx(ctx).Success(docDTO).Send() } // ListDocuments 列出集合或父文档下的所有文档 @@ -171,63 +173,63 @@ func (c *DocumentController) GetDocument(ctx *fiber.Ctx) error { // @Produce json // @Param collection_id path int true "集合ID" // @Param parent_id query int false "父文档ID,不传则获取根文档" -// @Success 200 {object} dto.Response{data=[]dto.DocumentDTO} +// @Success 200 {object} []dto.DocumentDTO // @Failure 403 {object} dto.Response "无权访问" // @Failure 404 {object} dto.Response "集合不存在或父文档不存在" // @Router /collections/{collection_id}/documents [get] func (c *DocumentController) ListDocuments(ctx *fiber.Ctx) error { var pathParams dto.ListDocumentsPathParam if err := ctx.ParamsParser(&pathParams); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } var queryParams dto.ListDocumentsQueryParam if err := ctx.QueryParser(&queryParams); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } // 检查集合是否存在 - collection, err := c.collectionService.Get(ctx.Context(), pathParams.CollectionID) + collectionEntity, err := c.collectionService.Get(ctx.Context(), pathParams.CollectionID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - if collection == nil { - return dto.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() + if collectionEntity == nil { + return response.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该集合 user := c.authService.GetUser(ctx) - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } // 如果指定了父文档,检查父文档是否存在且属于同一个集合 if queryParams.ParentID != nil { parentDoc, err := c.documentService.Get(ctx.Context(), *queryParams.ParentID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if parentDoc == nil { - return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() } if parentDoc.CollectionId != pathParams.CollectionID { - return dto.Ctx(ctx).Error(errs.ErrInvalidParentDocument).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Error(errs.ErrInvalidParentDocument).Status(fiber.StatusBadRequest).Send() } } // 获取文档列表 docs, err := c.documentService.List(ctx.Context(), pathParams.CollectionID, queryParams.ParentID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } // 转换为 DTO @@ -235,12 +237,12 @@ func (c *DocumentController) ListDocuments(ctx *fiber.Ctx) error { for i, doc := range docs { docDTO, err := c.documentService.ToDTO(ctx.Context(), doc) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } dtos[i] = docDTO } - return dto.Ctx(ctx).Success(dtos).Send() + return response.Ctx(ctx).Success(dtos).Send() } // UpdateDocument 更新文档 @@ -251,39 +253,39 @@ func (c *DocumentController) ListDocuments(ctx *fiber.Ctx) error { // @Produce json // @Param id path int true "文档ID" // @Param request body dto.UpdateDocumentRequest true "更新文档请求" -// @Success 200 {object} dto.Response{data=entity.Document} -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 {object} entity.Document +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Failure 404 {object} dto.Response "文档不存在" // @Router /documents/{id} [put] func (c *DocumentController) UpdateDocument(ctx *fiber.Ctx) error { var params dto.DocumentIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } var req dto.UpdateDocumentRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if validationErrors, ok, err := validator.Struct(req); !ok { if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() } doc, err := c.documentService.Update(ctx.Context(), params.ID, req.Name) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } docDTO, err := c.documentService.ToDTO(ctx.Context(), doc) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(docDTO).Send() + return response.Ctx(ctx).Success(docDTO).Send() } // DeleteDocument 删除文档 @@ -293,21 +295,21 @@ func (c *DocumentController) UpdateDocument(ctx *fiber.Ctx) error { // @Accept json // @Produce json // @Param id path int true "文档ID" -// @Success 200 {object} dto.Response +// @Success 200 // @Failure 404 {object} dto.Response "文档不存在" // @Router /documents/{id} [delete] func (c *DocumentController) DeleteDocument(ctx *fiber.Ctx) error { var params dto.DocumentIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } err := c.documentService.Delete(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(nil).Send() + return response.Ctx(ctx).Success(nil).Send() } // CreateBlock 创建文档块 @@ -318,59 +320,59 @@ func (c *DocumentController) DeleteDocument(ctx *fiber.Ctx) error { // @Produce json // @Param document_id path int true "文档ID" // @Param request body dto.CreateBlockRequest true "创建文档块请求" -// @Success 200 {object} dto.Response{data=entity.DocumentBlock} -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 {object} entity.DocumentBlock +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Failure 404 {object} dto.Response "文档不存在" // @Router /documents/{document_id}/blocks [post] func (c *DocumentController) CreateBlock(ctx *fiber.Ctx) error { var params dto.ListBlocksRequest if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } var req dto.CreateBlockRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if validationErrors, ok, err := validator.Struct(req); !ok { - return dto.Ctx(ctx).Data(validationErrors).Error(err).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Data(validationErrors).Error(err).Status(fiber.StatusBadRequest).Send() } // 检查文档是否存在 doc, err := c.documentService.Get(ctx.Context(), params.DocumentID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if doc == nil { - return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该文档 user := c.authService.GetUser(ctx) - collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + collectionEntity, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } block, err := c.documentService.CreateBlock(ctx.Context(), params.DocumentID, req.Type, req.Content, req.AfterBlockID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(block).Send() + return response.Ctx(ctx).Success(block).Send() } // UpdateBlock 更新文档块 @@ -381,71 +383,71 @@ func (c *DocumentController) CreateBlock(ctx *fiber.Ctx) error { // @Produce json // @Param block_id path int true "文档块ID" // @Param request body dto.UpdateBlockRequest true "更新文档块请求" -// @Success 200 {object} dto.Response{data=entity.DocumentBlock} -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 {object} entity.DocumentBlock +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Failure 404 {object} dto.Response "文档块不存在" // @Router /blocks/{block_id} [put] func (c *DocumentController) UpdateBlock(ctx *fiber.Ctx) error { var params dto.BlockIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } var req dto.UpdateBlockRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if validationErrors, ok, err := validator.Struct(req); !ok { if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() } // 获取块信息 block, err := c.documentService.GetBlock(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if block == nil { - return dto.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() } // 检查文档是否存在 doc, err := c.documentService.Get(ctx.Context(), block.DocumentId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if doc == nil { - return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该文档 user := c.authService.GetUser(ctx) - collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + collectionEntity, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } block, err = c.documentService.UpdateBlock(ctx.Context(), params.ID, req.Content) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(block).Send() + return response.Ctx(ctx).Success(block).Send() } // DeleteBlock ���除文档块 @@ -455,58 +457,58 @@ func (c *DocumentController) UpdateBlock(ctx *fiber.Ctx) error { // @Accept json // @Produce json // @Param block_id path int true "文档块ID" -// @Success 200 {object} dto.Response +// @Success 200 // @Failure 404 {object} dto.Response "文档块不存在" // @Router /blocks/{block_id} [delete] func (c *DocumentController) DeleteBlock(ctx *fiber.Ctx) error { var params dto.BlockIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } // 获取块信息 block, err := c.documentService.GetBlock(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if block == nil { - return dto.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() } // 检查文档是否存在 doc, err := c.documentService.Get(ctx.Context(), block.DocumentId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if doc == nil { - return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该文档 user := c.authService.GetUser(ctx) - collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + collectionEntity, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } err = c.documentService.DeleteBlock(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(nil).Send() + return response.Ctx(ctx).Success(nil).Send() } // ListBlocks 列出文档下的所有块 @@ -516,49 +518,49 @@ func (c *DocumentController) DeleteBlock(ctx *fiber.Ctx) error { // @Accept json // @Produce json // @Param document_id path int true "文档ID" -// @Success 200 {object} dto.Response{data=[]entity.DocumentBlock} +// @Success 200 {object} []entity.DocumentBlock // @Failure 404 {object} dto.Response "文档不存在" // @Router /documents/{document_id}/blocks [get] func (c *DocumentController) ListBlocks(ctx *fiber.Ctx) error { var params dto.ListBlocksRequest if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } // 检查文档是否存在 doc, err := c.documentService.Get(ctx.Context(), params.DocumentID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if doc == nil { - return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该文档 user := c.authService.GetUser(ctx) - collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + collectionEntity, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } blocks, err := c.documentService.ListBlocks(ctx.Context(), params.DocumentID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(blocks).Send() + return response.Ctx(ctx).Success(blocks).Send() } // MoveBlock 移动文档块 @@ -569,62 +571,62 @@ func (c *DocumentController) ListBlocks(ctx *fiber.Ctx) error { // @Produce json // @Param block_id path int true "文档块ID" // @Param request body dto.MoveBlockRequest true "移动文档块请求" -// @Success 200 {object} dto.Response -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Failure 404 {object} dto.Response "文档块不存在" // @Router /blocks/{block_id}/move [post] func (c *DocumentController) MoveBlock(ctx *fiber.Ctx) error { var params dto.BlockIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } var req dto.MoveBlockRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } // 获取块信息 block, err := c.documentService.GetBlock(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if block == nil { - return dto.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() } // 检查文档是否存在 doc, err := c.documentService.Get(ctx.Context(), block.DocumentId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if doc == nil { - return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() } // 检查用户是否有权限访问该文档 user := c.authService.GetUser(ctx) - collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + collectionEntity, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !isMember { - return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() } err = c.documentService.MoveBlock(ctx.Context(), params.ID, req.AfterBlockID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(nil).Send() + return response.Ctx(ctx).Success(nil).Send() } diff --git a/internal/api/http/controller/workspace.go b/internal/api/http/controller/workspace.go index ef0357b..b3c5bad 100644 --- a/internal/api/http/controller/workspace.go +++ b/internal/api/http/controller/workspace.go @@ -1,14 +1,18 @@ package controller import ( + "errors" + "leafdev.top/Leaf/leaf-library-3/internal/pkg/response" + "net/http" + "github.com/gofiber/fiber/v2" + "leafdev.top/Leaf/leaf-library-3/internal/dto" + "leafdev.top/Leaf/leaf-library-3/internal/dto/user" _ "leafdev.top/Leaf/leaf-library-3/internal/entity" + "leafdev.top/Leaf/leaf-library-3/internal/errs" "leafdev.top/Leaf/leaf-library-3/internal/pkg/validator" "leafdev.top/Leaf/leaf-library-3/internal/services/auth" "leafdev.top/Leaf/leaf-library-3/internal/services/workspace" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" - "leafdev.top/Leaf/leaf-library-3/internal/types/user" ) type WorkspaceController struct { @@ -30,32 +34,32 @@ func NewWorkspaceController(workspaceService *workspace.Service, authService *au // @Accept json // @Produce json // @Param request body dto.CreateWorkspaceRequest true "创建工作空间请求" -// @Success 200 {object} dto.Response{data=entity.Workspace} -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 {object} entity.Workspace +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Router /workspaces [post] func (c *WorkspaceController) CreateWorkspace(ctx *fiber.Ctx) error { var req dto.CreateWorkspaceRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if validationErrors, ok, err := validator.Struct(req); !ok { if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() } // 从上下文获取用户ID - user := c.authService.GetUser(ctx) - userId := user.ID + authUser := c.authService.GetUser(ctx) + userId := authUser.ID - workspace, err := c.workspaceService.Create(ctx.Context(), userId, req.Name) + workspaceEntity, err := c.workspaceService.Create(ctx.Context(), userId, req.Name) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(workspace).Send() + return response.Ctx(ctx).Success(workspaceEntity).Send() } // GetWorkspace 获取工作空间 @@ -65,29 +69,29 @@ func (c *WorkspaceController) CreateWorkspace(ctx *fiber.Ctx) error { // @Accept json // @Produce json // @Param id path int true "工作空间ID" -// @Success 200 {object} dto.Response{data=entity.Workspace} +// @Success 200 {object} entity.Workspace // @Failure 404 {object} dto.Response "工作空间不存在" // @Router /workspaces/{id} [get] func (c *WorkspaceController) GetWorkspace(ctx *fiber.Ctx) error { var params dto.WorkspaceIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } exists, err := c.workspaceService.Exists(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !exists { - return dto.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), params.ID) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(workspace).Send() + return response.Ctx(ctx).Success(workspaceEntity).Send() } // ListWorkspaces 列出用户的所有工作空间 @@ -96,19 +100,18 @@ func (c *WorkspaceController) GetWorkspace(ctx *fiber.Ctx) error { // @Tags Workspace // @Accept json // @Produce json -// @Success 200 {object} dto.Response{data=[]entity.Workspace} +// @Success 200 {object} []entity.Workspace // @Router /workspaces [get] func (c *WorkspaceController) ListWorkspaces(ctx *fiber.Ctx) error { // 从上下文获取用户ID - user := c.authService.GetUser(ctx) - userId := user.ID + authUser := c.authService.GetUser(ctx) - workspaces, err := c.workspaceService.List(ctx.Context(), userId) + workspaces, err := c.workspaceService.List(ctx.Context(), authUser.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(workspaces).Send() + return response.Ctx(ctx).Success(workspaces).Send() } // DeleteWorkspace 删除工作空间 @@ -118,29 +121,66 @@ func (c *WorkspaceController) ListWorkspaces(ctx *fiber.Ctx) error { // @Accept json // @Produce json // @Param id path int true "工作空间ID" -// @Success 200 {object} dto.Response +// @Success 200 // @Failure 404 {object} dto.Response "工作空间不存在" // @Router /workspaces/{id} [delete] func (c *WorkspaceController) DeleteWorkspace(ctx *fiber.Ctx) error { var params dto.WorkspaceIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } exists, err := c.workspaceService.Exists(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !exists { - return dto.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() } err = c.workspaceService.DeleteWorkspace(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(nil).Send() + return response.Ctx(ctx).Success(nil).Send() +} + +// GetWorkspaceMembers 获取工作空间的成员 +// @Summary 获取工作空间的成员 +// @Description 根据工作空间ID获取工作空间的成员 +// @Tags Workspace +// @Accept json +// @Produce json +// @Param id path int true "工作空间ID" +// @Success 200 {object} entity.Workspace +// @Failure 404 {object} dto.Response "工作空间不存在" +// @Router /workspaces/{id}/members [get] +func (c *WorkspaceController) GetWorkspaceMembers(ctx *fiber.Ctx) error { + var params dto.WorkspaceIDParam + if err := ctx.ParamsParser(¶ms); err != nil { + return response.Ctx(ctx).Status(http.StatusBadRequest).Send() + } + + exists, err := c.workspaceService.Exists(ctx.Context(), params.ID) + if err != nil { + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() + } + if !exists { + return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() + } + + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), params.ID) + if err != nil { + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() + } + + workspaceMembers, err := c.workspaceService.ListMembers(ctx.Context(), workspaceEntity) + if err != nil { + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() + } + + return response.Ctx(ctx).Success(workspaceMembers).Send() } // AddMember 添加工作空间成员 @@ -151,51 +191,51 @@ func (c *WorkspaceController) DeleteWorkspace(ctx *fiber.Ctx) error { // @Produce json // @Param id path int true "工作空间ID" // @Param request body dto.AddWorkspaceMemberRequest true "添加成员请求" -// @Success 200 {object} dto.Response{data=entity.WorkspaceMember} -// @Failure 400 {object} dto.Response{data=[]dto.ValidateError} "参数验证失败" +// @Success 200 {object} entity.WorkspaceMember +// @Failure 400 {object} []dto.ValidateError "参数验证失败" // @Failure 404 {object} dto.Response "工作空间不存在" // @Failure 409 {object} dto.Response "成员已存在" // @Router /workspaces/{id}/members [post] func (c *WorkspaceController) AddMember(ctx *fiber.Ctx) error { var params dto.WorkspaceIDParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } exists, err := c.workspaceService.Exists(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !exists { - return dto.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() } var req dto.AddWorkspaceMemberRequest if err := ctx.BodyParser(&req); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if validationErrors, ok, err := validator.Struct(req); !ok { if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() + return response.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), params.ID) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), params.ID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - member, err := c.workspaceService.AddMember(ctx.Context(), req.UserID, workspace) + member, err := c.workspaceService.AddMember(ctx.Context(), req.UserID, workspaceEntity) if err != nil { - if err == errs.ErrMemberAlreadyExists { - return dto.Ctx(ctx).Error(err).Status(fiber.StatusConflict).Send() + if errors.Is(err, errs.ErrMemberAlreadyExists) { + return response.Ctx(ctx).Error(err).Status(fiber.StatusConflict).Send() } - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(member).Send() + return response.Ctx(ctx).Success(member).Send() } // RemoveMember 移除工作空间成员 @@ -212,29 +252,29 @@ func (c *WorkspaceController) AddMember(ctx *fiber.Ctx) error { func (c *WorkspaceController) RemoveMember(ctx *fiber.Ctx) error { var params dto.WorkspaceMemberParam if err := ctx.ParamsParser(¶ms); err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } exists, err := c.workspaceService.Exists(ctx.Context(), params.WorkspaceID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } if !exists { - return dto.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() + return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send() } - workspace, err := c.workspaceService.Get(ctx.Context(), params.WorkspaceID) + workspaceEntity, err := c.workspaceService.Get(ctx.Context(), params.WorkspaceID) if err != nil { - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - err = c.workspaceService.RemoveMember(ctx.Context(), user.ID(params.UserID), workspace) + err = c.workspaceService.RemoveMember(ctx.Context(), user.ID(params.UserID), workspaceEntity) if err != nil { - if err == errs.ErrMemberNotExists { - return dto.Ctx(ctx).Error(err).Status(fiber.StatusNotFound).Send() + if errors.Is(err, errs.ErrMemberNotExists) { + return response.Ctx(ctx).Error(err).Status(fiber.StatusNotFound).Send() } - return err + return response.Ctx(ctx).Status(http.StatusInternalServerError).Send() } - return dto.Ctx(ctx).Success(nil).Send() + return response.Ctx(ctx).Success(nil).Send() } diff --git a/internal/api/http/middleware/auth.go b/internal/api/http/middleware/auth.go index 04693cc..48ffe3f 100644 --- a/internal/api/http/middleware/auth.go +++ b/internal/api/http/middleware/auth.go @@ -1,18 +1,18 @@ package middleware import ( + "leafdev.top/Leaf/leaf-library-3/internal/pkg/response" "net/http" "slices" "strings" - "leafdev.top/Leaf/leaf-library-3/internal/base/conf" - authService "leafdev.top/Leaf/leaf-library-3/internal/services/auth" - "leafdev.top/Leaf/leaf-library-3/internal/types/constants" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" - authType "leafdev.top/Leaf/leaf-library-3/internal/types/user" + "leafdev.top/Leaf/leaf-library-3/internal/constants" + authType "leafdev.top/Leaf/leaf-library-3/internal/dto/user" + "leafdev.top/Leaf/leaf-library-3/internal/errs" "github.com/gofiber/fiber/v2" + "leafdev.top/Leaf/leaf-library-3/internal/base/conf" + authService "leafdev.top/Leaf/leaf-library-3/internal/services/auth" ) type Auth struct { @@ -33,7 +33,7 @@ func NewAuth(config *conf.Config, authService *authService.Service) *Auth { func (a *Auth) Handler() fiber.Handler { return func(c *fiber.Ctx) error { - var r = dto.Ctx(c) + var r = response.Ctx(c) var err error var token = new(authType.User) diff --git a/internal/api/http/middleware/rbac.go b/internal/api/http/middleware/rbac.go index ae9b768..da68931 100644 --- a/internal/api/http/middleware/rbac.go +++ b/internal/api/http/middleware/rbac.go @@ -2,14 +2,15 @@ package middleware import ( "fmt" + "leafdev.top/Leaf/leaf-library-3/internal/pkg/response" "net/http" "strings" + userPkg "leafdev.top/Leaf/leaf-library-3/internal/dto/user" + "github.com/gofiber/fiber/v2" "leafdev.top/Leaf/leaf-library-3/internal/base/conf" "leafdev.top/Leaf/leaf-library-3/internal/services/auth" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - userPkg "leafdev.top/Leaf/leaf-library-3/internal/types/user" ) type RBAC struct { @@ -21,11 +22,11 @@ func (m *RBAC) RoutePermission() fiber.Handler { return func(c *fiber.Ctx) error { user, ok := m.authService.GetUserSafe(c) if !ok { - return dto.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() + return response.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() } if !user.Valid { - return dto.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() + return response.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() } var path = cleanPath(c.Path()) @@ -35,7 +36,7 @@ func (m *RBAC) RoutePermission() fiber.Handler { pass := user.HasPermissions(permissionName) if !pass { - return dto.Ctx(c). + return response.Ctx(c). Message(fmt.Sprintf("permission denied, permission name: %s", permissionName)). Error(nil). Status(http.StatusForbidden). @@ -50,11 +51,11 @@ func (m *RBAC) RequirePermissions(permissions ...string) fiber.Handler { return func(c *fiber.Ctx) error { user, ok := m.authService.GetUserSafe(c) if !ok { - return dto.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() + return response.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() } if !user.Valid { - return dto.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() + return response.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() } var pass = true @@ -72,7 +73,7 @@ func (m *RBAC) RequirePermissions(permissions ...string) fiber.Handler { } if !pass { - return dto.Ctx(c). + return response.Ctx(c). Message(fmt.Sprintf("permission denied, required permissions: %s, failed permission: %s", permissions, failedPermissionName)). Error(nil). @@ -88,11 +89,11 @@ func (m *RBAC) RequireRoles(roles ...string) fiber.Handler { return func(c *fiber.Ctx) error { user, ok := m.authService.GetUserSafe(c) if !ok { - return dto.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() + return response.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() } if !user.Valid { - return dto.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() + return response.Ctx(c).Error(nil).Status(http.StatusUnauthorized).Send() } var pass = true @@ -110,7 +111,7 @@ func (m *RBAC) RequireRoles(roles ...string) fiber.Handler { } if !pass { - return dto.Ctx(c). + return response.Ctx(c). Message(fmt.Sprintf("permission denied, required roles: %s, failed role %s", roles, failedRoleName)). Error(nil). Status(http.StatusForbidden). diff --git a/internal/base/server/error.go b/internal/base/server/error.go index b899059..30dc81b 100644 --- a/internal/base/server/error.go +++ b/internal/base/server/error.go @@ -2,24 +2,25 @@ package server import ( "errors" + "leafdev.top/Leaf/leaf-library-3/internal/pkg/response" "net/http" + errs2 "leafdev.top/Leaf/leaf-library-3/internal/errs" + "github.com/gofiber/fiber/v2" "gorm.io/gorm" "leafdev.top/Leaf/leaf-library-3/internal/base/logger" "leafdev.top/Leaf/leaf-library-3/internal/pkg/validator" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" ) func errorConverter(logger *logger.Logger, ctx *fiber.Ctx, err error) error { status := http.StatusInternalServerError if err == nil { - return dto.Ctx(ctx).Error(errs.ErrInternalServerError).Status(status).Send() + return response.Ctx(ctx).Error(errs2.ErrInternalServerError).Status(status).Send() } - var errorMsg dto.IError + var errorMsg response.IError switch { // 404 @@ -31,20 +32,20 @@ func errorConverter(logger *logger.Logger, ctx *fiber.Ctx, err error) error { status = http.StatusUnprocessableEntity errorMsg = err - case errors.Is(err, errs.ErrPermissionDenied): + case errors.Is(err, errs2.ErrPermissionDenied): status = http.StatusForbidden case errors.Is(err, validator.ErrValidationFailed): status = http.StatusBadRequest case errors.Is(err, gorm.ErrRecordNotFound): - errorMsg = errs.ErrNotFound + errorMsg = errs2.ErrNotFound status = http.StatusNotFound default: logger.Sugar.Errorf("fiber error: %s", err) - errorMsg = errs.UnknownError + errorMsg = errs2.UnknownError } - return dto.Ctx(ctx).Status(status).Error(errorMsg).Send() + return response.Ctx(ctx).Status(status).Error(errorMsg).Send() } diff --git a/internal/base/server/http.go b/internal/base/server/http.go index 28d8d89..e993e0c 100644 --- a/internal/base/server/http.go +++ b/internal/base/server/http.go @@ -1,9 +1,12 @@ package server import ( + "leafdev.top/Leaf/leaf-library-3/internal/pkg/response" "net/http" "strings" + "leafdev.top/Leaf/leaf-library-3/internal/errs" + "github.com/ansrivas/fiberprometheus/v2" "github.com/bytedance/sonic" "github.com/gofiber/fiber/v2" @@ -14,8 +17,6 @@ import ( "leafdev.top/Leaf/leaf-library-3/internal/base/logger" "leafdev.top/Leaf/leaf-library-3/internal/router" "leafdev.top/Leaf/leaf-library-3/internal/services/auth" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" ) type HttpServer struct { @@ -83,7 +84,7 @@ func (hs *HttpServer) BizRouter() *fiber.App { // 404 Route hs.Fiber.Use(func(ctx *fiber.Ctx) error { - return dto.Ctx(ctx).Status(fiber.StatusNotFound).Error(errs.RouteNotFound).Send() + return response.Ctx(ctx).Status(fiber.StatusNotFound).Error(errs.RouteNotFound).Send() }) return hs.Fiber diff --git a/internal/types/constants/auth.go b/internal/constants/auth.go similarity index 100% rename from internal/types/constants/auth.go rename to internal/constants/auth.go diff --git a/internal/types/constants/rbac.go b/internal/constants/rbac.go similarity index 60% rename from internal/types/constants/rbac.go rename to internal/constants/rbac.go index ea3d0b1..93889c4 100644 --- a/internal/types/constants/rbac.go +++ b/internal/constants/rbac.go @@ -1,7 +1,7 @@ package constants import ( - "leafdev.top/Leaf/leaf-library-3/internal/types/user" + "leafdev.top/Leaf/leaf-library-3/internal/dto/user" ) const ( diff --git a/internal/types/dto/base.go b/internal/dto/base.go similarity index 100% rename from internal/types/dto/base.go rename to internal/dto/base.go diff --git a/internal/types/dto/collection.go b/internal/dto/collection.go similarity index 100% rename from internal/types/dto/collection.go rename to internal/dto/collection.go diff --git a/internal/types/dto/document.go b/internal/dto/document.go similarity index 100% rename from internal/types/dto/document.go rename to internal/dto/document.go diff --git a/internal/types/dto/entity.go b/internal/dto/entity.go similarity index 100% rename from internal/types/dto/entity.go rename to internal/dto/entity.go diff --git a/internal/types/dto/params.go b/internal/dto/params.go similarity index 100% rename from internal/types/dto/params.go rename to internal/dto/params.go diff --git a/internal/types/dto/test_request.go b/internal/dto/test_request.go similarity index 100% rename from internal/types/dto/test_request.go rename to internal/dto/test_request.go diff --git a/internal/types/user/user.go b/internal/dto/user/user.go similarity index 100% rename from internal/types/user/user.go rename to internal/dto/user/user.go diff --git a/internal/types/dto/user_response.go b/internal/dto/user_response.go similarity index 81% rename from internal/types/dto/user_response.go rename to internal/dto/user_response.go index 1ef510e..5544b82 100644 --- a/internal/types/dto/user_response.go +++ b/internal/dto/user_response.go @@ -1,7 +1,7 @@ package dto import ( - "leafdev.top/Leaf/leaf-library-3/internal/types/user" + "leafdev.top/Leaf/leaf-library-3/internal/dto/user" ) type CurrentUserResponse struct { diff --git a/internal/types/dto/workspace.go b/internal/dto/workspace.go similarity index 91% rename from internal/types/dto/workspace.go rename to internal/dto/workspace.go index 4835bdc..9b198cf 100644 --- a/internal/types/dto/workspace.go +++ b/internal/dto/workspace.go @@ -1,6 +1,8 @@ package dto -import "leafdev.top/Leaf/leaf-library-3/internal/types/user" +import ( + "leafdev.top/Leaf/leaf-library-3/internal/dto/user" +) // CreateWorkspaceRequest 创建工作空间请求 type CreateWorkspaceRequest struct { diff --git a/internal/entity/Collection.go b/internal/entity/Collection.go index e8e48c1..1120d9a 100644 --- a/internal/entity/Collection.go +++ b/internal/entity/Collection.go @@ -2,7 +2,7 @@ package entity import ( "gorm.io/gorm" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" + "leafdev.top/Leaf/leaf-library-3/internal/dto" ) type Collection struct { diff --git a/internal/entity/Document.go b/internal/entity/Document.go index 2919760..c38e077 100644 --- a/internal/entity/Document.go +++ b/internal/entity/Document.go @@ -2,7 +2,7 @@ package entity import ( "gorm.io/gorm" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" + "leafdev.top/Leaf/leaf-library-3/internal/dto" ) type Document struct { diff --git a/internal/entity/Model.go b/internal/entity/Model.go index 4532c5d..0066a0c 100644 --- a/internal/entity/Model.go +++ b/internal/entity/Model.go @@ -3,7 +3,7 @@ package entity import ( "time" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" + "leafdev.top/Leaf/leaf-library-3/internal/dto" ) // Model 是所有 entity 的基类,后期要将所有的 Base 改成这种形式 diff --git a/internal/entity/Workspace.go b/internal/entity/Workspace.go index 712cb7c..2f4382b 100644 --- a/internal/entity/Workspace.go +++ b/internal/entity/Workspace.go @@ -2,8 +2,8 @@ package entity import ( "gorm.io/gorm" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/user" + "leafdev.top/Leaf/leaf-library-3/internal/dto" + "leafdev.top/Leaf/leaf-library-3/internal/dto/user" ) type Workspace struct { diff --git a/internal/types/errs/auth.go b/internal/errs/auth.go similarity index 100% rename from internal/types/errs/auth.go rename to internal/errs/auth.go diff --git a/internal/types/errs/collection.go b/internal/errs/collection.go similarity index 100% rename from internal/types/errs/collection.go rename to internal/errs/collection.go diff --git a/internal/types/errs/error.go b/internal/errs/error.go similarity index 100% rename from internal/types/errs/error.go rename to internal/errs/error.go diff --git a/internal/types/errs/model.go b/internal/errs/model.go similarity index 100% rename from internal/types/errs/model.go rename to internal/errs/model.go diff --git a/internal/types/errs/router.go b/internal/errs/router.go similarity index 100% rename from internal/types/errs/router.go rename to internal/errs/router.go diff --git a/internal/types/errs/validation.go b/internal/errs/validation.go similarity index 100% rename from internal/types/errs/validation.go rename to internal/errs/validation.go diff --git a/internal/types/errs/workspace.go b/internal/errs/workspace.go similarity index 100% rename from internal/types/errs/workspace.go rename to internal/errs/workspace.go diff --git a/internal/types/events/stream.go b/internal/events/stream.go similarity index 100% rename from internal/types/events/stream.go rename to internal/events/stream.go diff --git a/internal/types/dto/dto.go b/internal/pkg/response/response.go similarity index 82% rename from internal/types/dto/dto.go rename to internal/pkg/response/response.go index 513a12f..bd43802 100644 --- a/internal/types/dto/dto.go +++ b/internal/pkg/response/response.go @@ -1,4 +1,4 @@ -package dto +package response import ( "net/http" @@ -18,10 +18,9 @@ type ValidateError struct { type Body struct { Message string `json:"message"` Error string `json:"error"` - //ValidationErrors *[]ValidateError `json:"validation_error,omitempty"` - Success bool `json:"success"` - Data any `json:"data,omitempty"` - Wrap bool `json:"-"` + Success bool `json:"success"` + Data any `json:"data,omitempty"` + Wrap bool `json:"-"` } type Response struct { @@ -33,7 +32,7 @@ type Response struct { func Ctx(c *fiber.Ctx) *Response { return &Response{ body: &Body{ - Wrap: true, + Wrap: false, }, ctx: c, httpStatus: 0, @@ -50,13 +49,7 @@ func (r *Response) Message(message string) *Response { return r } -// WithoutWrap 将不在 body 中包裹 data -func (r *Response) WithoutWrap() *Response { - r.body.Wrap = false - - return r -} - +// Wrap 将在响应中包裹 data func (r *Response) Wrap() *Response { r.body.Wrap = true return r @@ -108,11 +101,27 @@ func (r *Response) Send() error { } } - if r.body.Wrap { - return r.ctx.Status(r.httpStatus).JSON(r.body) + // if 400 bad request but not have message + if r.httpStatus == http.StatusBadRequest { + if r.body.Message == "" { + r.Message("Bad request") + } } - return r.ctx.Status(r.httpStatus).JSON(r.body.Data) + var rspCtx = r.ctx.Status(r.httpStatus) + var rspErr error + + if r.body.Data == nil { + return rspCtx.Send([]byte{}) + } + + if r.body.Wrap { + rspErr = rspCtx.JSON(r.body) + } + + rspErr = rspCtx.JSON(r.body.Data) + + return rspErr } func (r *Response) Success(data any) *Response { diff --git a/internal/pkg/validator/validator.go b/internal/pkg/validator/validator.go index d179e2f..85fc229 100644 --- a/internal/pkg/validator/validator.go +++ b/internal/pkg/validator/validator.go @@ -2,9 +2,9 @@ package validator import ( "errors" + "leafdev.top/Leaf/leaf-library-3/internal/pkg/response" "github.com/gookit/validate" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" ) var ( @@ -27,11 +27,11 @@ func init() { }) } -func Struct(data interface{}) (validationErrors *[]dto.ValidateError, ok bool, err error) { +func Struct(data interface{}) (validationErrors *[]response.ValidateError, ok bool, err error) { v := validate.Struct(data) var e error - var ves []dto.ValidateError + var ves []response.ValidateError if v.Validate() { return &ves, true, nil // 返回指针 @@ -39,7 +39,7 @@ func Struct(data interface{}) (validationErrors *[]dto.ValidateError, ok bool, e e = ErrValidationFailed for _, err := range v.Errors { - ves = append(ves, dto.ValidateError{ + ves = append(ves, response.ValidateError{ Message: err.String(), }) } diff --git a/internal/services/auth/auth.go b/internal/services/auth/auth.go index a6e4438..7497d68 100644 --- a/internal/services/auth/auth.go +++ b/internal/services/auth/auth.go @@ -2,10 +2,9 @@ package auth import ( "context" - - "leafdev.top/Leaf/leaf-library-3/internal/types/constants" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" - "leafdev.top/Leaf/leaf-library-3/internal/types/user" + "leafdev.top/Leaf/leaf-library-3/internal/constants" + "leafdev.top/Leaf/leaf-library-3/internal/dto/user" + errs2 "leafdev.top/Leaf/leaf-library-3/internal/errs" "github.com/gofiber/fiber/v2" "github.com/mitchellh/mapstructure" @@ -82,24 +81,24 @@ func (a *Service) parseUserJWT(tokenType constants.JwtTokenTypes, jwtToken strin } else { token, err := a.jwks.ParseJWT(jwtToken) if err != nil { - return nil, errs.NotValidToken + return nil, errs2.NotValidToken } subStr, err := token.Claims.GetSubject() if err != nil { - return nil, errs.NotValidToken + return nil, errs2.NotValidToken } sub = user.ID(subStr) // 如果 token.Header 中没有 typ if token.Header["typ"] == "" { - return nil, errs.EmptyResponse + return nil, errs2.EmptyResponse } // 验证 token 类型 if tokenType != "" && tokenType.String() != token.Header["typ"] { - return nil, errs.TokenError + return nil, errs2.TokenError } jwtIdToken.Valid = true diff --git a/internal/services/collection/collection.go b/internal/services/collection/collection.go index 5352b73..d5d6831 100644 --- a/internal/services/collection/collection.go +++ b/internal/services/collection/collection.go @@ -3,9 +3,10 @@ package collection import ( "context" + "leafdev.top/Leaf/leaf-library-3/internal/errs" + + "leafdev.top/Leaf/leaf-library-3/internal/dto" "leafdev.top/Leaf/leaf-library-3/internal/entity" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" ) func (s *Service) Get(ctx context.Context, collectionId dto.EntityId) (*entity.Collection, error) { diff --git a/internal/services/document/document.go b/internal/services/document/document.go index 699f11e..eb91e2a 100644 --- a/internal/services/document/document.go +++ b/internal/services/document/document.go @@ -8,8 +8,8 @@ import ( "time" "gorm.io/gorm" + "leafdev.top/Leaf/leaf-library-3/internal/dto" "leafdev.top/Leaf/leaf-library-3/internal/entity" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" ) // Get 获取文档 diff --git a/internal/services/stream/producer.go b/internal/services/stream/producer.go index 73e43cf..f9fc02a 100644 --- a/internal/services/stream/producer.go +++ b/internal/services/stream/producer.go @@ -2,10 +2,10 @@ package stream import ( "context" + "leafdev.top/Leaf/leaf-library-3/internal/events" "time" "github.com/segmentio/kafka-go" - "leafdev.top/Leaf/leaf-library-3/internal/types/events" ) //var connections = map[string]*kafka.Conn{} diff --git a/internal/services/workspace/workspace.go b/internal/services/workspace/workspace.go index 843e428..6633548 100644 --- a/internal/services/workspace/workspace.go +++ b/internal/services/workspace/workspace.go @@ -3,10 +3,11 @@ package workspace import ( "context" + "leafdev.top/Leaf/leaf-library-3/internal/dto/user" + "leafdev.top/Leaf/leaf-library-3/internal/errs" + + "leafdev.top/Leaf/leaf-library-3/internal/dto" "leafdev.top/Leaf/leaf-library-3/internal/entity" - "leafdev.top/Leaf/leaf-library-3/internal/types/dto" - "leafdev.top/Leaf/leaf-library-3/internal/types/errs" - "leafdev.top/Leaf/leaf-library-3/internal/types/user" ) func (s *Service) Get(ctx context.Context, workspaceId dto.EntityId) (*entity.Workspace, error) { @@ -54,6 +55,10 @@ func (s *Service) Create(ctx context.Context, userId user.ID, name string) (*ent return workspace, nil } +func (s *Service) ListMembers(ctx context.Context, workspaceEntity *entity.Workspace) ([]*entity.WorkspaceMember, error) { + return s.dao.WithContext(ctx).WorkspaceMember.Where(s.dao.WorkspaceMember.WorkspaceId.Eq(workspaceEntity.ID.Uint())).Find() +} + func (s *Service) AddMember(ctx context.Context, userId user.ID, workspaceEntity *entity.Workspace) (*entity.WorkspaceMember, error) { memberExists, err := s.MemberExists(ctx, userId, workspaceEntity) if err != nil {