heritage-api/handler/org-handler.go

89 lines
2.2 KiB
Go
Raw Normal View History

2026-03-13 08:35:54 +00:00
package handler
import (
"net/http"
"github.com/gin-gonic/gin"
"myschools.me/heritage/heritage-api/service"
)
func OrgCreate(c *gin.Context) {
2026-03-18 09:18:06 +00:00
var req service.OrgCreateRequest
2026-03-13 08:35:54 +00:00
if err := c.ShouldBindJSON(&req); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
return
}
2026-03-18 09:18:06 +00:00
o, err := service.OrgCreate(req)
2026-03-13 08:35:54 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
return
}
2026-03-18 09:18:06 +00:00
c.JSON(http.StatusOK, service.OrgResponse{Org: o})
2026-03-13 08:35:54 +00:00
}
func OrgUpdate(c *gin.Context) {
id := c.Param("id")
2026-03-18 09:18:06 +00:00
var req service.OrgUpdateRequest
2026-03-13 08:35:54 +00:00
if err := c.ShouldBindJSON(&req); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
return
}
2026-03-18 09:18:06 +00:00
o, err := service.OrgUpdate(id, req)
2026-03-13 08:35:54 +00:00
if err != nil {
if err == service.ErrNotFound {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
return
}
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
return
}
2026-03-18 09:18:06 +00:00
c.JSON(http.StatusOK, service.OrgResponse{Org: o})
2026-03-13 08:35:54 +00:00
}
func OrgDelete(c *gin.Context) {
id := c.Param("id")
2026-03-18 09:18:06 +00:00
if err := service.OrgDelete(id); err != nil {
2026-03-13 08:35:54 +00:00
if err == service.ErrNotFound {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
return
}
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "删除失败"})
return
}
c.JSON(http.StatusOK, gin.H{"data": "ok"})
}
2026-03-18 09:18:06 +00:00
func OrgDetail(c *gin.Context) {
2026-03-13 08:35:54 +00:00
id := c.Param("id")
2026-03-18 09:18:06 +00:00
o, err := service.OrgDetail(id)
2026-03-13 08:35:54 +00:00
if err != nil {
if err == service.ErrNotFound {
c.AbortWithStatusJSON(http.StatusNotFound, gin.H{"data": "不存在"})
return
}
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "查询失败"})
return
}
2026-03-18 09:18:06 +00:00
c.JSON(http.StatusOK, service.OrgResponse{Org: o})
2026-03-13 08:35:54 +00:00
}
func OrgList(c *gin.Context) {
page, size := pageAndSize(c)
parentID := c.Query("parentId")
var pid *string
if parentID != "" {
pid = &parentID
}
2026-03-18 09:18:06 +00:00
items, total, err := service.OrgPage(pid, page, size)
2026-03-13 08:35:54 +00:00
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"data": "查询失败"})
return
}
2026-03-18 09:18:06 +00:00
c.JSON(http.StatusOK, service.OrgListResponse{
Items: items,
Total: total,
Page: page,
Size: size,
2026-03-13 08:35:54 +00:00
})
}