From c94220d13eb7d0b036157db63492334be4acc5b9 Mon Sep 17 00:00:00 2001 From: ivamp Date: Sat, 7 Dec 2024 03:05:33 +0800 Subject: [PATCH] update --- docs/docs.go | 479 ++++++++++++--------- docs/swagger.json | 479 ++++++++++++--------- docs/swagger.yaml | 296 +++++++------ internal/api/http/controller/collection.go | 94 +++- internal/api/http/controller/document.go | 189 +++++++- internal/router/api.go | 7 +- internal/services/document/document.go | 5 + internal/types/dto/collection.go | 5 + internal/types/errs/model.go | 1 + 9 files changed, 983 insertions(+), 572 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index a8bd92a..0500691 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -15,6 +15,190 @@ const docTemplate = `{ "host": "{{.Host}}", "basePath": "{{.BasePath}}", "paths": { + "/blocks/{block_id}": { + "put": { + "description": "更新文档块的内容", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Document" + ], + "summary": "更新文档块", + "parameters": [ + { + "type": "integer", + "description": "文档块ID", + "name": "block_id", + "in": "path", + "required": true + }, + { + "description": "更新文档块请求", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UpdateBlockRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/dto.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/entity.DocumentBlock" + } + } + } + ] + } + }, + "400": { + "description": "参数验证失败", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/dto.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } + } + } + } + ] + } + }, + "404": { + "description": "文档块不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + }, + "delete": { + "description": "删除指定的文档块", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Document" + ], + "summary": "删除文档块", + "parameters": [ + { + "type": "integer", + "description": "文档块ID", + "name": "block_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/dto.Response" + } + }, + "404": { + "description": "文档块不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + } + }, + "/blocks/{block_id}/move": { + "post": { + "description": "移动文档块的位置", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Document" + ], + "summary": "移动文档块", + "parameters": [ + { + "type": "integer", + "description": "文档块ID", + "name": "block_id", + "in": "path", + "required": true + }, + { + "description": "移动文档块请求", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.MoveBlockRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/dto.Response" + } + }, + "400": { + "description": "参数验证失败", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/dto.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } + } + } + } + ] + } + }, + "404": { + "description": "文档块不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + } + }, "/collections": { "post": { "description": "在工作空间下创建新的集合", @@ -169,7 +353,7 @@ const docTemplate = `{ "parameters": [ { "type": "integer", - "description": "集合ID", + "description": "集��ID", "name": "id", "in": "path", "required": true @@ -202,6 +386,84 @@ const docTemplate = `{ } } }, + "put": { + "description": "更新集合的名称等信息", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Collection" + ], + "summary": "更新集合", + "parameters": [ + { + "type": "integer", + "description": "集合ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "更新集合请求", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UpdateCollectionRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/dto.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$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" + } + } + } + } + ] + } + }, + "404": { + "description": "集合不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + }, "delete": { "description": "删除指定的集合", "consumes": [ @@ -320,7 +582,7 @@ const docTemplate = `{ }, "/documents/{document_id}/blocks": { "get": { - "description": "获取指文档下的所有文档块", + "description": "获取指定文档下的所有文档块", "consumes": [ "application/json" ], @@ -449,211 +711,6 @@ const docTemplate = `{ } } }, - "/documents/{document_id}/blocks/{block_id}": { - "put": { - "description": "更新文档块的内容", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "Document" - ], - "summary": "更新文档块", - "parameters": [ - { - "type": "integer", - "description": "文档ID", - "name": "document_id", - "in": "path", - "required": true - }, - { - "type": "integer", - "description": "文档块ID", - "name": "block_id", - "in": "path", - "required": true - }, - { - "description": "更新文档块请求", - "name": "request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/dto.UpdateBlockRequest" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.DocumentBlock" - } - } - } - ] - } - }, - "400": { - "description": "参数验证失败", - "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] - } - }, - "404": { - "description": "文档块不存在", - "schema": { - "$ref": "#/definitions/dto.Response" - } - } - } - }, - "delete": { - "description": "删除指定的文档块", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "Document" - ], - "summary": "删除文档块", - "parameters": [ - { - "type": "integer", - "description": "文档ID", - "name": "document_id", - "in": "path", - "required": true - }, - { - "type": "integer", - "description": "文档块ID", - "name": "block_id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } - }, - "404": { - "description": "文档块不存在", - "schema": { - "$ref": "#/definitions/dto.Response" - } - } - } - } - }, - "/documents/{document_id}/blocks/{block_id}/move": { - "post": { - "description": "移动文档块的位置", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "Document" - ], - "summary": "移动文档块", - "parameters": [ - { - "type": "integer", - "description": "文档ID", - "name": "document_id", - "in": "path", - "required": true - }, - { - "type": "integer", - "description": "文档块ID", - "name": "block_id", - "in": "path", - "required": true - }, - { - "description": "移动文档块请求", - "name": "request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/dto.MoveBlockRequest" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } - }, - "400": { - "description": "参数验证失败", - "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] - } - }, - "404": { - "description": "文档块不存在", - "schema": { - "$ref": "#/definitions/dto.Response" - } - } - } - } - }, "/documents/{id}": { "get": { "description": "根据文档ID获取文档详情", @@ -1322,6 +1379,14 @@ const docTemplate = `{ } } }, + "dto.UpdateCollectionRequest": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + } + }, "dto.UpdateDocumentRequest": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index d48762d..69afac0 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -6,6 +6,190 @@ "version": "1.0" }, "paths": { + "/blocks/{block_id}": { + "put": { + "description": "更新文档块的内容", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Document" + ], + "summary": "更新文档块", + "parameters": [ + { + "type": "integer", + "description": "文档块ID", + "name": "block_id", + "in": "path", + "required": true + }, + { + "description": "更新文档块请求", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UpdateBlockRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/dto.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/entity.DocumentBlock" + } + } + } + ] + } + }, + "400": { + "description": "参数验证失败", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/dto.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } + } + } + } + ] + } + }, + "404": { + "description": "文档块不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + }, + "delete": { + "description": "删除指定的文档块", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Document" + ], + "summary": "删除文档块", + "parameters": [ + { + "type": "integer", + "description": "文档块ID", + "name": "block_id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/dto.Response" + } + }, + "404": { + "description": "文档块不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + } + }, + "/blocks/{block_id}/move": { + "post": { + "description": "移动文档块的位置", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Document" + ], + "summary": "移动文档块", + "parameters": [ + { + "type": "integer", + "description": "文档块ID", + "name": "block_id", + "in": "path", + "required": true + }, + { + "description": "移动文档块请求", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.MoveBlockRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/dto.Response" + } + }, + "400": { + "description": "参数验证失败", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/dto.Response" + }, + { + "type": "object", + "properties": { + "data": { + "type": "array", + "items": { + "$ref": "#/definitions/dto.ValidateError" + } + } + } + } + ] + } + }, + "404": { + "description": "文档块不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + } + }, "/collections": { "post": { "description": "在工作空间下创建新的集合", @@ -160,7 +344,7 @@ "parameters": [ { "type": "integer", - "description": "集合ID", + "description": "集��ID", "name": "id", "in": "path", "required": true @@ -193,6 +377,84 @@ } } }, + "put": { + "description": "更新集合的名称等信息", + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Collection" + ], + "summary": "更新集合", + "parameters": [ + { + "type": "integer", + "description": "集合ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "更新集合请求", + "name": "request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/dto.UpdateCollectionRequest" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "allOf": [ + { + "$ref": "#/definitions/dto.Response" + }, + { + "type": "object", + "properties": { + "data": { + "$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" + } + } + } + } + ] + } + }, + "404": { + "description": "集合不存在", + "schema": { + "$ref": "#/definitions/dto.Response" + } + } + } + }, "delete": { "description": "删除指定的集合", "consumes": [ @@ -311,7 +573,7 @@ }, "/documents/{document_id}/blocks": { "get": { - "description": "获取指文档下的所有文档块", + "description": "获取指定文档下的所有文档块", "consumes": [ "application/json" ], @@ -440,211 +702,6 @@ } } }, - "/documents/{document_id}/blocks/{block_id}": { - "put": { - "description": "更新文档块的内容", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "Document" - ], - "summary": "更新文档块", - "parameters": [ - { - "type": "integer", - "description": "文档ID", - "name": "document_id", - "in": "path", - "required": true - }, - { - "type": "integer", - "description": "文档块ID", - "name": "block_id", - "in": "path", - "required": true - }, - { - "description": "更新文档块请求", - "name": "request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/dto.UpdateBlockRequest" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "$ref": "#/definitions/entity.DocumentBlock" - } - } - } - ] - } - }, - "400": { - "description": "参数验证失败", - "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] - } - }, - "404": { - "description": "文档块不存在", - "schema": { - "$ref": "#/definitions/dto.Response" - } - } - } - }, - "delete": { - "description": "删除指定的文档块", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "Document" - ], - "summary": "删除文档块", - "parameters": [ - { - "type": "integer", - "description": "文档ID", - "name": "document_id", - "in": "path", - "required": true - }, - { - "type": "integer", - "description": "文档块ID", - "name": "block_id", - "in": "path", - "required": true - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } - }, - "404": { - "description": "文档块不存在", - "schema": { - "$ref": "#/definitions/dto.Response" - } - } - } - } - }, - "/documents/{document_id}/blocks/{block_id}/move": { - "post": { - "description": "移动文档块的位置", - "consumes": [ - "application/json" - ], - "produces": [ - "application/json" - ], - "tags": [ - "Document" - ], - "summary": "移动文档块", - "parameters": [ - { - "type": "integer", - "description": "文档ID", - "name": "document_id", - "in": "path", - "required": true - }, - { - "type": "integer", - "description": "文档块ID", - "name": "block_id", - "in": "path", - "required": true - }, - { - "description": "移动文档块请求", - "name": "request", - "in": "body", - "required": true, - "schema": { - "$ref": "#/definitions/dto.MoveBlockRequest" - } - } - ], - "responses": { - "200": { - "description": "OK", - "schema": { - "$ref": "#/definitions/dto.Response" - } - }, - "400": { - "description": "参数验证失败", - "schema": { - "allOf": [ - { - "$ref": "#/definitions/dto.Response" - }, - { - "type": "object", - "properties": { - "data": { - "type": "array", - "items": { - "$ref": "#/definitions/dto.ValidateError" - } - } - } - } - ] - } - }, - "404": { - "description": "文档块不存在", - "schema": { - "$ref": "#/definitions/dto.Response" - } - } - } - } - }, "/documents/{id}": { "get": { "description": "根据文档ID获取文档详情", @@ -1313,6 +1370,14 @@ } } }, + "dto.UpdateCollectionRequest": { + "type": "object", + "properties": { + "name": { + "type": "string" + } + } + }, "dto.UpdateDocumentRequest": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 198e629..c426061 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -79,6 +79,11 @@ definitions: required: - content type: object + dto.UpdateCollectionRequest: + properties: + name: + type: string + type: object dto.UpdateDocumentRequest: properties: name: @@ -201,6 +206,119 @@ info: title: API Docs version: "1.0" paths: + /blocks/{block_id}: + delete: + consumes: + - application/json + description: 删除指定的文档块 + parameters: + - description: 文档块ID + in: path + name: block_id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/dto.Response' + "404": + description: 文档块不存在 + schema: + $ref: '#/definitions/dto.Response' + summary: 删除文档块 + tags: + - Document + put: + consumes: + - application/json + description: 更新文档块的内容 + parameters: + - description: 文档块ID + in: path + name: block_id + required: true + type: integer + - description: 更新文档块请求 + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.UpdateBlockRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/dto.Response' + - properties: + data: + $ref: '#/definitions/entity.DocumentBlock' + type: object + "400": + description: 参数验证失败 + schema: + allOf: + - $ref: '#/definitions/dto.Response' + - properties: + data: + items: + $ref: '#/definitions/dto.ValidateError' + type: array + type: object + "404": + description: 文档块不存在 + schema: + $ref: '#/definitions/dto.Response' + summary: 更新文档块 + tags: + - Document + /blocks/{block_id}/move: + post: + consumes: + - application/json + description: 移动文档块的位置 + parameters: + - description: 文档块ID + in: path + name: block_id + required: true + type: integer + - description: 移动文档块请求 + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.MoveBlockRequest' + produces: + - application/json + 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 + "404": + description: 文档块不存在 + schema: + $ref: '#/definitions/dto.Response' + summary: 移动文档块 + tags: + - Document /collections: post: consumes: @@ -313,7 +431,7 @@ paths: - application/json description: 根据集合ID获取集合详情 parameters: - - description: 集合ID + - description: 集��ID in: path name: id required: true @@ -337,6 +455,52 @@ paths: summary: 获取集合 tags: - Collection + put: + consumes: + - application/json + description: 更新集合的名称等信息 + parameters: + - description: 集合ID + in: path + name: id + required: true + type: integer + - description: 更新集合请求 + in: body + name: request + required: true + schema: + $ref: '#/definitions/dto.UpdateCollectionRequest' + produces: + - application/json + responses: + "200": + description: OK + schema: + allOf: + - $ref: '#/definitions/dto.Response' + - properties: + data: + $ref: '#/definitions/leafdev_top_Leaf_leaf-library-3_internal_entity.Collection' + type: object + "400": + description: 参数验证失败 + schema: + allOf: + - $ref: '#/definitions/dto.Response' + - properties: + data: + items: + $ref: '#/definitions/dto.ValidateError' + type: array + type: object + "404": + description: 集合不存在 + schema: + $ref: '#/definitions/dto.Response' + summary: 更新集合 + tags: + - Collection /documents: post: consumes: @@ -387,7 +551,7 @@ paths: get: consumes: - application/json - description: 获取指文档下的所有文档块 + description: 获取指定文档下的所有文档块 parameters: - description: 文档ID in: path @@ -461,134 +625,6 @@ paths: summary: 创建文档块 tags: - Document - /documents/{document_id}/blocks/{block_id}: - delete: - consumes: - - application/json - description: 删除指定的文档块 - parameters: - - description: 文档ID - in: path - name: document_id - required: true - type: integer - - description: 文档块ID - in: path - name: block_id - required: true - type: integer - produces: - - application/json - responses: - "200": - description: OK - schema: - $ref: '#/definitions/dto.Response' - "404": - description: 文档块不存在 - schema: - $ref: '#/definitions/dto.Response' - summary: 删除文档块 - tags: - - Document - put: - consumes: - - application/json - description: 更新文档块的内容 - parameters: - - description: 文档ID - in: path - name: document_id - required: true - type: integer - - description: 文档块ID - in: path - name: block_id - required: true - type: integer - - description: 更新文档块请求 - in: body - name: request - required: true - schema: - $ref: '#/definitions/dto.UpdateBlockRequest' - produces: - - application/json - responses: - "200": - description: OK - schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - $ref: '#/definitions/entity.DocumentBlock' - type: object - "400": - description: 参数验证失败 - schema: - allOf: - - $ref: '#/definitions/dto.Response' - - properties: - data: - items: - $ref: '#/definitions/dto.ValidateError' - type: array - type: object - "404": - description: 文档块不存在 - schema: - $ref: '#/definitions/dto.Response' - summary: 更新文档块 - tags: - - Document - /documents/{document_id}/blocks/{block_id}/move: - post: - consumes: - - application/json - description: 移动文档块的位置 - parameters: - - description: 文档ID - in: path - name: document_id - required: true - type: integer - - description: 文档块ID - in: path - name: block_id - required: true - type: integer - - description: 移动文档块请求 - in: body - name: request - required: true - schema: - $ref: '#/definitions/dto.MoveBlockRequest' - produces: - - application/json - 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 - "404": - description: 文档块不存在 - schema: - $ref: '#/definitions/dto.Response' - summary: 移动文档块 - tags: - - Document /documents/{id}: delete: consumes: diff --git a/internal/api/http/controller/collection.go b/internal/api/http/controller/collection.go index 2c2cfb7..6121dcd 100644 --- a/internal/api/http/controller/collection.go +++ b/internal/api/http/controller/collection.go @@ -76,7 +76,7 @@ func (c *CollectionController) CreateCollection(ctx *fiber.Ctx) error { // @Tags Collection // @Accept json // @Produce json -// @Param id path int true "集合ID" +// @Param id path int true "集��ID" // @Success 200 {object} dto.Response{data=entity.Collection} // @Failure 404 {object} dto.Response "集合不存在" // @Router /collections/{id} [get] @@ -118,7 +118,7 @@ func (c *CollectionController) ListCollections(ctx *fiber.Ctx) error { return err } - // 检查工作空间是否存在 + // 检���工作空间是否存在 exists, err := c.workspaceService.Exists(ctx.Context(), req.WorkspaceID) if err != nil { return err @@ -135,6 +135,69 @@ func (c *CollectionController) ListCollections(ctx *fiber.Ctx) error { return dto.Ctx(ctx).Success(collections).Send() } +// UpdateCollection 更新集合 +// @Summary 更新集合 +// @Description 更新集合的名称等信息 +// @Tags Collection +// @Accept json +// @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} "参数验证失败" +// @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 + } + + var req dto.UpdateCollectionRequest + if err := ctx.BodyParser(&req); err != nil { + return err + } + + if validationErrors, ok, err := validator.Struct(req); !ok { + if err != nil { + return err + } + return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() + } + + // 检查集合是否存在 + collection, err := c.collectionService.Get(ctx.Context(), params.ID) + if err != nil { + return err + } + if collection == nil { + return dto.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() + } + + // 检查用户是否有权限访问该集合 + user := c.authService.GetUser(ctx) + workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + if err != nil { + return err + } + + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + if err != nil { + return err + } + if !isMember { + return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + } + + // 更新集合 + collection, err = c.collectionService.Update(ctx.Context(), params.ID, req.Name) + if err != nil { + return err + } + + return dto.Ctx(ctx).Success(collection).Send() +} + // DeleteCollection 删除集合 // @Summary 删除集合 // @Description 删除指定的集合 @@ -146,20 +209,37 @@ func (c *CollectionController) ListCollections(ctx *fiber.Ctx) error { // @Failure 404 {object} dto.Response "集合不存在" // @Router /collections/{id} [delete] func (c *CollectionController) DeleteCollection(ctx *fiber.Ctx) error { - var req dto.DeleteCollectionRequest - if err := ctx.ParamsParser(&req); err != nil { + var params dto.CollectionIDParam + if err := ctx.ParamsParser(¶ms); err != nil { return err } - exists, err := c.collectionService.Exists(ctx.Context(), req.ID) + // 检查集合是否存在 + collection, err := c.collectionService.Get(ctx.Context(), params.ID) if err != nil { return err } - if !exists { + if collection == nil { return dto.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send() } - err = c.collectionService.Delete(ctx.Context(), req.ID) + // 检查用户是否有权限访问该集合 + user := c.authService.GetUser(ctx) + workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + if err != nil { + return err + } + + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + if err != nil { + return err + } + if !isMember { + return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + } + + // 删除集合 + err = c.collectionService.Delete(ctx.Context(), params.ID) if err != nil { return err } diff --git a/internal/api/http/controller/document.go b/internal/api/http/controller/document.go index 00187a9..80586c0 100644 --- a/internal/api/http/controller/document.go +++ b/internal/api/http/controller/document.go @@ -340,6 +340,34 @@ func (c *DocumentController) CreateBlock(ctx *fiber.Ctx) error { return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() } + // 检查文档是否存在 + doc, err := c.documentService.Get(ctx.Context(), params.DocumentID) + if err != nil { + return err + } + if doc == nil { + return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + } + + // 检查用户是否有权限访问该文档 + user := c.authService.GetUser(ctx) + collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + if err != nil { + return err + } + workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + if err != nil { + return err + } + + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + if err != nil { + return err + } + if !isMember { + return dto.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 @@ -354,13 +382,12 @@ func (c *DocumentController) CreateBlock(ctx *fiber.Ctx) error { // @Tags Document // @Accept json // @Produce json -// @Param document_id path int true "文档ID" // @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} "参数验证失败" // @Failure 404 {object} dto.Response "文档块不存在" -// @Router /documents/{document_id}/blocks/{block_id} [put] +// @Router /blocks/{block_id} [put] func (c *DocumentController) UpdateBlock(ctx *fiber.Ctx) error { var params dto.BlockIDParam if err := ctx.ParamsParser(¶ms); err != nil { @@ -379,7 +406,44 @@ func (c *DocumentController) UpdateBlock(ctx *fiber.Ctx) error { return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() } - block, err := c.documentService.UpdateBlock(ctx.Context(), params.ID, req.Content) + // 获取块信息 + block, err := c.documentService.GetBlock(ctx.Context(), params.ID) + if err != nil { + return err + } + if block == nil { + return dto.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() + } + + // 检查文档是否存在 + doc, err := c.documentService.Get(ctx.Context(), block.DocumentId) + if err != nil { + return err + } + if doc == nil { + return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + } + + // 检查用户是否有权限访问该文档 + user := c.authService.GetUser(ctx) + collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + if err != nil { + return err + } + workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + if err != nil { + return err + } + + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + if err != nil { + return err + } + if !isMember { + return dto.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 } @@ -387,24 +451,60 @@ func (c *DocumentController) UpdateBlock(ctx *fiber.Ctx) error { return dto.Ctx(ctx).Success(block).Send() } -// DeleteBlock 删除文档块 +// DeleteBlock ���除文档块 // @Summary 删除文档块 // @Description 删除指定的文档块 // @Tags Document // @Accept json // @Produce json -// @Param document_id path int true "文档ID" // @Param block_id path int true "文档块ID" // @Success 200 {object} dto.Response // @Failure 404 {object} dto.Response "文档块不存在" -// @Router /documents/{document_id}/blocks/{block_id} [delete] +// @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 } - err := c.documentService.DeleteBlock(ctx.Context(), params.ID) + // 获取块信息 + block, err := c.documentService.GetBlock(ctx.Context(), params.ID) + if err != nil { + return err + } + if block == nil { + return dto.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() + } + + // 检查文档是否存在 + doc, err := c.documentService.Get(ctx.Context(), block.DocumentId) + if err != nil { + return err + } + if doc == nil { + return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + } + + // 检查用户是否有权限访问该文档 + user := c.authService.GetUser(ctx) + collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + if err != nil { + return err + } + workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + if err != nil { + return err + } + + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + if err != nil { + return err + } + if !isMember { + return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + } + + err = c.documentService.DeleteBlock(ctx.Context(), params.ID) if err != nil { return err } @@ -414,7 +514,7 @@ func (c *DocumentController) DeleteBlock(ctx *fiber.Ctx) error { // ListBlocks 列出文档下的所有块 // @Summary 列出文档块列表 -// @Description 获取指文档下的所有文档块 +// @Description 获取指定文档下的所有文档块 // @Tags Document // @Accept json // @Produce json @@ -428,6 +528,34 @@ func (c *DocumentController) ListBlocks(ctx *fiber.Ctx) error { return err } + // 检查文档是否存在 + doc, err := c.documentService.Get(ctx.Context(), params.DocumentID) + if err != nil { + return err + } + if doc == nil { + return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + } + + // 检查用户是否有权限访问该文档 + user := c.authService.GetUser(ctx) + collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + if err != nil { + return err + } + workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + if err != nil { + return err + } + + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + if err != nil { + return err + } + if !isMember { + return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + } + blocks, err := c.documentService.ListBlocks(ctx.Context(), params.DocumentID) if err != nil { return err @@ -442,13 +570,12 @@ func (c *DocumentController) ListBlocks(ctx *fiber.Ctx) error { // @Tags Document // @Accept json // @Produce json -// @Param document_id path int true "文档ID" // @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} "参数验证失败" // @Failure 404 {object} dto.Response "文档块不存在" -// @Router /documents/{document_id}/blocks/{block_id}/move [post] +// @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 { @@ -460,14 +587,44 @@ func (c *DocumentController) MoveBlock(ctx *fiber.Ctx) error { return err } - if validationErrors, ok, err := validator.Struct(req); !ok { - if err != nil { - return err - } - return dto.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send() + // 获取块信息 + block, err := c.documentService.GetBlock(ctx.Context(), params.ID) + if err != nil { + return err + } + if block == nil { + return dto.Ctx(ctx).Error(errs.ErrBlockNotExists).Status(fiber.StatusNotFound).Send() } - err := c.documentService.MoveBlock(ctx.Context(), params.ID, req.AfterBlockID) + // 检查文档是否存在 + doc, err := c.documentService.Get(ctx.Context(), block.DocumentId) + if err != nil { + return err + } + if doc == nil { + return dto.Ctx(ctx).Error(errs.ErrDocumentNotExists).Status(fiber.StatusNotFound).Send() + } + + // 检查用户是否有权限访问该文档 + user := c.authService.GetUser(ctx) + collection, err := c.collectionService.Get(ctx.Context(), doc.CollectionId) + if err != nil { + return err + } + workspace, err := c.workspaceService.Get(ctx.Context(), collection.WorkspaceId) + if err != nil { + return err + } + + isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspace) + if err != nil { + return err + } + if !isMember { + return dto.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send() + } + + err = c.documentService.MoveBlock(ctx.Context(), params.ID, req.AfterBlockID) if err != nil { return err } diff --git a/internal/router/api.go b/internal/router/api.go index 0f628c1..4f7f90a 100644 --- a/internal/router/api.go +++ b/internal/router/api.go @@ -47,6 +47,8 @@ func (a *Api) V1(r fiber.Router) { { collections.Post("/", a.HttpHandler.Collection.CreateCollection) collections.Get("/:id", a.HttpHandler.Collection.GetCollection) + collections.Put("/:id", a.HttpHandler.Collection.UpdateCollection) + collections.Delete("/:id", a.HttpHandler.Collection.DeleteCollection) // 集合下的文档 collections.Get("/:collection_id/documents", a.HttpHandler.Document.ListDocuments) @@ -73,9 +75,4 @@ func (a *Api) V1(r fiber.Router) { blocks.Post("/:block_id/move", a.HttpHandler.Document.MoveBlock) } } - - // guest := r.Group("/api/v1") - // { - // guest.Get("/guest_ping", a.HttpHandler.User.Test) - // } } diff --git a/internal/services/document/document.go b/internal/services/document/document.go index 8bb7faa..699f11e 100644 --- a/internal/services/document/document.go +++ b/internal/services/document/document.go @@ -254,3 +254,8 @@ func (s *Service) List(ctx context.Context, collectionId dto.EntityId, parentId return q.Find() } + +// GetBlock 获取文档块 +func (s *Service) GetBlock(ctx context.Context, blockId dto.EntityId) (*entity.DocumentBlock, error) { + return s.dao.DocumentBlock.Where(s.dao.DocumentBlock.ID.Eq(blockId.Uint())).First() +} diff --git a/internal/types/dto/collection.go b/internal/types/dto/collection.go index 9fd0342..a213adf 100644 --- a/internal/types/dto/collection.go +++ b/internal/types/dto/collection.go @@ -20,3 +20,8 @@ type DeleteCollectionRequest struct { type ListCollectionsRequest struct { WorkspaceID EntityId `params:"workspace_id"` } + +// UpdateCollectionRequest 更新集合请求 +type UpdateCollectionRequest struct { + Name string `json:"name" validate:"required|minLen:1"` +} diff --git a/internal/types/errs/model.go b/internal/types/errs/model.go index 4304b87..fe6cf15 100644 --- a/internal/types/errs/model.go +++ b/internal/types/errs/model.go @@ -8,4 +8,5 @@ var ( ErrNoPermission = errors.New("no permission") ErrDocumentNotExists = errors.New("document not exists") ErrInvalidParentDocument = errors.New("invalid parent document") + ErrBlockNotExists = errors.New("block not exists") )