Files
user-system/internal/api/handler/custom_field_handler.go
long-agent 0564bfd9ad docs: add Swagger annotations to 13 API handlers
Added @Summary, @Description, @Tags, @Param, @Success, @Failure,
@Router annotations to all major handler endpoints for OpenAPI/Swagger
auto-generation. Covers 86 annotations across:

- auth_handler.go (25): all auth endpoints
- user_handler.go (14): CRUD + roles + admin management
- device_handler.go (13): device CRUD + trust management
- role_handler.go (8): role CRUD + permissions
- custom_field_handler.go (7): field CRUD + user values
- permission_handler.go (7): permission CRUD + tree
- log_handler.go (3): login/operation logs
- captcha_handler.go (3): generate/verify
- stats_handler.go (2): dashboard + user stats
- avatar_handler.go (1): upload avatar
- totp_handler.go (1): totp status
- password_reset_handler.go (1): forgot password

Partially addresses P2: missing Swagger annotations
(PRODUCTION_GAP_ANALYSIS_2026-04-08)
2026-04-11 21:23:52 +08:00

242 lines
6.7 KiB
Go

package handler
import (
"net/http"
"strconv"
"github.com/gin-gonic/gin"
"github.com/user-management-system/internal/service"
)
// CustomFieldHandler 自定义字段处理器
type CustomFieldHandler struct {
customFieldService *service.CustomFieldService
}
// NewCustomFieldHandler 创建自定义字段处理器
func NewCustomFieldHandler(customFieldService *service.CustomFieldService) *CustomFieldHandler {
return &CustomFieldHandler{customFieldService: customFieldService}
}
// CreateField 创建自定义字段
// @Summary 创建自定义字段
// @Description 创建新的自定义字段定义(仅管理员)
// @Tags 自定义字段
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body service.CreateFieldRequest true "字段定义"
// @Success 201 {object} Response{data=domain.CustomField} "创建成功"
// @Failure 400 {object} Response "请求参数错误"
// @Failure 403 {object} Response "无权限"
// @Router /api/v1/fields [post]
func (h *CustomFieldHandler) CreateField(c *gin.Context) {
var req service.CreateFieldRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
field, err := h.customFieldService.CreateField(c.Request.Context(), &req)
if err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusCreated, gin.H{
"code": 0,
"message": "success",
"data": field,
})
}
// UpdateField 更新自定义字段
// @Summary 更新自定义字段
// @Description 更新自定义字段定义(仅管理员)
// @Tags 自定义字段
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param id path int true "字段ID"
// @Param request body service.UpdateFieldRequest true "更新信息"
// @Success 200 {object} Response{data=domain.CustomField} "更新成功"
// @Failure 400 {object} Response "请求参数错误"
// @Failure 403 {object} Response "无权限"
// @Failure 404 {object} Response "字段不存在"
// @Router /api/v1/fields/{id} [put]
func (h *CustomFieldHandler) UpdateField(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"})
return
}
var req service.UpdateFieldRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
field, err := h.customFieldService.UpdateField(c.Request.Context(), id, &req)
if err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": field,
})
}
// DeleteField 删除自定义字段
// @Summary 删除自定义字段
// @Description 删除自定义字段定义(仅管理员)
// @Tags 自定义字段
// @Produce json
// @Security BearerAuth
// @Param id path int true "字段ID"
// @Success 200 {object} Response "删除成功"
// @Failure 403 {object} Response "无权限"
// @Failure 404 {object} Response "字段不存在"
// @Router /api/v1/fields/{id} [delete]
func (h *CustomFieldHandler) DeleteField(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"})
return
}
if err := h.customFieldService.DeleteField(c.Request.Context(), id); err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "field deleted",
})
}
// GetField 获取自定义字段
// @Summary 获取自定义字段详情
// @Description 根据ID获取自定义字段定义
// @Tags 自定义字段
// @Produce json
// @Security BearerAuth
// @Param id path int true "字段ID"
// @Success 200 {object} Response{data=domain.CustomField} "字段信息"
// @Failure 404 {object} Response "字段不存在"
// @Router /api/v1/fields/{id} [get]
func (h *CustomFieldHandler) GetField(c *gin.Context) {
id, err := strconv.ParseInt(c.Param("id"), 10, 64)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid field id"})
return
}
field, err := h.customFieldService.GetField(c.Request.Context(), id)
if err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": field,
})
}
// ListFields 获取所有自定义字段
// @Summary 获取自定义字段列表
// @Description 获取所有自定义字段定义列表
// @Tags 自定义字段
// @Produce json
// @Security BearerAuth
// @Success 200 {object} Response{data=[]domain.CustomField} "字段列表"
// @Router /api/v1/fields [get]
func (h *CustomFieldHandler) ListFields(c *gin.Context) {
fields, err := h.customFieldService.ListFields(c.Request.Context())
if err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": fields,
})
}
// SetUserFieldValues 设置用户自定义字段值
// @Summary 设置用户自定义字段值
// @Description 设置当前用户的自定义字段值
// @Tags 自定义字段
// @Accept json
// @Produce json
// @Security BearerAuth
// @Param request body SetUserFieldValuesRequest true "字段值"
// @Success 200 {object} Response "设置成功"
// @Failure 400 {object} Response "请求参数错误"
// @Failure 401 {object} Response "未认证"
// @Router /api/v1/users/me/fields [put]
func (h *CustomFieldHandler) SetUserFieldValues(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
var req struct {
Values map[string]string `json:"values" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
if err := h.customFieldService.BatchSetUserFieldValues(c.Request.Context(), userID, req.Values); err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "field values set",
})
}
// GetUserFieldValues 获取用户自定义字段值
// @Summary 获取用户自定义字段值
// @Description 获取当前用户的自定义字段值
// @Tags 自定义字段
// @Produce json
// @Security BearerAuth
// @Success 200 {object} Response{data=map} "字段值"
// @Failure 401 {object} Response "未认证"
// @Router /api/v1/users/me/fields [get]
func (h *CustomFieldHandler) GetUserFieldValues(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
values, err := h.customFieldService.GetUserFieldValues(c.Request.Context(), userID)
if err != nil {
handleError(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"code": 0,
"message": "success",
"data": values,
})
}