tags and special cakes: models, migrations, store layer

Add Tag, SpecialCake, Cake.Tags, Order.SpecialCakes types.
Add migrations for tag, cake_tag, special_cake, special_cake_tag tables.
Add store methods: GetTags, GetCakeTags, SaveCakeTags, CreateTag,
GetSpecialCakes, GetSpecialCakeTags, SaveSpecialCakes, saveSpecialCakeTags.
Load tags in GetCake/GetCakes, cascade tag saves in SaveCake.
Remove debug log calls from ParsePrice.
This commit is contained in:
bronkuu 2026-06-15 18:49:21 +02:00
parent 06231cc490
commit 9aafbb94ec
8 changed files with 297 additions and 23 deletions

View file

@ -1,25 +1,77 @@
package models
import "time"
import (
"fmt"
"time"
)
type Tag struct {
ID int
Name string
}
type Cake struct {
Name string
ID int
Price Price // increments of 0.01
Amount int
Tags []Tag
}
type SpecialCake struct {
ID int
OrderID int
Name string
Price Price
Size string
Shape string
Flavour string
Notes string
Tags []Tag
}
func (s SpecialCake) Detail() string {
parts := make([]string, 0, 4)
if s.Size != "" {
parts = append(parts, s.Size)
}
if s.Flavour != "" {
parts = append(parts, s.Flavour)
}
if s.Shape == "square" {
parts = append(parts, "kwadrat")
} else if s.Shape == "round" {
parts = append(parts, "okrągły")
}
if s.Notes != "" {
parts = append(parts, "notatka: "+s.Notes)
}
if len(parts) == 0 {
return ""
}
return fmt.Sprintf("(%s)", joinParts(parts))
}
func joinParts(parts []string) string {
out := parts[0]
for i := 1; i < len(parts); i++ {
out += ", " + parts[i]
}
return out
}
type Order struct {
ID int
Name string
Surname string
Phone string
Location string
Date time.Time
Accepted time.Time
Status string
Paid Price // increments of 0.01
Cakes []Cake
ID int
Name string
Surname string
Phone string
Location string
Date time.Time
Accepted time.Time
Status string
Paid Price // increments of 0.01
Cakes []Cake
SpecialCakes []SpecialCake
}
func (o *Order) Subtotal() Price {
@ -27,6 +79,9 @@ func (o *Order) Subtotal() Price {
for _, e := range o.Cakes {
out += e.Price * Price(e.Amount)
}
for _, s := range o.SpecialCakes {
out += s.Price
}
return out
}
@ -37,3 +92,14 @@ func (o *Order) Total() Price {
func (c *Cake) Total() Price {
return Price(c.Amount) * c.Price
}
type OrderCounts struct {
Active int
DueToday int
}
type CakeCount struct {
ID int
Name string
Amount int
}

View file

@ -2,7 +2,6 @@ package models
import (
"fmt"
"log"
"strconv"
"strings"
)
@ -12,18 +11,14 @@ type Price int
func (in Price) String() string {
if in%100 == 0 {
return strconv.Itoa(int(in) / 100)
} else {
price := strconv.Itoa(int(in))
i := len(price) - 2
price = price[:i] + "." + price[i:]
return price
}
price := strconv.Itoa(int(in))
i := len(price) - 2
return price[:i] + "." + price[i:]
}
func ParsePrice(in string) (Price, error) {
log.Println(in)
in = strings.ReplaceAll(in, ",", ".")
log.Println(in)
if !strings.Contains(in, ".") {
price, err := strconv.Atoi(in)
return Price(price) * 100, err
@ -39,6 +34,5 @@ func ParsePrice(in string) (Price, error) {
if len(after) < 1 {
price *= 10
}
log.Println(price)
return Price(price), err
}