159 lines
4.2 KiB
Go
159 lines
4.2 KiB
Go
// Package controller 控制器层
|
|
package controller
|
|
|
|
import (
|
|
"strconv"
|
|
|
|
"server/common"
|
|
"server/modules/user/dto"
|
|
"server/modules/user/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type UserController struct {
|
|
service *service.UserService
|
|
}
|
|
|
|
func NewUserController() *UserController {
|
|
return &UserController{service: service.NewUserService()}
|
|
}
|
|
|
|
func (ctrl *UserController) RegisterRoutes(r *gin.RouterGroup) {
|
|
group := r.Group("/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 /users [get]
|
|
func (ctrl *UserController) 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 /users/{id} [get]
|
|
func (ctrl *UserController) 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.CreateUserRequest true "用户信息"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /users [post]
|
|
func (ctrl *UserController) Create(c *gin.Context) {
|
|
var req dto.CreateUserRequest
|
|
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.UpdateUserRequest true "用户信息"
|
|
// @Success 200 {object} common.Response
|
|
// @Router /users/{id} [put]
|
|
func (ctrl *UserController) 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.UpdateUserRequest
|
|
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 /users/{id} [patch]
|
|
func (ctrl *UserController) 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 /users/{id} [delete]
|
|
func (ctrl *UserController) 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)
|
|
}
|