66 lines
1.6 KiB
Go
66 lines
1.6 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)
|
|
}
|
|
|
|
return &Meal{
|
|
Title: meal.Title,
|
|
Location: meal.Location,
|
|
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
|
|
Location 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.Title }
|
|
|
|
// 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"]}
|
|
}
|