252 lines
8.5 KiB
Go
252 lines
8.5 KiB
Go
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"
|
|
)
|
|
|
|
type CollectionController struct {
|
|
collectionService *collection.Service
|
|
workspaceService *workspace.Service
|
|
authService *auth.Service
|
|
}
|
|
|
|
func NewCollectionController(
|
|
collectionService *collection.Service,
|
|
workspaceService *workspace.Service,
|
|
authService *auth.Service,
|
|
) *CollectionController {
|
|
return &CollectionController{
|
|
collectionService: collectionService,
|
|
workspaceService: workspaceService,
|
|
authService: authService,
|
|
}
|
|
}
|
|
|
|
// CreateCollection 创建集合
|
|
// @Summary 创建集合
|
|
// @Description 在工作空间下创建新的集合
|
|
// @Tags Collection
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body dto.CreateCollectionRequest true "创建集合请求"
|
|
// @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 response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
if validationErrors, ok, err := validator.Struct(req); !ok {
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).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 response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
if !exists {
|
|
return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send()
|
|
}
|
|
|
|
collectionEntity, err := c.collectionService.Create(ctx.Context(), req.WorkspaceID, req.Name)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(collectionEntity).Send()
|
|
}
|
|
|
|
// GetCollection 获取集合
|
|
// @Summary 获取集合
|
|
// @Description 根据集合ID获取集合详情
|
|
// @Tags Collection
|
|
// @Accept json
|
|
// @Produce json
|
|
// @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 response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
exists, err := c.collectionService.Exists(ctx.Context(), req.ID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
if !exists {
|
|
return response.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send()
|
|
}
|
|
|
|
collectionEntity, err := c.collectionService.Get(ctx.Context(), req.ID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(collectionEntity).Send()
|
|
}
|
|
|
|
// ListCollections 列出工作空间下的所有集合
|
|
// @Summary 列出集合列表
|
|
// @Description 获取指定工作空间下的所有集合
|
|
// @Tags Collection
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param workspace_id path int true "工作空间ID"
|
|
// @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 response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
// 检查工作空间是否存在
|
|
exists, err := c.workspaceService.Exists(ctx.Context(), req.WorkspaceID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
if !exists {
|
|
return response.Ctx(ctx).Error(errs.ErrWorkspaceNotExists).Status(fiber.StatusNotFound).Send()
|
|
}
|
|
|
|
collections, err := c.collectionService.List(ctx.Context(), req.WorkspaceID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.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} 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 response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
var req dto.UpdateCollectionRequest
|
|
if err := ctx.BodyParser(&req); err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
if validationErrors, ok, err := validator.Struct(req); !ok {
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
return response.Ctx(ctx).Data(validationErrors).Status(fiber.StatusBadRequest).Send()
|
|
}
|
|
|
|
// 检查集合是否存在
|
|
collectionEntity, err := c.collectionService.Get(ctx.Context(), params.ID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
if collectionEntity == nil {
|
|
return response.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send()
|
|
}
|
|
|
|
// 检查用户是否有权限访问该集合
|
|
user := c.authService.GetUser(ctx)
|
|
workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
if !isMember {
|
|
return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send()
|
|
}
|
|
|
|
// 更新集合
|
|
collectionEntity, err = c.collectionService.Update(ctx.Context(), params.ID, req.Name)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(collectionEntity).Send()
|
|
}
|
|
|
|
// DeleteCollection 删除集合
|
|
// @Summary 删除集合
|
|
// @Description 删除指定的集合
|
|
// @Tags Collection
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "集合ID"
|
|
// @Success 200 {object} dto.Response
|
|
// @Failure 404 {object} dto.Response "集合不存在"
|
|
// @Router /collections/{id} [delete]
|
|
func (c *CollectionController) DeleteCollection(ctx *fiber.Ctx) error {
|
|
var params dto.CollectionIDParam
|
|
if err := ctx.ParamsParser(¶ms); err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
// 检查集合是否存在
|
|
collectionEntity, err := c.collectionService.Get(ctx.Context(), params.ID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
if collectionEntity == nil {
|
|
return response.Ctx(ctx).Error(errs.ErrCollectionNotExists).Status(fiber.StatusNotFound).Send()
|
|
}
|
|
|
|
// 检查用户是否有权限访问该集合
|
|
user := c.authService.GetUser(ctx)
|
|
workspaceEntity, err := c.workspaceService.Get(ctx.Context(), collectionEntity.WorkspaceId)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
isMember, err := c.workspaceService.IsMember(ctx.Context(), user.ID, workspaceEntity)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
if !isMember {
|
|
return response.Ctx(ctx).Error(errs.ErrNoPermission).Status(fiber.StatusForbidden).Send()
|
|
}
|
|
|
|
// 删除集合
|
|
err = c.collectionService.Delete(ctx.Context(), params.ID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(nil).Send()
|
|
}
|