281 lines
8.4 KiB
Go
281 lines
8.4 KiB
Go
|
|
package handler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
|
||
|
|
"gen2d/internal/logger"
|
||
|
|
"gen2d/internal/model"
|
||
|
|
"gen2d/internal/service"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
)
|
||
|
|
|
||
|
|
// CreateProjectRequest 创建工程请求。
|
||
|
|
type CreateProjectRequest struct {
|
||
|
|
Name string `json:"name" binding:"required,min=1,max=100"`
|
||
|
|
Style map[string]string `json:"style"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateProjectRequest 更新工程请求。
|
||
|
|
type UpdateProjectRequest struct {
|
||
|
|
Name string `json:"name" binding:"omitempty,min=1,max=100"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateStyleRequest 更新风格请求。
|
||
|
|
type UpdateStyleRequest struct {
|
||
|
|
KvPairs map[string]string `json:"kvPairs"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// CreateProject 创建工程。
|
||
|
|
func CreateProject(c *gin.Context) {
|
||
|
|
userID := c.GetUint("userID")
|
||
|
|
if userID == 0 {
|
||
|
|
c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, "未认证"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
var req CreateProjectRequest
|
||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "参数错误: "+err.Error()))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectSvc := service.GetProjectService()
|
||
|
|
proj, err := projectSvc.CreateProject(c.Request.Context(), userID, req.Name, req.Style)
|
||
|
|
if err != nil {
|
||
|
|
logger.FromCtx(c.Request.Context()).Error("create project failed", "error", err)
|
||
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "创建工程失败"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusCreated, model.OK(proj))
|
||
|
|
}
|
||
|
|
|
||
|
|
// ListProjects 获取工程列表。
|
||
|
|
func ListProjects(c *gin.Context) {
|
||
|
|
userID := c.GetUint("userID")
|
||
|
|
if userID == 0 {
|
||
|
|
c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, "未认证"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
pageStr := c.DefaultQuery("page", "1")
|
||
|
|
pageSizeStr := c.DefaultQuery("pageSize", "20")
|
||
|
|
|
||
|
|
page, err := strconv.Atoi(pageStr)
|
||
|
|
if err != nil || page < 1 {
|
||
|
|
page = 1
|
||
|
|
}
|
||
|
|
|
||
|
|
pageSize, err := strconv.Atoi(pageSizeStr)
|
||
|
|
if err != nil || pageSize < 1 || pageSize > 100 {
|
||
|
|
pageSize = 20
|
||
|
|
}
|
||
|
|
|
||
|
|
projectSvc := service.GetProjectService()
|
||
|
|
result, err := projectSvc.ListProjects(c.Request.Context(), userID, page, pageSize)
|
||
|
|
if err != nil {
|
||
|
|
logger.FromCtx(c.Request.Context()).Error("list projects failed", "error", err)
|
||
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "获取工程列表失败"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, model.OK(result))
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetProject 获取工程详情。
|
||
|
|
func GetProject(c *gin.Context) {
|
||
|
|
userID := c.GetUint("userID")
|
||
|
|
if userID == 0 {
|
||
|
|
c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, "未认证"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectIDStr := c.Param("projectId")
|
||
|
|
projectID, err := strconv.ParseUint(projectIDStr, 10, 32)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "无效的工程ID"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectSvc := service.GetProjectService()
|
||
|
|
proj, err := projectSvc.GetProject(c.Request.Context(), userID, uint(projectID))
|
||
|
|
if err != nil {
|
||
|
|
logger.FromCtx(c.Request.Context()).Error("get project failed", "error", err)
|
||
|
|
if strings.Contains(err.Error(), "not found") {
|
||
|
|
c.JSON(http.StatusNotFound, model.Fail(http.StatusNotFound, "工程不存在"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "获取工程详情失败"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, model.OK(proj))
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateProject 更新工程信息。
|
||
|
|
func UpdateProject(c *gin.Context) {
|
||
|
|
userID := c.GetUint("userID")
|
||
|
|
if userID == 0 {
|
||
|
|
c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, "未认证"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectIDStr := c.Param("projectId")
|
||
|
|
projectID, err := strconv.ParseUint(projectIDStr, 10, 32)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "无效的工程ID"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
var req UpdateProjectRequest
|
||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "参数错误: "+err.Error()))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectSvc := service.GetProjectService()
|
||
|
|
proj, err := projectSvc.UpdateProject(c.Request.Context(), userID, uint(projectID), req.Name)
|
||
|
|
if err != nil {
|
||
|
|
logger.FromCtx(c.Request.Context()).Error("update project failed", "error", err)
|
||
|
|
if strings.Contains(err.Error(), "not found") {
|
||
|
|
c.JSON(http.StatusNotFound, model.Fail(http.StatusNotFound, "工程不存在"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "更新工程信息失败"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, model.OK(proj))
|
||
|
|
}
|
||
|
|
|
||
|
|
// DeleteProject 删除工程。
|
||
|
|
func DeleteProject(c *gin.Context) {
|
||
|
|
userID := c.GetUint("userID")
|
||
|
|
if userID == 0 {
|
||
|
|
c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, "未认证"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectIDStr := c.Param("projectId")
|
||
|
|
projectID, err := strconv.ParseUint(projectIDStr, 10, 32)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "无效的工程ID"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectSvc := service.GetProjectService()
|
||
|
|
err = projectSvc.DeleteProject(c.Request.Context(), userID, uint(projectID))
|
||
|
|
if err != nil {
|
||
|
|
logger.FromCtx(c.Request.Context()).Error("delete project failed", "error", err)
|
||
|
|
if strings.Contains(err.Error(), "not found") {
|
||
|
|
c.JSON(http.StatusNotFound, model.Fail(http.StatusNotFound, "工程不存在"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "删除工程失败"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, model.OK(nil))
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetStyle 获取工程风格。
|
||
|
|
func GetStyle(c *gin.Context) {
|
||
|
|
userID := c.GetUint("userID")
|
||
|
|
if userID == 0 {
|
||
|
|
c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, "未认证"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectIDStr := c.Param("projectId")
|
||
|
|
projectID, err := strconv.ParseUint(projectIDStr, 10, 32)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "无效的工程ID"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectSvc := service.GetProjectService()
|
||
|
|
style, err := projectSvc.GetStyle(c.Request.Context(), userID, uint(projectID))
|
||
|
|
if err != nil {
|
||
|
|
logger.FromCtx(c.Request.Context()).Error("get style failed", "error", err)
|
||
|
|
if strings.Contains(err.Error(), "not found") {
|
||
|
|
c.JSON(http.StatusNotFound, model.Fail(http.StatusNotFound, "工程不存在"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "获取工程风格失败"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, model.OK(map[string]interface{}{
|
||
|
|
"kvPairs": style,
|
||
|
|
}))
|
||
|
|
}
|
||
|
|
|
||
|
|
// UpdateStyle 更新工程风格。
|
||
|
|
func UpdateStyle(c *gin.Context) {
|
||
|
|
userID := c.GetUint("userID")
|
||
|
|
if userID == 0 {
|
||
|
|
c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, "未认证"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectIDStr := c.Param("projectId")
|
||
|
|
projectID, err := strconv.ParseUint(projectIDStr, 10, 32)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "无效的工程ID"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
var req UpdateStyleRequest
|
||
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "参数错误: "+err.Error()))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectSvc := service.GetProjectService()
|
||
|
|
err = projectSvc.UpdateStyle(c.Request.Context(), userID, uint(projectID), req.KvPairs)
|
||
|
|
if err != nil {
|
||
|
|
logger.FromCtx(c.Request.Context()).Error("update style failed", "error", err)
|
||
|
|
if strings.Contains(err.Error(), "not found") {
|
||
|
|
c.JSON(http.StatusNotFound, model.Fail(http.StatusNotFound, "工程不存在"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "更新工程风格失败"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, model.OK(nil))
|
||
|
|
}
|
||
|
|
|
||
|
|
// GetProjectTasks 获取工程下的任务列表。
|
||
|
|
func GetProjectTasks(c *gin.Context) {
|
||
|
|
userID := c.GetUint("userID")
|
||
|
|
if userID == 0 {
|
||
|
|
c.JSON(http.StatusUnauthorized, model.Fail(http.StatusUnauthorized, "未认证"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectIDStr := c.Param("projectId")
|
||
|
|
projectID, err := strconv.ParseUint(projectIDStr, 10, 32)
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusBadRequest, model.Fail(http.StatusBadRequest, "无效的工程ID"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
projectSvc := service.GetProjectService()
|
||
|
|
tasks, err := projectSvc.GetTasks(c.Request.Context(), userID, uint(projectID))
|
||
|
|
if err != nil {
|
||
|
|
logger.FromCtx(c.Request.Context()).Error("get tasks failed", "error", err)
|
||
|
|
if strings.Contains(err.Error(), "not found") {
|
||
|
|
c.JSON(http.StatusNotFound, model.Fail(http.StatusNotFound, "工程不存在"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusInternalServerError, model.Fail(http.StatusInternalServerError, "获取任务列表失败"))
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.JSON(http.StatusOK, model.OK(tasks))
|
||
|
|
}
|