fix: rename dish to meal
This commit is contained in:
64
model/resources/meals.go
Normal file
64
model/resources/meals.go
Normal file
@ -0,0 +1,64 @@
|
||||
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) Name() string { return d.Title }
|
||||
|
||||
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"]}
|
||||
}
|
||||
Reference in New Issue
Block a user