wz-golang-server/server/modules/user/controller/platform_user_controller.go

159 lines
4.5 KiB
Go

// Package controller 控制器层
package controller
import (
"strconv"
"server/common"
"server/modules/user/dto"
"server/modules/user/service"
"github.com/gin-gonic/gin"
)
type PlatformUserController struct {
service *service.PlatformUserService
}
func NewPlatformUserController() *PlatformUserController {
return &PlatformUserController{service: service.NewPlatformUserService()}
}
func (ctrl *PlatformUserController) RegisterRoutes(r *gin.RouterGroup) {
group := r.Group("/platform-users")
group.GET("", ctrl.List)
group.GET("/:id", ctrl.GetByID)
group.POST("", ctrl.Create)
group.PUT("/:id", ctrl.Update)
group.PATCH("/:id", ctrl.UpdateFields)
group.DELETE("/:id", ctrl.Delete)
}
// List 获取平台用户列表
// @Summary 获取平台用户列表
// @Tags 平台用户
// @Param page query int false "页码"
// @Param size query int false "每页数量"
// @Success 200 {object} common.Response
// @Router /platform-users [get]
func (ctrl *PlatformUserController) List(c *gin.Context) {
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
size, _ := strconv.Atoi(c.DefaultQuery("size", "10"))
items, total, err := ctrl.service.List(page, size)
if err != nil {
common.Error(c, 500, err.Error())
return
}
common.SuccessPage(c, items, total, page, size)
}
// GetByID 获取平台用户详情
// @Summary 获取平台用户详情
// @Tags 平台用户
// @Param id path int true "ID"
// @Success 200 {object} common.Response
// @Router /platform-users/{id} [get]
func (ctrl *PlatformUserController) GetByID(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
common.Error(c, 400, "ID格式错误")
return
}
item, err := ctrl.service.GetByID(id)
if err != nil {
common.Error(c, 404, "未找到")
return
}
common.Success(c, item)
}
// Create 创建平台用户
// @Summary 创建平台用户
// @Tags 平台用户
// @Param item body dto.CreatePlatformUserRequest true "平台用户信息"
// @Success 200 {object} common.Response
// @Router /platform-users [post]
func (ctrl *PlatformUserController) Create(c *gin.Context) {
var req dto.CreatePlatformUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
common.Error(c, 400, "参数错误")
return
}
item, err := ctrl.service.Create(&req)
if err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, item)
}
// Update 更新平台用户
// @Summary 更新平台用户
// @Tags 平台用户
// @Param id path int true "ID"
// @Param item body dto.UpdatePlatformUserRequest true "平台用户信息"
// @Success 200 {object} common.Response
// @Router /platform-users/{id} [put]
func (ctrl *PlatformUserController) Update(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
common.Error(c, 400, "ID格式错误")
return
}
var req dto.UpdatePlatformUserRequest
if err := c.ShouldBindJSON(&req); err != nil {
common.Error(c, 400, "参数错误")
return
}
item, err := ctrl.service.Update(id, &req)
if err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, item)
}
// UpdateFields 动态字段更新
// @Summary 动态字段更新
// @Tags 平台用户
// @Param id path int true "ID"
// @Param fields body map[string]interface{} true "要更新的字段"
// @Success 200 {object} common.Response
// @Router /platform-users/{id} [patch]
func (ctrl *PlatformUserController) UpdateFields(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
common.Error(c, 400, "ID格式错误")
return
}
var fields map[string]interface{}
if err := c.ShouldBindJSON(&fields); err != nil {
common.Error(c, 400, "参数错误")
return
}
if err := ctrl.service.UpdateFields(id, fields); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, nil)
}
// Delete 删除平台用户
// @Summary 删除平台用户
// @Tags 平台用户
// @Param id path int true "ID"
// @Success 200 {object} common.Response
// @Router /platform-users/{id} [delete]
func (ctrl *PlatformUserController) Delete(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
common.Error(c, 400, "ID格式错误")
return
}
if err := ctrl.service.Delete(id); err != nil {
common.Error(c, 500, err.Error())
return
}
common.Success(c, nil)
}