feat: 优化web
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"giter.top/smart/internal/iam/service"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type RoleHandler struct {
|
||||
svc service.RoleService
|
||||
}
|
||||
|
||||
func NewRoleHandler(svc service.RoleService) *RoleHandler {
|
||||
return &RoleHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *RoleHandler) Create(c *gin.Context) {
|
||||
tid := headerTenantID(c)
|
||||
var req service.CreateRoleRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
role, err := h.svc.Create(c.Request.Context(), tid, &req, headerGrantorUserID(c))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, role)
|
||||
}
|
||||
|
||||
func (h *RoleHandler) Update(c *gin.Context) {
|
||||
tid := headerTenantID(c)
|
||||
rid := c.Param("id")
|
||||
if rid == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
var req service.UpdateRoleRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
r, err := h.svc.Update(c.Request.Context(), tid, rid, &req, headerGrantorUserID(c))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, r)
|
||||
}
|
||||
|
||||
func (h *RoleHandler) 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 *RoleHandler) Get(c *gin.Context) {
|
||||
tid := headerTenantID(c)
|
||||
rid := c.Param("id")
|
||||
if rid == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
r, err := h.svc.Get(c.Request.Context(), tid, rid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, r)
|
||||
}
|
||||
|
||||
func (h *RoleHandler) List(c *gin.Context) {
|
||||
tid := headerTenantID(c)
|
||||
name := c.Query("name")
|
||||
code := c.Query("code")
|
||||
page := atoiDef(c.Query("page"), 1)
|
||||
pageSize := atoiDef(c.Query("page_size"), 10)
|
||||
resp, err := h.svc.List(c.Request.Context(), tid, name, code, page, pageSize)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
|
||||
type assignMenusBody struct {
|
||||
MenuIDs []string `json:"menu_ids"`
|
||||
}
|
||||
|
||||
func (h *RoleHandler) AssignMenus(c *gin.Context) {
|
||||
tid := headerTenantID(c)
|
||||
rid := c.Param("id")
|
||||
if rid == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
||||
return
|
||||
}
|
||||
var body assignMenusBody
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := h.svc.AssignMenus(c.Request.Context(), tid, rid, body.MenuIDs, headerGrantorUserID(c)); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
Reference in New Issue
Block a user