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.
38 lines
755 B
Go
38 lines
755 B
Go
package models
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type Price int
|
|
|
|
func (in Price) String() string {
|
|
if in%100 == 0 {
|
|
return strconv.Itoa(int(in) / 100)
|
|
}
|
|
price := strconv.Itoa(int(in))
|
|
i := len(price) - 2
|
|
return price[:i] + "." + price[i:]
|
|
}
|
|
|
|
func ParsePrice(in string) (Price, error) {
|
|
in = strings.ReplaceAll(in, ",", ".")
|
|
if !strings.Contains(in, ".") {
|
|
price, err := strconv.Atoi(in)
|
|
return Price(price) * 100, err
|
|
}
|
|
before, after, _ := strings.Cut(in, ".")
|
|
if len(after) > 2 {
|
|
return 0, fmt.Errorf("Wrong format, requires no more than 2 decimal places after the separator")
|
|
}
|
|
price, err := strconv.Atoi(before + after)
|
|
if len(after) < 2 {
|
|
price *= 10
|
|
}
|
|
if len(after) < 1 {
|
|
price *= 10
|
|
}
|
|
return Price(price), err
|
|
}
|