Archived
1
0
Fork 0
This repository has been archived on 2024-12-18. You can view files and clone it, but cannot push or open issues or pull requests.
restockbot/models.go
Julien Riou 17a88265c6
Add HTTP API
Add `-api` mode to start the HTTP API with the following routes:
- /health
- /shops
- /shops/:id
- /products
- /products/:id

Signed-off-by: Julien Riou <julien@riou.xyz>
2021-03-30 18:00:12 +02:00

45 lines
1.3 KiB
Go

package main
import (
"gorm.io/gorm"
)
// Product is self-explainatory
type Product struct {
gorm.Model
Name string `gorm:"not null" json:"name"`
URL string `gorm:"unique" json:"url"`
Price float64 `gorm:"not null" json:"price"`
PriceCurrency string `gorm:"not null" json:"price_currency"`
Available bool `gorm:"not null;default:false" json:"available"`
ShopID uint `json:"shop_id"`
Shop Shop `json:"shop"`
}
// Equal compares a database product to another product
func (p *Product) Equal(other *Product) bool {
return p.URL == other.URL && p.Available == other.Available
}
// IsValid returns true when a Product has all required values
func (p *Product) IsValid() bool {
return p.Name != "" && p.URL != "" && p.Price != 0 && p.PriceCurrency != ""
}
// Merge one product with another
func (p *Product) Merge(o *Product) {
p.Price = o.Price
p.PriceCurrency = o.PriceCurrency
p.Available = o.Available
}
// ToMerge detects if a product needs to be merged with another one
func (p *Product) ToMerge(o *Product) bool {
return p.Price != o.Price || p.PriceCurrency != o.PriceCurrency || p.Available != o.Available
}
// Shop represents a retailer website
type Shop struct {
ID uint `gorm:"primaryKey"`
Name string `gorm:"unique" json:"name"`
}