package handler import ( "net/http" "strconv" "github.com/gin-gonic/gin" "github.com/user-management-system/internal/service" ) // ThemeHandler 主题配置处理器 type ThemeHandler struct { themeService *service.ThemeService } // NewThemeHandler 创建主题配置处理器 func NewThemeHandler(themeService *service.ThemeService) *ThemeHandler { return &ThemeHandler{themeService: themeService} } // CreateTheme 创建主题 // @Summary 创建主题 // @Description 创建新的主题配置 // @Tags 主题管理 // @Accept json // @Produce json // @Security BearerAuth // @Param request body service.CreateThemeRequest true "主题信息" // @Success 201 {object} Response{data=SwaggerTheme} "主题创建成功" // @Failure 400 {object} Response "请求参数错误" // @Failure 401 {object} Response "未认证" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/themes [post] func (h *ThemeHandler) CreateTheme(c *gin.Context) { var req service.CreateThemeRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } theme, err := h.themeService.CreateTheme(c.Request.Context(), &req) if err != nil { handleError(c, err) return } c.JSON(http.StatusCreated, gin.H{ "code": 0, "message": "success", "data": theme, }) } // UpdateTheme 更新主题 // @Summary 更新主题 // @Description 更新指定主题的配置 // @Tags 主题管理 // @Accept json // @Produce json // @Security BearerAuth // @Param id path int true "主题ID" // @Param request body service.UpdateThemeRequest true "更新信息" // @Success 200 {object} Response{data=SwaggerTheme} "主题更新成功" // @Failure 400 {object} Response "请求参数错误" // @Failure 401 {object} Response "未认证" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/themes/{id} [put] func (h *ThemeHandler) UpdateTheme(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 theme id"}) return } var req service.UpdateThemeRequest if err := c.ShouldBindJSON(&req); err != nil { c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()}) return } theme, err := h.themeService.UpdateTheme(c.Request.Context(), id, &req) if err != nil { handleError(c, err) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": theme, }) } // DeleteTheme 删除主题 // @Summary 删除主题 // @Description 删除指定的主题 // @Tags 主题管理 // @Produce json // @Security BearerAuth // @Param id path int true "主题ID" // @Success 200 {object} Response "主题删除成功" // @Failure 400 {object} Response "请求参数错误" // @Failure 401 {object} Response "未认证" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/themes/{id} [delete] func (h *ThemeHandler) DeleteTheme(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 theme id"}) return } if err := h.themeService.DeleteTheme(c.Request.Context(), id); err != nil { handleError(c, err) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "theme deleted", }) } // GetTheme 获取主题 // @Summary 获取主题 // @Description 根据ID获取主题详情 // @Tags 主题管理 // @Produce json // @Security BearerAuth // @Param id path int true "主题ID" // @Success 200 {object} Response{data=SwaggerTheme} "主题详情" // @Failure 400 {object} Response "请求参数错误" // @Failure 401 {object} Response "未认证" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/themes/{id} [get] func (h *ThemeHandler) GetTheme(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 theme id"}) return } theme, err := h.themeService.GetTheme(c.Request.Context(), id) if err != nil { handleError(c, err) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": theme, }) } // ListThemes 获取主题列表 // @Summary 获取主题列表 // @Description 获取所有已启用的主题 // @Tags 主题管理 // @Produce json // @Security BearerAuth // @Success 200 {object} Response{data=[]SwaggerTheme} "主题列表" // @Failure 401 {object} Response "未认证" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/themes [get] func (h *ThemeHandler) ListThemes(c *gin.Context) { themes, err := h.themeService.ListThemes(c.Request.Context()) if err != nil { handleError(c, err) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": themes, }) } // ListAllThemes 获取所有主题(包括禁用的) // @Summary 获取所有主题 // @Description 获取所有主题(包括已禁用的) // @Tags 主题管理 // @Produce json // @Security BearerAuth // @Success 200 {object} Response{data=[]SwaggerTheme} "主题列表" // @Failure 401 {object} Response "未认证" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/themes [get] func (h *ThemeHandler) ListAllThemes(c *gin.Context) { themes, err := h.themeService.ListAllThemes(c.Request.Context()) if err != nil { handleError(c, err) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": themes, }) } // GetDefaultTheme 获取默认主题 // @Summary 获取默认主题 // @Description 获取系统默认主题 // @Tags 主题管理 // @Produce json // @Security BearerAuth // @Success 200 {object} Response{data=SwaggerTheme} "默认主题" // @Failure 401 {object} Response "未认证" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/themes/default [get] func (h *ThemeHandler) GetDefaultTheme(c *gin.Context) { theme, err := h.themeService.GetDefaultTheme(c.Request.Context()) if err != nil { handleError(c, err) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": theme, }) } // SetDefaultTheme 设置默认主题 // @Summary 设置默认主题 // @Description 将指定主题设为系统默认主题 // @Tags 主题管理 // @Produce json // @Security BearerAuth // @Param id path int true "主题ID" // @Success 200 {object} Response "设置成功" // @Failure 400 {object} Response "请求参数错误" // @Failure 401 {object} Response "未认证" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/themes/default/{id} [put] func (h *ThemeHandler) SetDefaultTheme(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 theme id"}) return } if err := h.themeService.SetDefaultTheme(c.Request.Context(), id); err != nil { handleError(c, err) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "default theme set", }) } // GetActiveTheme 获取当前生效的主题 // @Summary 获取当前生效的主题 // @Description 获取当前系统正在使用的主题(公开接口) // @Tags 主题管理 // @Produce json // @Success 200 {object} Response{data=SwaggerTheme} "当前生效主题" // @Failure 500 {object} Response "服务器错误" // @Router /api/v1/theme/active [get] func (h *ThemeHandler) GetActiveTheme(c *gin.Context) { theme, err := h.themeService.GetActiveTheme(c.Request.Context()) if err != nil { handleError(c, err) return } c.JSON(http.StatusOK, gin.H{ "code": 0, "message": "success", "data": theme, }) }