99 lines
2.4 KiB
Go
99 lines
2.4 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"giter.top/smart/internal/iam/service"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type TenantHandler struct {
|
|
svc service.TenantService
|
|
}
|
|
|
|
func NewTenantHandler(svc service.TenantService) *TenantHandler {
|
|
return &TenantHandler{svc: svc}
|
|
}
|
|
|
|
func (h *TenantHandler) Create(c *gin.Context) {
|
|
var req service.CreateTenantRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
t, err := h.svc.Create(c.Request.Context(), &req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusCreated, t)
|
|
}
|
|
|
|
func (h *TenantHandler) Update(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
var req service.UpdateTenantRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
t, err := h.svc.Update(c.Request.Context(), id, &req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, t)
|
|
}
|
|
|
|
func (h *TenantHandler) Delete(c *gin.Context) {
|
|
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(), ids); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.Status(http.StatusNoContent)
|
|
}
|
|
|
|
func (h *TenantHandler) Get(c *gin.Context) {
|
|
id := c.Param("id")
|
|
if id == "" {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid id"})
|
|
return
|
|
}
|
|
t, err := h.svc.Get(c.Request.Context(), id)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, t)
|
|
}
|
|
|
|
func (h *TenantHandler) List(c *gin.Context) {
|
|
name := c.Query("name")
|
|
code := c.Query("code")
|
|
var status *int16
|
|
if s := c.Query("status"); s != "" {
|
|
v64, err := strconv.ParseInt(s, 10, 16)
|
|
if err == nil {
|
|
v := int16(v64)
|
|
status = &v
|
|
}
|
|
}
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "10"))
|
|
resp, err := h.svc.List(c.Request.Context(), name, code, status, page, pageSize)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, resp)
|
|
}
|