Files
Gardomatic/internal/api/species_categories.go
T
kleiax 904d14b64c
CI / test (push) Canceled after 0s
Initial commit
2026-09-12 22:22:17 +02:00

142 lines
3.8 KiB
Go

package api
import (
"fmt"
"net/http"
"strings"
"gardomatic.kleiax.de/internal/platform/validate"
"gardomatic.kleiax.de/internal/storage"
)
type speciesCategoryInput struct {
Name *string `json:"name"`
SortOrder *int `json:"sort_order"`
Active *bool `json:"active"`
Lifecycle *string `json:"lifecycle"`
}
func (input speciesCategoryInput) apply(category *storage.SpeciesCategory) {
if input.Name != nil {
category.Name = strings.TrimSpace(*input.Name)
}
if input.SortOrder != nil {
category.SortOrder = *input.SortOrder
}
if input.Active != nil {
category.Active = *input.Active
}
if input.Lifecycle != nil {
value := strings.TrimSpace(*input.Lifecycle)
if value == "" {
category.Lifecycle = nil
} else {
category.Lifecycle = &value
}
}
}
func (app *application) listSpeciesCategoriesHandler(w http.ResponseWriter, r *http.Request) {
categories, err := app.models.SpeciesCategories.GetAll()
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
if err := app.writeJSON(w, http.StatusOK, envelope{"categories": categories}, nil); err != nil {
app.serverErrorResponse(w, r, err)
}
}
func (app *application) listAdminSpeciesCategoriesHandler(w http.ResponseWriter, r *http.Request) {
categories, err := app.models.SpeciesCategories.GetAll()
if err != nil {
app.serverErrorResponse(w, r, err)
return
}
if err := app.writeJSON(w, http.StatusOK, envelope{"categories": categories}, nil); err != nil {
app.serverErrorResponse(w, r, err)
}
}
func (app *application) createAdminSpeciesCategoryHandler(w http.ResponseWriter, r *http.Request) {
var input speciesCategoryInput
if err := app.readJSON(w, r, &input); err != nil {
app.badRequestResponse(w, r, err)
return
}
category := storage.SpeciesCategory{Active: true}
input.apply(&category)
if !app.validateSpeciesCategory(w, r, category) {
return
}
category, err := app.models.SpeciesCategories.Insert(category)
if err != nil {
app.respondToEntityModelError(w, r, err)
return
}
headers := make(http.Header)
headers.Set("Location", fmt.Sprintf("/v1/admin/species-categories/%d", category.ID))
if err := app.writeJSON(w, http.StatusCreated, envelope{"category": category}, headers); err != nil {
app.serverErrorResponse(w, r, err)
}
}
func (app *application) updateAdminSpeciesCategoryHandler(w http.ResponseWriter, r *http.Request) {
id, err := app.readIDParam(r)
if err != nil {
app.notFoundResponse(w, r)
return
}
category, err := app.models.SpeciesCategories.Get(id)
if err != nil {
app.respondToEntityModelError(w, r, err)
return
}
var input speciesCategoryInput
if err := app.readJSON(w, r, &input); err != nil {
app.badRequestResponse(w, r, err)
return
}
input.apply(&category)
if !app.validateSpeciesCategory(w, r, category) {
return
}
category, err = app.models.SpeciesCategories.Update(category)
if err != nil {
app.respondToEntityModelError(w, r, err)
return
}
if err := app.writeJSON(w, http.StatusOK, envelope{"category": category}, nil); err != nil {
app.serverErrorResponse(w, r, err)
}
}
func (app *application) deleteAdminSpeciesCategoryHandler(w http.ResponseWriter, r *http.Request) {
id, err := app.readIDParam(r)
if err != nil {
app.notFoundResponse(w, r)
return
}
category, err := app.models.SpeciesCategories.Get(id)
if err != nil {
app.respondToEntityModelError(w, r, err)
return
}
category.Active = false
if _, err = app.models.SpeciesCategories.Update(category); err != nil {
app.respondToEntityModelError(w, r, err)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (app *application) validateSpeciesCategory(w http.ResponseWriter, r *http.Request, category storage.SpeciesCategory) bool {
v := validate.New()
storage.ValidateSpeciesCategory(v, category)
if !v.Valid() {
app.failedValidationResponse(w, r, v.Errors)
return false
}
return true
}