| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- package handler
- import (
- "encoding/json"
- "github.com/gin-gonic/gin"
- "spider/internal/model"
- "spider/internal/store"
- )
- // SettingHandler handles system settings.
- type SettingHandler struct {
- store *store.Store
- }
- // GetGrading handles GET /settings/grading
- func (h *SettingHandler) GetGrading(c *gin.Context) {
- cfg, err := h.store.GetGradingConfig()
- if err != nil {
- Fail(c, 500, err.Error())
- return
- }
- OK(c, cfg)
- }
- // UpdateGrading handles PUT /settings/grading
- func (h *SettingHandler) UpdateGrading(c *gin.Context) {
- var cfg model.GradingConfig
- if err := c.ShouldBindJSON(&cfg); err != nil {
- Fail(c, 400, err.Error())
- return
- }
- // Validate: must have at least one level
- if len(cfg.Levels) == 0 {
- Fail(c, 400, "至少需要一个等级")
- return
- }
- if err := h.store.SetSetting("grading", cfg); err != nil {
- Fail(c, 500, err.Error())
- return
- }
- LogAudit(h.store, c, "update", "setting", "grading", nil)
- OK(c, cfg)
- }
- // ResetGrading handles POST /settings/grading/reset
- func (h *SettingHandler) ResetGrading(c *gin.Context) {
- cfg := model.DefaultGradingConfig()
- if err := h.store.SetSetting("grading", cfg); err != nil {
- Fail(c, 500, err.Error())
- return
- }
- LogAudit(h.store, c, "update", "setting", "grading", gin.H{"action": "reset"})
- OK(c, cfg)
- }
- // GetLevelMap handles GET /settings/level-map — returns key→label mapping for frontend display.
- func (h *SettingHandler) GetLevelMap(c *gin.Context) {
- cfg, err := h.store.GetGradingConfig()
- if err != nil {
- Fail(c, 500, err.Error())
- return
- }
- m := make(map[string]map[string]string)
- for _, level := range cfg.Levels {
- m[level.Key] = map[string]string{
- "label": level.Label,
- "color": level.Color,
- "description": level.Description,
- }
- }
- OK(c, m)
- }
- // GetAPIKeys handles GET /settings/api-keys
- func (h *SettingHandler) GetAPIKeys(c *gin.Context) {
- cfg, err := h.store.GetAPIKeysConfig()
- if err != nil {
- Fail(c, 500, err.Error())
- return
- }
- OK(c, cfg)
- }
- // UpdateAPIKeys handles PUT /settings/api-keys
- func (h *SettingHandler) UpdateAPIKeys(c *gin.Context) {
- var cfg model.APIKeysConfig
- if err := c.ShouldBindJSON(&cfg); err != nil {
- Fail(c, 400, err.Error())
- return
- }
- if err := h.store.SetSetting("api_keys", cfg); err != nil {
- Fail(c, 500, err.Error())
- return
- }
- LogAudit(h.store, c, "update", "setting", "api_keys", nil)
- OK(c, cfg)
- }
- // GetAllSettings handles GET /settings — returns all settings keys.
- func (h *SettingHandler) GetAllSettings(c *gin.Context) {
- var settings []model.Setting
- h.store.DB.Find(&settings)
- result := make(map[string]json.RawMessage)
- for _, s := range settings {
- result[s.Key] = json.RawMessage(s.Value)
- }
- OK(c, result)
- }
|