Initial commit: Gitea Project Management System
Features: - Complete project management system with Epic/Story/Task hierarchy - Vue.js 3 + Element Plus frontend with kanban board - Go backend with Gin framework and GORM - OAuth2 integration with Gitea - Docker containerization with MySQL - RESTful API for project, task, and user management - JWT authentication and authorization - Responsive web interface with dashboard
This commit is contained in:
131
internal/api/handlers/users.go
Normal file
131
internal/api/handlers/users.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"giteapm/internal/middleware"
|
||||
"giteapm/internal/models"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func (h *Handlers) ListUsers(c *gin.Context) {
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "20"))
|
||||
offset := (page - 1) * limit
|
||||
|
||||
users, total, err := models.ListUsers(offset, limit)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, ErrorResponse(500, "获取用户列表失败"))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, PaginatedSuccessResponse(users, total, page, limit))
|
||||
}
|
||||
|
||||
func (h *Handlers) GetUser(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse(400, "无效的用户ID"))
|
||||
return
|
||||
}
|
||||
|
||||
user, err := models.GetUserByID(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, ErrorResponse(404, "用户不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, SuccessResponse(user))
|
||||
}
|
||||
|
||||
func (h *Handlers) UpdateUser(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse(400, "无效的用户ID"))
|
||||
return
|
||||
}
|
||||
|
||||
user, err := models.GetUserByID(uint(id))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, ErrorResponse(404, "用户不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
var updateData map[string]interface{}
|
||||
if err := c.ShouldBindJSON(&updateData); err != nil {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse(400, "请求参数无效"))
|
||||
return
|
||||
}
|
||||
|
||||
if fullName, ok := updateData["full_name"].(string); ok {
|
||||
user.FullName = fullName
|
||||
}
|
||||
if email, ok := updateData["email"].(string); ok {
|
||||
user.Email = email
|
||||
}
|
||||
if role, ok := updateData["role"].(string); ok {
|
||||
user.Role = role
|
||||
}
|
||||
|
||||
if err := models.UpdateUser(user); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, ErrorResponse(500, "更新用户失败"))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, SuccessResponse(user))
|
||||
}
|
||||
|
||||
func (h *Handlers) DeleteUser(c *gin.Context) {
|
||||
id, err := strconv.ParseUint(c.Param("id"), 10, 32)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse(400, "无效的用户ID"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.DeleteUser(uint(id)); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, ErrorResponse(500, "删除用户失败"))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, SuccessResponse(gin.H{"message": "删除成功"}))
|
||||
}
|
||||
|
||||
func (h *Handlers) GetCurrentUser(c *gin.Context) {
|
||||
user, err := middleware.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, ErrorResponse(401, "获取当前用户失败"))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, SuccessResponse(user))
|
||||
}
|
||||
|
||||
func (h *Handlers) UpdateCurrentUser(c *gin.Context) {
|
||||
user, err := middleware.GetCurrentUser(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, ErrorResponse(401, "获取当前用户失败"))
|
||||
return
|
||||
}
|
||||
|
||||
var updateData map[string]interface{}
|
||||
if err := c.ShouldBindJSON(&updateData); err != nil {
|
||||
c.JSON(http.StatusBadRequest, ErrorResponse(400, "请求参数无效"))
|
||||
return
|
||||
}
|
||||
|
||||
if fullName, ok := updateData["full_name"].(string); ok {
|
||||
user.FullName = fullName
|
||||
}
|
||||
if email, ok := updateData["email"].(string); ok {
|
||||
user.Email = email
|
||||
}
|
||||
|
||||
if err := models.UpdateUser(user); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, ErrorResponse(500, "更新用户失败"))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, SuccessResponse(user))
|
||||
}
|
Reference in New Issue
Block a user