Files
unifood/model/resources/meals.go

72 lines
1.8 KiB
Go

package resources
import (
"fmt"
"strconv"
"strings"
"time"
"git.bissendorf.co/bissendorf/unifood/m/v2/model/external/stwbremen"
"git.bissendorf.co/bissendorf/unifood/m/v2/util"
)
func MealFromDTO(meal stwbremen.Meal) (*Meal, error) {
date, err := time.Parse(time.DateOnly, meal.Date)
if err != nil {
return nil, fmt.Errorf("unable to parse meal date: %w", err)
}
id, _ := strings.CutPrefix(meal.ID, "meals/")
return &Meal{
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 {
p = 0
}
return i.Label, float32(p)
}),
Ingredients: util.Select(util.Transform(meal.Ingredients, func(i *stwbremen.Ingredient) ingredient {
return ingredient{
Name: i.Label,
Additionals: i.Additionals,
}
}), func(i *ingredient) bool { return i.Name != "" }),
}, nil
}
const ResourceMeal = "meal"
type Meal struct {
Title string
ID string
RestaurantID string
Ingredients []ingredient
Prices map[string]float32
Date time.Time
Counter string
Tags []string
}
type ingredient struct {
Name string
Additionals []string
}
func (d *Meal) Kind() string { return ResourceMeal }
func (d *Meal) ItemName() string { return d.ID }
// Table output
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"]}
}