96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"giter.top/smart/internal/iam/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type DeptHandler struct {
|
|
svc service.DeptService
|
|
}
|
|
|
|
func NewDeptHandler(svc service.DeptService) *DeptHandler {
|
|
return &DeptHandler{svc: svc}
|
|
}
|
|
|
|
func (h *DeptHandler) Tree(c *gin.Context) {
|
|
tid := headerTenantID(c)
|
|
keyword := c.Query("keyword")
|
|
var leaderID *string
|
|
if s := c.Query("leader_id"); s != "" {
|
|
leaderID = &s
|
|
}
|
|
tree, err := h.svc.Tree(c.Request.Context(), tid, keyword, leaderID)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, tree)
|
|
}
|
|
|
|
func (h *DeptHandler) Create(c *gin.Context) {
|
|
tid := headerTenantID(c)
|
|
var req service.CreateDeptRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
d, err := h.svc.Create(c.Request.Context(), tid, &req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, d)
|
|
}
|
|
|
|
func (h *DeptHandler) Update(c *gin.Context) {
|
|
tid := headerTenantID(c)
|
|
id := c.Param("id")
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
var req service.UpdateDeptRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
d, err := h.svc.Update(c.Request.Context(), tid, id, &req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, d)
|
|
}
|
|
|
|
func (h *DeptHandler) Delete(c *gin.Context) {
|
|
tid := headerTenantID(c)
|
|
var ids []string
|
|
if err := c.ShouldBindJSON(&ids); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
if err := h.svc.Delete(c.Request.Context(), tid, ids); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *DeptHandler) Get(c *gin.Context) {
|
|
tid := headerTenantID(c)
|
|
id := c.Param("id")
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
d, err := h.svc.Get(c.Request.Context(), tid, id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, d)
|
|
}
|