130 lines
3.1 KiB
Go
130 lines
3.1 KiB
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"myschools.me/heritage/heritage-api/service"
|
|
)
|
|
|
|
// UserCreate 创建用户
|
|
func UserCreate(c *gin.Context) {
|
|
var req service.UserCreateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
|
|
user, err := service.UserCreate(req)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, service.UserResponse{User: user})
|
|
}
|
|
|
|
// UserUpdate 更新用户
|
|
func UserUpdate(c *gin.Context) {
|
|
userID := c.Param("id")
|
|
if userID == "" {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "用户ID不能为空"})
|
|
return
|
|
}
|
|
|
|
var req service.UserUpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
|
|
user, err := service.UserUpdate(userID, req)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, service.UserResponse{User: user})
|
|
}
|
|
|
|
// UserDelete 删除用户
|
|
func UserDelete(c *gin.Context) {
|
|
userID := c.Param("id")
|
|
if userID == "" {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "用户ID不能为空"})
|
|
return
|
|
}
|
|
|
|
success, err := service.UserDelete(userID)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": success})
|
|
}
|
|
|
|
// UserGet 获取用户
|
|
func UserGet(c *gin.Context) {
|
|
userID := c.Param("id")
|
|
if userID == "" {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "用户ID不能为空"})
|
|
return
|
|
}
|
|
|
|
user, err := service.UserGet(userID)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, service.UserResponse{User: user})
|
|
}
|
|
|
|
// UserList 获取用户列表
|
|
func UserList(c *gin.Context) {
|
|
// 获取分页参数
|
|
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
|
pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
|
|
|
|
// 确保分页参数有效
|
|
if page < 1 {
|
|
page = 1
|
|
}
|
|
if pageSize < 1 || pageSize > 100 {
|
|
pageSize = 10
|
|
}
|
|
|
|
users, total, err := service.UserList(page, pageSize)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, service.UserListResponse{Users: users, Total: total})
|
|
}
|
|
|
|
// UserPasswordUpdate 更新用户密码
|
|
func UserPasswordUpdate(c *gin.Context) {
|
|
userID := c.Param("id")
|
|
if userID == "" {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "用户ID不能为空"})
|
|
return
|
|
}
|
|
|
|
var req service.UserPasswordUpdateRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": "参数错误"})
|
|
return
|
|
}
|
|
|
|
err := service.UserPasswordUpdate(userID, req)
|
|
if err != nil {
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{"data": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"data": "密码更新成功"})
|
|
}
|