@@ -0,0 +1,93 @@
|
||||
package web
|
||||
|
||||
import (
|
||||
"gardomatic.kleiax.de/lib/client"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type careInstructionForm struct {
|
||||
Text string `form:"text"`
|
||||
Status string `form:"status"`
|
||||
}
|
||||
|
||||
func (app *application) careInstructionSave(w http.ResponseWriter, r *http.Request) {
|
||||
gardenID, e := app.readPathID(r, "gardenID")
|
||||
if e != nil {
|
||||
app.notFound(w)
|
||||
return
|
||||
}
|
||||
speciesID, e := app.readPathID(r, "speciesID")
|
||||
if e != nil {
|
||||
app.notFound(w)
|
||||
return
|
||||
}
|
||||
var form careInstructionForm
|
||||
if e = app.decodePostForm(r, &form); e != nil {
|
||||
app.clientError(w, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
form.Text = strings.TrimSpace(form.Text)
|
||||
if form.Status == "" {
|
||||
form.Status = "untested"
|
||||
}
|
||||
input := client.CareInstructionInput{Text: &form.Text, Status: &form.Status}
|
||||
instructionID, _ := app.readPathID(r, "instructionID")
|
||||
if instructionID > 0 {
|
||||
_, _, e = client.FromContext(r.Context()).UpdateCareInstruction(r.Context(), gardenID, speciesID, instructionID, input)
|
||||
} else {
|
||||
_, _, e = client.FromContext(r.Context()).CreateCareInstruction(r.Context(), gardenID, speciesID, input)
|
||||
}
|
||||
if e != nil {
|
||||
app.handleAPIError(w, r, e)
|
||||
return
|
||||
}
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
apiClient := client.FromContext(r.Context())
|
||||
instructions, _, loadErr := apiClient.CareInstructions(r.Context(), gardenID, speciesID)
|
||||
if loadErr != nil {
|
||||
app.handleAPIError(w, r, loadErr)
|
||||
return
|
||||
}
|
||||
data := app.newTemplateData(r)
|
||||
garden, _, loadErr := apiClient.Garden(r.Context(), gardenID)
|
||||
if loadErr != nil {
|
||||
app.handleAPIError(w, r, loadErr)
|
||||
return
|
||||
}
|
||||
species, _, loadErr := apiClient.Species(r.Context(), gardenID, speciesID)
|
||||
if loadErr != nil {
|
||||
app.handleAPIError(w, r, loadErr)
|
||||
return
|
||||
}
|
||||
data.Garden = &garden
|
||||
data.SpeciesID = speciesID
|
||||
data.SpeciesGlobal = species.GardenID == nil
|
||||
data.CareInstructions = instructions
|
||||
app.renderTemplate(w, http.StatusOK, "species_form.tmpl", "care_instruction_list", data)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, pathWithQuery(webPath("species.edit", gardenID, speciesID), "step", "care"), http.StatusSeeOther)
|
||||
}
|
||||
func (app *application) careInstructionDelete(w http.ResponseWriter, r *http.Request) {
|
||||
gardenID, e := app.readPathID(r, "gardenID")
|
||||
if e != nil {
|
||||
app.notFound(w)
|
||||
return
|
||||
}
|
||||
speciesID, e := app.readPathID(r, "speciesID")
|
||||
if e != nil {
|
||||
app.notFound(w)
|
||||
return
|
||||
}
|
||||
instructionID, e := app.readPathID(r, "instructionID")
|
||||
if e != nil {
|
||||
app.notFound(w)
|
||||
return
|
||||
}
|
||||
if _, e = client.FromContext(r.Context()).DeleteCareInstruction(r.Context(), gardenID, speciesID, instructionID); e != nil {
|
||||
app.handleAPIError(w, r, e)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, pathWithQuery(webPath("species.edit", gardenID, speciesID), "step", "care"), http.StatusSeeOther)
|
||||
}
|
||||
Reference in New Issue
Block a user