63 lines
952 B
Go
63 lines
952 B
Go
/**
|
|
* file: types/product.go
|
|
* author: Theo Technicguy
|
|
* license: Apache 2.0
|
|
*
|
|
* Product type and functions
|
|
*/
|
|
|
|
package types
|
|
|
|
type TTL struct {
|
|
Normal uint `json:"normal"`
|
|
Fridge uint `json:"fridge"`
|
|
Freezer uint `json:"freezer"`
|
|
}
|
|
|
|
type Product struct {
|
|
Id uint `json:"id"`
|
|
EAN uint `json:"ean"`
|
|
Name string `json:"name"`
|
|
TTL TTL `json:"ttl"`
|
|
Tags []Tag `json:"tags"`
|
|
}
|
|
|
|
func (t *TTL) ValidTTL() bool {
|
|
if t.Normal == 0 {
|
|
return true
|
|
}
|
|
|
|
var fridge, freezer bool
|
|
|
|
if t.Fridge > 0 {
|
|
fridge = t.Normal < t.Fridge
|
|
} else {
|
|
fridge = true
|
|
}
|
|
|
|
if t.Freezer > 0 {
|
|
freezer = t.Normal < t.Freezer
|
|
} else {
|
|
freezer = true
|
|
}
|
|
return fridge && freezer
|
|
}
|
|
|
|
func (t *TTL) Valid() bool {
|
|
return t.ValidTTL()
|
|
}
|
|
|
|
func (p *Product) ValidName() bool {
|
|
return p.Name != ""
|
|
}
|
|
|
|
func (p *Product) Valid() bool {
|
|
for _, tag := range p.Tags {
|
|
if !tag.Valid() {
|
|
return false
|
|
}
|
|
}
|
|
|
|
return p.TTL.Valid() && p.ValidName()
|
|
}
|