package handler import ( "database/sql" "encoding/json" "fmt" "net/http" c "npm/internal/api/context" h "npm/internal/api/http" "npm/internal/api/middleware" "npm/internal/entity/setting" "github.com/go-chi/chi/v5" ) // GetSettings will return a list of Settings // Route: GET /settings func GetSettings() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { pageInfo, err := getPageInfoFromRequest(r) if err != nil { h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil) return } settings, err := setting.List(pageInfo, middleware.GetFiltersFromContext(r)) if err != nil { h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil) } else { h.ResultResponseJSON(w, r, http.StatusOK, settings) } } } // GetSetting will return a single Setting // Route: GET /settings/{name} func GetSetting() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { name := chi.URLParam(r, "name") item, err := setting.GetByName(name) switch err { case sql.ErrNoRows: h.NotFound(w, r) case nil: h.ResultResponseJSON(w, r, http.StatusOK, item) default: h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil) } } } // CreateSetting will create a Setting // Route: POST /settings func CreateSetting() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte) var newSetting setting.Model err := json.Unmarshal(bodyBytes, &newSetting) if err != nil { h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil) return } if err = newSetting.Save(); err != nil { h.ResultErrorJSON(w, r, http.StatusBadRequest, fmt.Sprintf("Unable to save Setting: %s", err.Error()), nil) return } h.ResultResponseJSON(w, r, http.StatusOK, newSetting) } } // UpdateSetting updates a setting // Route: PUT /settings/{name} func UpdateSetting() func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { settingName := chi.URLParam(r, "name") setting, err := setting.GetByName(settingName) switch err { case sql.ErrNoRows: h.NotFound(w, r) case nil: bodyBytes, _ := r.Context().Value(c.BodyCtxKey).([]byte) err := json.Unmarshal(bodyBytes, &setting) if err != nil { h.ResultErrorJSON(w, r, http.StatusBadRequest, h.ErrInvalidPayload.Error(), nil) return } if err = setting.Save(); err != nil { h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil) return } h.ResultResponseJSON(w, r, http.StatusOK, setting) default: h.ResultErrorJSON(w, r, http.StatusBadRequest, err.Error(), nil) } } }