281 lines
9.4 KiB
Go
281 lines
9.4 KiB
Go
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"
|
|
)
|
|
|
|
type WorkspaceController struct {
|
|
workspaceService *workspace.Service
|
|
authService *auth.Service
|
|
}
|
|
|
|
func NewWorkspaceController(workspaceService *workspace.Service, authService *auth.Service) *WorkspaceController {
|
|
return &WorkspaceController{
|
|
workspaceService: workspaceService,
|
|
authService: authService,
|
|
}
|
|
}
|
|
|
|
// CreateWorkspace 创建工作空间
|
|
// @Summary 创建工作空间
|
|
// @Description 创建一个新的工作空间
|
|
// @Tags Workspace
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param request body dto.CreateWorkspaceRequest true "创建工作空间请求"
|
|
// @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 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()
|
|
}
|
|
|
|
// 从上下文获取用户ID
|
|
authUser := c.authService.GetUser(ctx)
|
|
userId := authUser.ID
|
|
|
|
workspaceEntity, err := c.workspaceService.Create(ctx.Context(), userId, req.Name)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(workspaceEntity).Send()
|
|
}
|
|
|
|
// GetWorkspace 获取工作空间
|
|
// @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} [get]
|
|
func (c *WorkspaceController) GetWorkspace(ctx *fiber.Ctx) error {
|
|
var params dto.WorkspaceIDParam
|
|
if err := ctx.ParamsParser(¶ms); err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).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()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(workspaceEntity).Send()
|
|
}
|
|
|
|
// ListWorkspaces 列出用户的所有工作空间
|
|
// @Summary 列出工作空间列表
|
|
// @Description 获取当前用户的所有工作空间
|
|
// @Tags Workspace
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Success 200 {object} []entity.Workspace
|
|
// @Router /workspaces [get]
|
|
func (c *WorkspaceController) ListWorkspaces(ctx *fiber.Ctx) error {
|
|
// 从上下文获取用户ID
|
|
authUser := c.authService.GetUser(ctx)
|
|
|
|
workspaces, err := c.workspaceService.List(ctx.Context(), authUser.ID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(workspaces).Send()
|
|
}
|
|
|
|
// DeleteWorkspace 删除工作空间
|
|
// @Summary 删除工作空间
|
|
// @Description 删除指定的工作空间
|
|
// @Tags Workspace
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "工作空间ID"
|
|
// @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 response.Ctx(ctx).Status(http.StatusInternalServerError).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()
|
|
}
|
|
|
|
err = c.workspaceService.DeleteWorkspace(ctx.Context(), params.ID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).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 添加工作空间成员
|
|
// @Summary 添加工作空间成员
|
|
// @Description 向工作空间添加新成员
|
|
// @Tags Workspace
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "工作空间ID"
|
|
// @Param request body dto.AddWorkspaceMemberRequest true "添加成员请求"
|
|
// @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 response.Ctx(ctx).Status(http.StatusInternalServerError).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()
|
|
}
|
|
|
|
var req dto.AddWorkspaceMemberRequest
|
|
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()
|
|
}
|
|
|
|
workspaceEntity, err := c.workspaceService.Get(ctx.Context(), params.ID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
member, err := c.workspaceService.AddMember(ctx.Context(), req.UserID, workspaceEntity)
|
|
if err != nil {
|
|
if errors.Is(err, errs.ErrMemberAlreadyExists) {
|
|
return response.Ctx(ctx).Error(err).Status(fiber.StatusConflict).Send()
|
|
}
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(member).Send()
|
|
}
|
|
|
|
// RemoveMember 移除工作空间成员
|
|
// @Summary 移除工作空间成员
|
|
// @Description 从工作空间移除成员
|
|
// @Tags Workspace
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param id path int true "工作空间ID"
|
|
// @Param user_id path string true "用户ID"
|
|
// @Success 200 {object} dto.Response
|
|
// @Failure 404 {object} dto.Response "工作空间不存在或成员不存在"
|
|
// @Router /workspaces/{id}/members/{user_id} [delete]
|
|
func (c *WorkspaceController) RemoveMember(ctx *fiber.Ctx) error {
|
|
var params dto.WorkspaceMemberParam
|
|
if err := ctx.ParamsParser(¶ms); err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
exists, err := c.workspaceService.Exists(ctx.Context(), params.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()
|
|
}
|
|
|
|
workspaceEntity, err := c.workspaceService.Get(ctx.Context(), params.WorkspaceID)
|
|
if err != nil {
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
err = c.workspaceService.RemoveMember(ctx.Context(), user.ID(params.UserID), workspaceEntity)
|
|
if err != nil {
|
|
if errors.Is(err, errs.ErrMemberNotExists) {
|
|
return response.Ctx(ctx).Error(err).Status(fiber.StatusNotFound).Send()
|
|
}
|
|
return response.Ctx(ctx).Status(http.StatusInternalServerError).Send()
|
|
}
|
|
|
|
return response.Ctx(ctx).Success(nil).Send()
|
|
}
|