130 lines
3.1 KiB
Go
130 lines
3.1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/niangaodev/art-code/models"
|
|
"github.com/niangaodev/art-code/repositories"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
// GetUsers 获取所有用户
|
|
func AdminGetUsers(c *gin.Context) {
|
|
users, err := repositories.GetUsers()
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get users"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, repositories.BuildUsersResponse(users))
|
|
}
|
|
|
|
// GetUser 获取单个用户
|
|
func AdminGetUser(c *gin.Context) {
|
|
idStr := c.Param("id")
|
|
id, err := strconv.ParseUint(idStr, 10, 32)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
|
|
return
|
|
}
|
|
|
|
user, err := repositories.GetUserByID(uint(id))
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get user"})
|
|
return
|
|
}
|
|
|
|
if user == nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
|
return
|
|
}
|
|
|
|
// 构建响应
|
|
response := repositories.BuildUserResponse(user)
|
|
|
|
// 设置响应头
|
|
c.Header("Content-Type", "application/json; charset=utf-8")
|
|
|
|
// 返回JSON响应
|
|
c.JSON(http.StatusOK, response)
|
|
}
|
|
|
|
// CreateUser 创建用户
|
|
func AdminCreateUser(c *gin.Context) {
|
|
var user models.User
|
|
if err := c.ShouldBindJSON(&user); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
// 设置默认密码并使用bcrypt哈希
|
|
defaultPassword := "admin123"
|
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(defaultPassword), bcrypt.DefaultCost)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to hash password"})
|
|
return
|
|
}
|
|
user.PasswordHash = string(hashedPassword)
|
|
|
|
// 创建用户
|
|
if err := repositories.CreateUser(&user); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "User created successfully"})
|
|
}
|
|
|
|
// UpdateUser 更新用户
|
|
func AdminUpdateUser(c *gin.Context) {
|
|
userID := c.Param("id")
|
|
var user models.User
|
|
if err := c.ShouldBindJSON(&user); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request"})
|
|
return
|
|
}
|
|
|
|
// 转换用户ID为uint
|
|
var idUint uint
|
|
_, err := fmt.Sscanf(userID, "%d", &idUint)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
|
|
return
|
|
}
|
|
|
|
// 设置用户ID
|
|
user.ID = idUint
|
|
|
|
// 更新用户
|
|
if err := repositories.UpdateUser(&user); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "User updated successfully"})
|
|
}
|
|
|
|
// DeleteUser 删除用户
|
|
func AdminDeleteUser(c *gin.Context) {
|
|
userID := c.Param("id")
|
|
|
|
// 转换用户ID为uint
|
|
var idUint uint
|
|
_, err := fmt.Sscanf(userID, "%d", &idUint)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid user ID"})
|
|
return
|
|
}
|
|
|
|
// 删除用户
|
|
if err := repositories.DeleteUser(idUint); err != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete user"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "User deleted successfully"})
|
|
}
|