refactor(api): expose project identity DTOs
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package projects
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
"senlinai-agent/backend/internal/httpx"
|
||||
"senlinai-agent/backend/internal/logic/auth"
|
||||
"senlinai-agent/backend/internal/logic/files"
|
||||
)
|
||||
@@ -27,6 +30,8 @@ func NewHandler(service *Service, fileServices ...*files.Service) *Handler {
|
||||
func (h *Handler) Register(router gin.IRouter) {
|
||||
router.POST("/projects", h.createProject)
|
||||
router.GET("/projects", h.listProjects)
|
||||
router.GET("/projects/:id", h.getProject)
|
||||
router.PATCH("/projects/:id", h.updateProject)
|
||||
router.GET("/projects/:id/dashboard", h.dashboard)
|
||||
router.GET("/projects/:id/workspace", h.workspace)
|
||||
router.GET("/projects/:id/tags", h.listTags)
|
||||
@@ -40,48 +45,76 @@ func (h *Handler) Register(router gin.IRouter) {
|
||||
func (h *Handler) createProject(c *gin.Context) {
|
||||
userID, ok := auth.CurrentUserID(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "missing current user"})
|
||||
httpx.Error(c, http.StatusUnauthorized, "unauthorized", "未登录或登录已失效")
|
||||
return
|
||||
}
|
||||
var input struct {
|
||||
Name string `json:"name"`
|
||||
Identifier string `json:"identifier"`
|
||||
Icon string `json:"icon"`
|
||||
Background string `json:"background"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
var input CreateProjectRequest
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request"})
|
||||
httpx.Error(c, http.StatusBadRequest, "invalid_request", "请求参数无效")
|
||||
return
|
||||
}
|
||||
project, err := h.service.CreateProjectWithInput(userID, CreateProjectInput{
|
||||
Name: input.Name,
|
||||
Identifier: input.Identifier,
|
||||
Icon: input.Icon,
|
||||
Background: input.Background,
|
||||
Description: input.Description,
|
||||
})
|
||||
project, err := h.service.CreateProjectWithInput(userID, input)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
writeProjectError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, project)
|
||||
c.JSON(http.StatusCreated, projectDTO(*project))
|
||||
}
|
||||
|
||||
func (h *Handler) listProjects(c *gin.Context) {
|
||||
userID, ok := auth.CurrentUserID(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "missing current user"})
|
||||
httpx.Error(c, http.StatusUnauthorized, "unauthorized", "未登录或登录已失效")
|
||||
return
|
||||
}
|
||||
projects, err := h.service.ListProjects(userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to list projects"})
|
||||
httpx.Error(c, http.StatusInternalServerError, "internal_error", "项目列表加载失败")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, projects)
|
||||
}
|
||||
|
||||
func (h *Handler) getProject(c *gin.Context) {
|
||||
userID, ok := auth.CurrentUserID(c)
|
||||
if !ok {
|
||||
httpx.Error(c, http.StatusUnauthorized, "unauthorized", "未登录或登录已失效")
|
||||
return
|
||||
}
|
||||
identity, ok := httpx.IdentityParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
project, err := h.service.GetProject(userID, identity)
|
||||
if err != nil {
|
||||
writeProjectError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, project)
|
||||
}
|
||||
|
||||
func (h *Handler) updateProject(c *gin.Context) {
|
||||
userID, ok := auth.CurrentUserID(c)
|
||||
if !ok {
|
||||
httpx.Error(c, http.StatusUnauthorized, "unauthorized", "未登录或登录已失效")
|
||||
return
|
||||
}
|
||||
identity, ok := httpx.IdentityParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var input UpdateProjectRequest
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
httpx.Error(c, http.StatusBadRequest, "invalid_request", "请求参数无效")
|
||||
return
|
||||
}
|
||||
if err := h.service.UpdateProject(userID, identity, input); err != nil {
|
||||
writeProjectError(c, err)
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func (h *Handler) dashboard(c *gin.Context) {
|
||||
userID, ok := auth.CurrentUserID(c)
|
||||
if !ok {
|
||||
@@ -340,6 +373,19 @@ func parseProjectID(c *gin.Context) (uint, bool) {
|
||||
return uint(projectID), true
|
||||
}
|
||||
|
||||
func writeProjectError(c *gin.Context, err error) {
|
||||
switch {
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
httpx.Error(c, http.StatusNotFound, "not_found", "项目不存在")
|
||||
case errors.Is(err, ErrProjectIdentifierConflict):
|
||||
httpx.Error(c, http.StatusConflict, "conflict", "项目标识已存在")
|
||||
case errors.Is(err, ErrProjectNameRequired), errors.Is(err, ErrProjectIdentifierRequired):
|
||||
httpx.Error(c, http.StatusBadRequest, "invalid_request", "请求参数无效")
|
||||
default:
|
||||
httpx.Error(c, http.StatusInternalServerError, "internal_error", "项目操作失败")
|
||||
}
|
||||
}
|
||||
|
||||
func parseTaskID(c *gin.Context) (uint, bool) {
|
||||
taskID, err := strconv.ParseUint(c.Param("taskID"), 10, 64)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user