feat: unifood base with GET dishes (#1)
Features: - CLI structure with verbs and resources - Application config and parameters - Output formatters - Initial resource: dishes Reviewed-on: #1 Co-authored-by: bdoerfchen <git@bissendorf.co> Co-committed-by: bdoerfchen <git@bissendorf.co>
This commit is contained in:
64
model/resources/dish.go
Normal file
64
model/resources/dish.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 DishFromDTO(dish stwbremen.Dish) (*Dish, error) {
|
||||
date, err := time.Parse(time.DateOnly, dish.Date)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to parse dish date: %w", err)
|
||||
}
|
||||
|
||||
return &Dish{
|
||||
Title: dish.Title,
|
||||
Location: dish.Location,
|
||||
Date: date,
|
||||
Tags: strings.Split(strings.Replace(dish.Tags, " ", "", -1), ","),
|
||||
Counter: dish.Counter,
|
||||
Prices: util.Map(dish.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(dish.Ingredients, func(i *stwbremen.Ingredient) ingredient {
|
||||
return ingredient{
|
||||
Name: i.Label,
|
||||
Additionals: i.Additionals,
|
||||
}
|
||||
}), func(i *ingredient) bool { return i.Name != "" }),
|
||||
}, nil
|
||||
}
|
||||
|
||||
const ResourceDish = "dish"
|
||||
|
||||
type Dish 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 *Dish) Kind() string { return ResourceDish }
|
||||
func (d *Dish) Name() string { return d.Title }
|
||||
|
||||
func (d *Dish) ColumnNames() []string { return []string{"Location", "Date", "Counter", "Price"} }
|
||||
func (d *Dish) Columns() []any {
|
||||
return []any{d.Location, d.Date.Format(time.DateOnly), d.Counter, d.Prices["Studierende"]}
|
||||
}
|
||||
Reference in New Issue
Block a user