90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
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) (*interfaces.ResourceList, 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 &interfaces.ResourceList{
|
|
ItemKind: resources.ResourceDish,
|
|
Items: util.Transform(*dishes, func(i *stwbremen.Dish) interfaces.Resource {
|
|
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,
|
|
},
|
|
}
|
|
}
|