62 lines
1.7 KiB
Go
62 lines
1.7 KiB
Go
![]() |
package inventoryslot
|
||
|
|
||
|
import (
|
||
|
"log"
|
||
|
|
||
|
item "example.com/database/item"
|
||
|
|
||
|
"gorm.io/gorm"
|
||
|
)
|
||
|
|
||
|
type InventorySlot struct {
|
||
|
gorm.Model
|
||
|
Item item.Item `gorm:"foreignKey:Name" json:"item"`
|
||
|
Quantity int64 `json:"quantity"` // Positive
|
||
|
}
|
||
|
|
||
|
func CreateDatabaseInventorySlot(db *gorm.DB, inventorySlot InventorySlot) error {
|
||
|
result := db.Create(&inventorySlot)
|
||
|
if result.Error != nil {
|
||
|
return result.Error
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func GetDatabaseInventorySlot(db *gorm.DB, inputInventorySlot int) InventorySlot {
|
||
|
var outputInventorySlot InventorySlot
|
||
|
result := db.Model(&InventorySlot{}).Where("id = ?", inputInventorySlot).Take(&outputInventorySlot)
|
||
|
if result.Error != nil {
|
||
|
return InventorySlot{}
|
||
|
}
|
||
|
db.Model(&outputInventorySlot).Association("Item").Find(&outputInventorySlot.Item)
|
||
|
return outputInventorySlot
|
||
|
}
|
||
|
|
||
|
func GetDatabaseInventorySlots(db *gorm.DB, inputInventorySlots []int) []InventorySlot {
|
||
|
var outputInventorySlots []InventorySlot
|
||
|
for _, inputInventorySlot := range inputInventorySlots {
|
||
|
outputInventorySlots = append(outputInventorySlots, GetDatabaseInventorySlot(db, inputInventorySlot))
|
||
|
}
|
||
|
return outputInventorySlots
|
||
|
}
|
||
|
|
||
|
func GetAllDatabaseInventorySlots(db *gorm.DB) []InventorySlot {
|
||
|
var outputInventorySlots []InventorySlot
|
||
|
result := db.Find(&outputInventorySlots)
|
||
|
if result.Error != nil {
|
||
|
log.Println(result.Error)
|
||
|
}
|
||
|
for index, outputInventorySlot := range outputInventorySlots {
|
||
|
outputInventorySlots[index] = GetDatabaseInventorySlot(db, int(outputInventorySlot.ID))
|
||
|
}
|
||
|
return outputInventorySlots
|
||
|
}
|
||
|
|
||
|
func UpdateDatabaseInventorySlot(db *gorm.DB, inventoySlot InventorySlot) error {
|
||
|
result := db.Save(&inventoySlot)
|
||
|
if result.Error != nil {
|
||
|
return result.Error
|
||
|
}
|
||
|
return nil
|
||
|
}
|