Skip to content

Commit

Permalink
Merge pull request #160 from calvinmclean/feature/improve-storage
Browse files Browse the repository at this point in the history
Update babyapi for built-in EndDate functionality
  • Loading branch information
calvinmclean authored May 2, 2024
2 parents c731d2b + b7b725f commit 76bd874
Show file tree
Hide file tree
Showing 10 changed files with 12 additions and 54 deletions.
2 changes: 1 addition & 1 deletion garden-app/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.3

require (
github.com/AlecAivazis/survey/v2 v2.3.7
github.com/calvinmclean/babyapi v0.12.0
github.com/calvinmclean/babyapi v0.13.0
github.com/eclipse/paho.mqtt.golang v1.4.3
github.com/go-chi/render v1.0.3
github.com/go-co-op/gocron v1.35.2
Expand Down
4 changes: 2 additions & 2 deletions garden-app/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/calvinmclean/babyapi v0.12.0 h1:yisOOKKrZdsmhpSG3gYu6/8OGUrEHCvn+KoYtXTN/qE=
github.com/calvinmclean/babyapi v0.12.0/go.mod h1:XVUTIRu0bqXTfx7aUY66YXZUZeIGkNn8GeK+8DpJ59k=
github.com/calvinmclean/babyapi v0.13.0 h1:TTlsLfR1FVPdl8D8iMhlro2P8Ily9s22HcbGgmfd8sQ=
github.com/calvinmclean/babyapi v0.13.0/go.mod h1:KkJFZ4FUfPdKbu6xi/q3tIV7ncgvcSs66GmFwpLmF00=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down
10 changes: 5 additions & 5 deletions garden-app/pkg/storage/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ func NewClient(config Config) (*Client, error) {
}

return &Client{
Gardens: kv.NewClient[*pkg.Garden](db, "Garden"),
Zones: kv.NewClient[*pkg.Zone](db, "Zone"),
WaterSchedules: kv.NewClient[*pkg.WaterSchedule](db, "WaterSchedule"),
WeatherClientConfigs: kv.NewClient[*weather.Config](db, "WeatherClient"),
NotificationClientConfigs: kv.NewClient[*notifications.Client](db, "NotificationClient"),
Gardens: babyapi.NewKVStorage[*pkg.Garden](db, "Garden"),
Zones: babyapi.NewKVStorage[*pkg.Zone](db, "Zone"),
WaterSchedules: babyapi.NewKVStorage[*pkg.WaterSchedule](db, "WaterSchedule"),
WeatherClientConfigs: babyapi.NewKVStorage[*weather.Config](db, "WeatherClient"),
NotificationClientConfigs: babyapi.NewKVStorage[*notifications.Client](db, "NotificationClient"),
}, nil
}

Expand Down
12 changes: 0 additions & 12 deletions garden-app/pkg/storage/filters.go

This file was deleted.

3 changes: 1 addition & 2 deletions garden-app/pkg/storage/water_schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ import (

// GetZonesUsingWaterSchedule will find all Zones that use this WaterSchedule and return the Zones along with the Gardens they belong to
func (c *Client) GetZonesUsingWaterSchedule(id string) ([]*pkg.ZoneAndGarden, error) {
gardens, err := c.Gardens.GetAll(context.Background(), nil)
gardens, err := c.Gardens.GetAll(context.Background(), babyapi.EndDatedQueryParam(false))
if err != nil {
return nil, fmt.Errorf("unable to get all Gardens: %w", err)
}
gardens = FilterEndDated[*pkg.Garden](false).Filter(gardens)

results := []*pkg.ZoneAndGarden{}
for _, g := range gardens {
Expand Down
14 changes: 0 additions & 14 deletions garden-app/server/end_dated.go

This file was deleted.

2 changes: 0 additions & 2 deletions garden-app/server/garden.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ func NewGardensAPI(config Config, storageClient *storage.Client, influxdbClient
}
}))

gr.SetGetAllFilter(EndDatedFilter[*pkg.Garden])

gr.SetBeforeDelete(func(r *http.Request) *babyapi.ErrResponse {
logger := babyapi.GetLoggerFromContext(r.Context())
gardenID := gr.GetIDParam(r)
Expand Down
10 changes: 2 additions & 8 deletions garden-app/server/garden_responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/calvinmclean/automated-garden/garden-app/pkg"
"github.com/calvinmclean/automated-garden/garden-app/pkg/storage"
"github.com/calvinmclean/babyapi"

"github.com/go-chi/render"
Expand Down Expand Up @@ -157,16 +156,11 @@ func (agr AllGardensResponse) HTML(r *http.Request) string {
}

func (api *GardensAPI) getAllZones(ctx context.Context, gardenID string, getEndDated bool) ([]*pkg.Zone, error) {
zones, err := api.storageClient.Zones.GetAll(ctx, nil)
zones, err := api.storageClient.Zones.GetAll(ctx, babyapi.EndDatedQueryParam(getEndDated))
if err != nil {
return nil, fmt.Errorf("error getting Zones for Garden: %w", err)
}
zones = babyapi.FilterFunc[*pkg.Zone](func(z *pkg.Zone) bool {
gardenIDFilter := filterZoneByGardenID(gardenID)
endDateFilter := storage.FilterEndDated[*pkg.Zone](getEndDated)

return gardenIDFilter(z) && endDateFilter(z)
}).Filter(zones)
zones = babyapi.FilterFunc[*pkg.Zone](filterZoneByGardenID(gardenID)).Filter(zones)

return zones, nil
}
Expand Down
2 changes: 0 additions & 2 deletions garden-app/server/water_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ func NewWaterSchedulesAPI(storageClient *storage.Client, worker *worker.Worker)
return nil
})

api.SetGetAllFilter(EndDatedFilter[*pkg.WaterSchedule])

api.AddCustomRoute(http.MethodGet, "/components", babyapi.Handler(func(_ http.ResponseWriter, r *http.Request) render.Renderer {
switch r.URL.Query().Get("type") {
case "create_modal":
Expand Down
7 changes: 1 addition & 6 deletions garden-app/server/zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,7 @@ func NewZonesAPI(storageClient *storage.Client, influxdbClient influxdb.Client,

api.SetGetAllFilter(func(r *http.Request) babyapi.FilterFunc[*pkg.Zone] {
gardenID := api.GetParentIDParam(r)
gardenIDFilter := filterZoneByGardenID(gardenID)

endDateFilter := EndDatedFilter[*pkg.Zone](r)
return func(z *pkg.Zone) bool {
return gardenIDFilter(z) && endDateFilter(z)
}
return filterZoneByGardenID(gardenID)
})

api.ApplyExtension(extensions.HTMX[*pkg.Zone]{})
Expand Down

0 comments on commit 76bd874

Please sign in to comment.