86 lines
2.4 KiB
Go
86 lines
2.4 KiB
Go
package meals
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"git.bissendorf.co/bissendorf/unifood/m/v2/core/handler"
|
|
"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 MealsHandler struct {
|
|
interfaces.ResourceHandler
|
|
interfaces.GetHandler
|
|
|
|
QueryClient interfaces.QueryClient[[]stwbremen.Meal]
|
|
}
|
|
|
|
func (h *MealsHandler) Get(ctx context.Context, name string, params params.Container) (*interfaces.ResourceList, error) {
|
|
// Read parameters
|
|
p, err := params.GetValue(handler.ParamDate)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("unable to parse date parameter: %w", err)
|
|
}
|
|
date := p.(time.Time)
|
|
|
|
location, err := params.GetValue(handler.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
|
|
meals, 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 &interfaces.ResourceList{
|
|
ItemKind: resources.ResourceMeal,
|
|
Items: util.Transform(*meals, func(i *stwbremen.Meal) interfaces.Resource {
|
|
d, err := resources.MealFromDTO(*i)
|
|
if err != nil {
|
|
return &resources.Meal{}
|
|
}
|
|
|
|
return d
|
|
}),
|
|
}, nil
|
|
|
|
}
|
|
|
|
func (h *MealsHandler) GetParametersForVerb(verb interfaces.Verb) []params.Registration {
|
|
return []params.Registration{
|
|
{
|
|
Name: handler.ParamDate,
|
|
ShortHand: "d",
|
|
Description: "Meal date",
|
|
DefaultFunc: func() string { return time.Now().Format(time.DateOnly) },
|
|
ParseFunc: func(value string) (any, error) { return time.Parse(time.DateOnly, value) },
|
|
},
|
|
{
|
|
Name: handler.ParamLocation,
|
|
ShortHand: "l",
|
|
Description: "Select a restaurant",
|
|
DefaultFunc: func() string { return "330" },
|
|
ParseFunc: params.ParseString,
|
|
},
|
|
}
|
|
}
|