package handler import ( "RelationshipManagement-backend/model" "RelationshipManagement-backend/service" "net/http" "strconv" "github.com/gin-gonic/gin" ) // UserHandler 用户处理器 type UserHandler struct { userService *service.UserService } // NewUserHandler 创建用户处理器实例 func NewUserHandler(userService *service.UserService) *UserHandler { return &UserHandler{userService: userService} } // CreateUser 创建用户 // @Summary 创建用户 // @Description 创建新用户 // @Tags 用户 // @Accept json // @Produce json // @Param user body model.User true "用户信息" // @Success 200 {object} map[string]interface{} // @Failure 400 {object} map[string]interface{} // @Router /api/users [post] func (h *UserHandler) CreateUser(c *gin.Context) { var user model.User if err := c.ShouldBindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "message": "参数错误", "error": err.Error(), }) return } if err := h.userService.CreateUser(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "message": "创建用户失败", "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 200, "message": "创建成功", "data": user, }) } // GetUserByID 根据ID获取用户 // @Summary 获取用户 // @Description 根据ID获取用户信息 // @Tags 用户 // @Accept json // @Produce json // @Param id path int true "用户ID" // @Success 200 {object} map[string]interface{} // @Failure 400 {object} map[string]interface{} // @Router /api/users/{id} [get] func (h *UserHandler) GetUserByID(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "message": "无效的用户ID", }) return } user, err := h.userService.GetUserByID(uint(id)) if err != nil { c.JSON(http.StatusNotFound, gin.H{ "code": 404, "message": "用户不存在", "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 200, "message": "获取成功", "data": user, }) } // ListUsers 获取用户列表 // @Summary 获取用户列表 // @Description 分页获取用户列表 // @Tags 用户 // @Accept json // @Produce json // @Param page query int false "页码" default(1) // @Param pageSize query int false "每页数量" default(10) // @Success 200 {object} map[string]interface{} // @Router /api/users [get] func (h *UserHandler) ListUsers(c *gin.Context) { page, _ := strconv.Atoi(c.DefaultQuery("page", "1")) pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10")) users, total, err := h.userService.ListUsers(page, pageSize) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "code": 500, "message": "获取用户列表失败", "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 200, "message": "获取成功", "data": gin.H{ "list": users, "total": total, "page": page, "pageSize": pageSize, }, }) } // UpdateUser 更新用户 // @Summary 更新用户 // @Description 更新用户信息 // @Tags 用户 // @Accept json // @Produce json // @Param id path int true "用户ID" // @Param user body model.User true "用户信息" // @Success 200 {object} map[string]interface{} // @Failure 400 {object} map[string]interface{} // @Router /api/users/{id} [put] func (h *UserHandler) UpdateUser(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "message": "无效的用户ID", }) return } var user model.User if err := c.ShouldBindJSON(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "message": "参数错误", "error": err.Error(), }) return } user.ID = uint(id) if err := h.userService.UpdateUser(&user); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "message": "更新用户失败", "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 200, "message": "更新成功", "data": user, }) } // DeleteUser 删除用户 // @Summary 删除用户 // @Description 根据ID删除用户 // @Tags 用户 // @Accept json // @Produce json // @Param id path int true "用户ID" // @Success 200 {object} map[string]interface{} // @Failure 400 {object} map[string]interface{} // @Router /api/users/{id} [delete] func (h *UserHandler) DeleteUser(c *gin.Context) { idStr := c.Param("id") id, err := strconv.ParseUint(idStr, 10, 32) if err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "message": "无效的用户ID", }) return } if err := h.userService.DeleteUser(uint(id)); err != nil { c.JSON(http.StatusBadRequest, gin.H{ "code": 400, "message": "删除用户失败", "error": err.Error(), }) return } c.JSON(http.StatusOK, gin.H{ "code": 200, "message": "删除成功", }) }