2024-08-01 23:02:55 +02:00
|
|
|
package db
|
|
|
|
|
|
|
|
import "gorm.io/gorm"
|
|
|
|
|
|
|
|
type Item struct {
|
|
|
|
gorm.Model
|
|
|
|
Name string
|
2024-10-29 11:25:01 +01:00
|
|
|
Comment string
|
2024-11-20 17:17:21 +01:00
|
|
|
Price uint64
|
2024-11-01 08:29:48 +01:00
|
|
|
MetricType uint8
|
|
|
|
MetricValue uint64
|
2024-08-01 23:02:55 +02:00
|
|
|
//
|
|
|
|
CategoryID uint
|
|
|
|
Category *Category
|
|
|
|
//
|
|
|
|
Proteins uint64
|
|
|
|
Carbs uint64
|
|
|
|
Fats uint64
|
|
|
|
//
|
2024-11-20 17:17:45 +01:00
|
|
|
Prices []ItemPrice `gorm:"constraint:OnDelete:CASCADE;"`
|
2024-08-01 23:02:55 +02:00
|
|
|
CurrentPriceID uint
|
|
|
|
CurrentPrice *ItemPrice
|
2024-11-01 08:30:01 +01:00
|
|
|
//
|
|
|
|
TypeID uint
|
|
|
|
Type *Type
|
2024-11-18 08:51:46 +01:00
|
|
|
UserID uint
|
|
|
|
User *User
|
|
|
|
}
|
|
|
|
|
2024-11-20 17:19:12 +01:00
|
|
|
func (i Item) __internalBelogingToPayment() {}
|
|
|
|
|
2024-11-18 08:51:46 +01:00
|
|
|
// Implements db.UserIdentifiable:1
|
|
|
|
func (i Item) GetID() uint {
|
|
|
|
return i.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements db.UserIdentifiable:2
|
|
|
|
func (i Item) GetUserID() uint {
|
|
|
|
return i.UserID
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implements db.UserIdentifiable:3
|
|
|
|
func (i *Item) SetUserID(id uint) {
|
|
|
|
i.UserID = id
|
2024-08-01 23:02:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetItem(id uint, preloadPrices bool) (*Item, error) {
|
|
|
|
if udb == nil {
|
|
|
|
return nil, ERROR_DB_NOT_INIT
|
|
|
|
}
|
|
|
|
db := udb
|
|
|
|
if preloadPrices {
|
|
|
|
db = db.Preload("Prices")
|
|
|
|
}
|
|
|
|
var item Item
|
|
|
|
err := db.Preload("Category").Preload("CurrentPrice").First(&item, id).Error
|
|
|
|
return &item, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetItemToRootCat(id uint, preloadPrices bool) (*Item, error) {
|
|
|
|
if udb == nil {
|
|
|
|
return nil, ERROR_DB_NOT_INIT
|
|
|
|
}
|
|
|
|
db := udb
|
|
|
|
if preloadPrices {
|
|
|
|
db = db.Preload("Prices")
|
|
|
|
}
|
|
|
|
var item Item
|
|
|
|
err := db.Preload("Category.Parent", func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Preload("Parent", func(db *gorm.DB) *gorm.DB {
|
|
|
|
return db.Preload("Parent")
|
|
|
|
})
|
|
|
|
}).Preload("CurrentPrice").First(&item, id).Error
|
|
|
|
return &item, err
|
|
|
|
}
|