Season view: filter the editor to a year (#54) (#66)
Build image / build-and-push (push) Successful in 17s
Build image / build-and-push (push) Successful in 17s
Co-authored-by: Steve Dudenhoeffer <[email protected]>
This commit was merged in pull request #66.
This commit is contained in:
@@ -84,6 +84,7 @@ func New(cfg *config.Config, svc *service.Service) *gin.Engine {
|
||||
gardens.POST("/:id/copy", h.copyGarden) // duplicate a garden the actor owns
|
||||
gardens.GET("/:id/full", h.getGardenFull) // one-shot editor load
|
||||
gardens.GET("/:id/history", h.getGardenHistory) // change sets, newest first
|
||||
gardens.GET("/:id/years", h.getGardenYears) // years with planting data
|
||||
gardens.POST("/:id/objects", h.createObject)
|
||||
// The grow journal hangs off a garden even for entries about one bed or one
|
||||
// plop, so it inherits the ordinary garden-role check.
|
||||
|
||||
+34
-1
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
@@ -171,15 +172,47 @@ func (h *handlers) deleteObject(c *gin.Context) {
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// getGardenFull serves the editor's one-shot load. ?year=YYYY switches it to the
|
||||
// season view: every plop whose time in the ground overlapped that calendar
|
||||
// year, INCLUDING ones since removed.
|
||||
//
|
||||
// Undated plantings are returned for every year. Everything planted before this
|
||||
// feature existed has a null planted_at, so excluding them would empty every
|
||||
// garden the moment a year was picked — technically defensible, useless in
|
||||
// practice. Without the param the behaviour is exactly what it has always been.
|
||||
func (h *handlers) getGardenFull(c *gin.Context) {
|
||||
gardenID, ok := parseIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
full, err := h.svc.GardenFull(c.Request.Context(), mustActor(c).ID, gardenID)
|
||||
var year *int
|
||||
if raw := c.Query("year"); raw != "" {
|
||||
y, err := strconv.Atoi(raw)
|
||||
if err != nil {
|
||||
writeAPIError(c, http.StatusBadRequest, "INVALID_INPUT", "year must be a four-digit year")
|
||||
return
|
||||
}
|
||||
year = &y
|
||||
}
|
||||
full, err := h.svc.GardenFull(c.Request.Context(), mustActor(c).ID, gardenID, year)
|
||||
if err != nil {
|
||||
writeServiceError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, full)
|
||||
}
|
||||
|
||||
// getGardenYears lists the years this garden has planting data for, so the year
|
||||
// selector can offer only years that hold something.
|
||||
func (h *handlers) getGardenYears(c *gin.Context) {
|
||||
gardenID, ok := parseIDParam(c, "id")
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
years, err := h.svc.GardenYears(c.Request.Context(), mustActor(c).ID, gardenID)
|
||||
if err != nil {
|
||||
writeServiceError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"years": years})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user