feat: improve meals with better search and ids (#5)

Features:
- Meals now have an id instead of a title only
- It is now possible to get all meals without date or restaurant filter
- Sorting meals output

Reviewed-on: #5
Co-authored-by: bdoerfchen <git@bissendorf.co>
Co-committed-by: bdoerfchen <git@bissendorf.co>
This commit is contained in:
2025-07-20 23:00:07 +00:00
committed by bissendorf
parent 4b7866da03
commit 90a5125c66
5 changed files with 74 additions and 42 deletions

View File

@ -2,6 +2,7 @@ package stwbremen
type Meal struct {
Title string `json:"title"`
ID string `json:"id"`
Ingredients []Ingredient `json:"ingredients"`
Prices []Price `json:"prices"`
Location string `json:"location"`

View File

@ -16,12 +16,15 @@ func MealFromDTO(meal stwbremen.Meal) (*Meal, error) {
return nil, fmt.Errorf("unable to parse meal date: %w", err)
}
id, _ := strings.CutPrefix(meal.ID, "meals/")
return &Meal{
Title: meal.Title,
Location: meal.Location,
Date: date,
Tags: strings.Split(strings.Replace(meal.Tags, " ", "", -1), ","),
Counter: meal.Counter,
Title: meal.Title,
RestaurantID: meal.Location,
ID: id,
Date: date,
Tags: strings.Split(strings.Replace(meal.Tags, " ", "", -1), ","),
Counter: meal.Counter,
Prices: util.Map(meal.Prices, func(i *stwbremen.Price) (string, float32) {
p, err := strconv.ParseFloat(strings.Trim(i.Price, " "), 32)
if err != nil {
@ -41,13 +44,14 @@ func MealFromDTO(meal stwbremen.Meal) (*Meal, error) {
const ResourceMeal = "meal"
type Meal struct {
Title string
Location string
Ingredients []ingredient
Prices map[string]float32
Date time.Time
Counter string
Tags []string
Title string
ID string
RestaurantID string
Ingredients []ingredient
Prices map[string]float32
Date time.Time
Counter string
Tags []string
}
type ingredient struct {
@ -56,10 +60,12 @@ type ingredient struct {
}
func (d *Meal) Kind() string { return ResourceMeal }
func (d *Meal) ItemName() string { return d.Title }
func (d *Meal) ItemName() string { return d.ID }
// Table output
func (d *Meal) ColumnNames() []string { return []string{"Location", "Date", "Counter", "Price"} }
func (d *Meal) Columns() []any {
return []any{d.Location, d.Date.Format(time.DateOnly), d.Counter, d.Prices["Studierende"]}
func (d *Meal) ColumnNames() []string {
return []string{"Date", "Restaurant", "Title", "Counter", "Price"}
}
func (d *Meal) Columns() []any {
return []any{d.Date.Format(time.DateOnly), d.RestaurantID, d.Title, d.Counter, d.Prices["Studierende"]}
}