feat: 实现工程管理模块
- 新增 Project ProjectStyleRecord Task 数据模型 - 新增 ProjectService 服务层,支持工程和风格的 CRUD 操作 - 新增工程相关 handler,实现前端所需的所有工程接口 - 数据库层新增 GetDB 方法,便于服务层获取数据库连接 涉及接口: - GET /api/v1/projects - 获取工程列表 - POST /api/v1/projects - 创建工程 - GET /api/v1/projects/:projectId - 获取工程详情 - PUT /api/v1/projects/:projectId - 更新工程信息 - DELETE /api/v1/projects/:projectId - 删除工程 - GET /api/v1/projects/:projectId/style - 获取工程风格 - PUT /api/v1/projects/:projectId/style - 更新工程风格 - GET /api/v1/projects/:projectId/tasks - 获取工程任务列表
This commit is contained in:
@@ -0,0 +1,280 @@
|
||||
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))
|
||||
}
|
||||
Reference in New Issue
Block a user