39 lines
903 B
Go
39 lines
903 B
Go
package categories
|
|
|
|
import (
|
|
"gis/app"
|
|
"gis/server/httputil"
|
|
"net/http"
|
|
)
|
|
|
|
type CreateCategoryRequest struct {
|
|
Name string `json:"name" validate:"required,max=255"`
|
|
Description string `json:"description" validate:"required"`
|
|
}
|
|
|
|
func createCategoryRoute(application *app.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
req, err := httputil.DecodeJSON[CreateCategoryRequest](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
|
|
}
|
|
|
|
_, err = application.Db.Exec(application.Ctx,
|
|
"INSERT INTO categories (name, description) VALUES ($1, $2)",
|
|
req.Name, req.Description,
|
|
)
|
|
if err != nil {
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.WriteHeader(http.StatusCreated)
|
|
}
|
|
}
|