45 lines
1.0 KiB
Go
45 lines
1.0 KiB
Go
package categories
|
|
|
|
import (
|
|
"gis/app"
|
|
"gis/server/httputil"
|
|
"net/http"
|
|
)
|
|
|
|
type UpdateCategoryRequest struct {
|
|
Name string `json:"name" validate:"required,max=255"`
|
|
Description string `json:"description" validate:"required"`
|
|
}
|
|
|
|
func updateCategoryRoute(application *app.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
id := r.PathValue("id")
|
|
|
|
req, err := httputil.DecodeJSON[UpdateCategoryRequest](w, r)
|
|
if err != nil {
|
|
http.Error(w, "Invalid request", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
if err := application.Validator.Struct(req); err != nil {
|
|
httputil.WriteValidationErrors(w, err)
|
|
return
|
|
}
|
|
|
|
tag, err := application.Db.Exec(application.Ctx,
|
|
"UPDATE categories SET name=$1, description=$2, updated_at=now() WHERE id=$3",
|
|
req.Name, req.Description, id,
|
|
)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
w.WriteHeader(http.StatusNotFound)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
}
|