| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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)
- }
- // 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)
- }
|