feat: meals with id and better search

This commit is contained in:
2025-07-21 00:57:53 +02:00
parent 4b7866da03
commit 6278b773d0
5 changed files with 74 additions and 42 deletions

View File

@ -3,6 +3,8 @@ package meals
import (
"context"
"fmt"
"slices"
"strings"
"time"
"git.bissendorf.co/bissendorf/unifood/m/v2/core/handler"
@ -22,47 +24,68 @@ type MealsHandler struct {
func (h *MealsHandler) Get(ctx context.Context, name string, params params.Container) (*interfaces.ResourceList, error) {
// Read parameters
p, err := params.GetValue(handler.ParamDate)
date, 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)
restaurant, err := params.GetValue(handler.ParamRestaurant)
if err != nil {
return nil, fmt.Errorf("unable to parse location parameter: %w", err)
return nil, fmt.Errorf("unable to parse restaurant 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),
)
query := "page('meals').children.filterBy('printonly', 0)"
if date != "" {
parsed, err := time.Parse(time.DateOnly, date.(string))
if err != nil {
return nil, fmt.Errorf("unable to parse date parameter: %w", err)
}
query += fmt.Sprintf(".filterBy('date', '%s')", parsed.Format(time.DateOnly))
}
if restaurant != "" {
query += fmt.Sprintf(".filterBy('location', '%s')", restaurant.(string))
}
if name != "" {
query += fmt.Sprintf(".filterBy('id', 'meals/%s')", name)
}
// 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"}`,
`{"title":true,"id":"page.id","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)
}
// Transform to meal resource
items := util.Transform(*meals, func(i *stwbremen.Meal) *resources.Meal {
d, err := resources.MealFromDTO(*i)
if err != nil {
return &resources.Meal{}
}
return d
})
// Sort by date, restaurant and title - need to apply stable sorts in reverse order to achieve this
slices.SortFunc(items, func(a, b *resources.Meal) int {
return strings.Compare(a.Title, b.Title)
})
slices.SortStableFunc(items, func(a, b *resources.Meal) int {
return strings.Compare(a.RestaurantID, b.RestaurantID)
})
slices.SortStableFunc(items, func(a, b *resources.Meal) int {
return a.Date.Compare(b.Date)
})
// 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
}),
Items: util.Transform(items, func(i **resources.Meal) interfaces.Resource { return *i }),
}, nil
}
func (h *MealsHandler) GetParametersForVerb(verb interfaces.Verb) []params.Registration {
@ -71,14 +94,14 @@ func (h *MealsHandler) GetParametersForVerb(verb interfaces.Verb) []params.Regis
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) },
DefaultFunc: func() string { return "" },
ParseFunc: params.ParseString,
},
{
Name: handler.ParamLocation,
ShortHand: "l",
Name: handler.ParamRestaurant,
ShortHand: "r",
Description: "Select a restaurant",
DefaultFunc: func() string { return "330" },
DefaultFunc: func() string { return "" },
ParseFunc: params.ParseString,
},
}