feat: get handlers return object slices
This commit is contained in:
86
core/handler/dishes/handler.go
Normal file
86
core/handler/dishes/handler.go
Normal file
@ -0,0 +1,86 @@
|
||||
package dishes
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces"
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces/params"
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/model/external/stwbremen"
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/model/resources"
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/util"
|
||||
)
|
||||
|
||||
type DishesHandler struct {
|
||||
interfaces.ResourceHandler
|
||||
interfaces.GetHandler
|
||||
|
||||
QueryClient interfaces.QueryClient[[]stwbremen.Dish]
|
||||
}
|
||||
|
||||
const (
|
||||
paramDate = "date"
|
||||
paramLocation = "location"
|
||||
)
|
||||
|
||||
func (h *DishesHandler) Get(ctx context.Context, params params.Container) ([]any, error) {
|
||||
// Read parameters
|
||||
p, err := params.GetValue(paramDate)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse date parameter: %w", err)
|
||||
}
|
||||
date := p.(time.Time)
|
||||
|
||||
location, err := params.GetValue(paramLocation)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse location parameter: %w", err)
|
||||
}
|
||||
|
||||
// Build query
|
||||
query := fmt.Sprintf(
|
||||
`page('meals').children.filterBy('location', '%s').filterBy('date', '%s').filterBy('printonly', 0)`,
|
||||
location,
|
||||
date.Format(time.DateOnly),
|
||||
)
|
||||
|
||||
// Run query
|
||||
dishes, err := h.QueryClient.Get(ctx,
|
||||
query,
|
||||
`{"title":true,"ingredients":"page.ingredients.toObject","prices":"page.prices.toObject","location":true,"counter":true,"date":true,"mealadds":true,"mark":true,"frei3":true,"printonly":true,"kombicategory":true,"categories":"page.categories.split"}`,
|
||||
false,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("querying menu failed: %w", err)
|
||||
}
|
||||
|
||||
// Return
|
||||
return util.Transform(*dishes, func(i *stwbremen.Dish) any {
|
||||
d, err := resources.DishFromDTO(*i)
|
||||
if err != nil {
|
||||
return resources.Dish{}
|
||||
}
|
||||
|
||||
return *d
|
||||
}), nil
|
||||
|
||||
}
|
||||
|
||||
func (h *DishesHandler) GetParametersForVerb(verb interfaces.Verb) []params.Registration {
|
||||
return []params.Registration{
|
||||
{
|
||||
Name: paramDate,
|
||||
ShortHand: "d",
|
||||
Description: "Menu date",
|
||||
DefaultFunc: func() string { return time.Now().Format(time.DateOnly) },
|
||||
ParseFunc: func(value string) (any, error) { return time.Parse(time.DateOnly, value) },
|
||||
},
|
||||
{
|
||||
Name: paramLocation,
|
||||
ShortHand: "l",
|
||||
Description: "Define the restaurant",
|
||||
DefaultFunc: func() string { return "330" },
|
||||
ParseFunc: params.ParseString,
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user