feat: vani of the day edit
This commit is contained in:
@@ -58,6 +58,36 @@ func (h *VaniHandler) Set(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
}
|
||||
|
||||
type setVaniByURLReq struct {
|
||||
Day string `json:"day"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// SetByURL — POST /admin/vani/by-url. Pins whatever a link points at.
|
||||
func (h *VaniHandler) SetByURL(w http.ResponseWriter, r *http.Request) {
|
||||
var req setVaniByURLReq
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
response.Error(w, http.StatusBadRequest, "invalid JSON body")
|
||||
return
|
||||
}
|
||||
day, err := parseVaniDay(req.Day)
|
||||
if err != nil {
|
||||
response.Error(w, http.StatusBadRequest, "day must be YYYY-MM-DD")
|
||||
return
|
||||
}
|
||||
pick, err := h.svc.SetByURL(r.Context(), req.URL, day)
|
||||
if err != nil {
|
||||
writeVaniError(w, err)
|
||||
return
|
||||
}
|
||||
response.JSON(w, http.StatusOK, map[string]any{
|
||||
"day": pick.Day.Format("2006-01-02"),
|
||||
"kind": pick.Kind,
|
||||
"ref_id": pick.RefID,
|
||||
"payload": pick.Payload,
|
||||
})
|
||||
}
|
||||
|
||||
// Clear — DELETE /admin/vani/{day}. Hands the day back to the random picker.
|
||||
func (h *VaniHandler) Clear(w http.ResponseWriter, r *http.Request) {
|
||||
day, err := parseVaniDay(chi.URLParam(r, "day"))
|
||||
@@ -103,7 +133,9 @@ func parseVaniDay(s string) (time.Time, error) {
|
||||
|
||||
func writeVaniError(w http.ResponseWriter, err error) {
|
||||
switch {
|
||||
case errors.Is(err, repository.ErrVaniRefInvalid), errors.Is(err, service.ErrVaniBadKind):
|
||||
case errors.Is(err, repository.ErrVaniRefInvalid),
|
||||
errors.Is(err, service.ErrVaniBadKind),
|
||||
errors.Is(err, service.ErrVaniBadURL):
|
||||
response.Error(w, http.StatusBadRequest, err.Error())
|
||||
case errors.Is(err, repository.ErrVaniRefNotFound):
|
||||
response.Error(w, http.StatusNotFound, err.Error())
|
||||
|
||||
Reference in New Issue
Block a user