2025-04-28 12:38:15 -05:00
|
|
|
package inventoryslot
|
|
|
|
|
|
|
|
import (
|
2025-05-15 10:13:35 -05:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2025-04-28 12:38:15 -05:00
|
|
|
|
|
|
|
item "example.com/database/item"
|
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InventorySlot struct {
|
|
|
|
gorm.Model
|
2025-05-16 22:51:54 -05:00
|
|
|
Item item.Item `gorm:"foreignKey:Name" json:"item"`
|
2025-04-28 12:38:15 -05:00
|
|
|
Quantity int64 `json:"quantity"` // Positive
|
|
|
|
}
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
type InventorySlotParams struct {
|
|
|
|
// ID(s) of the object being modified
|
2025-05-16 22:51:54 -05:00
|
|
|
IDArray []uint
|
2025-05-15 10:13:35 -05:00
|
|
|
// New fields
|
|
|
|
Item uint `json:"item"`
|
|
|
|
Quantity int64 `json:"quantity"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (params *InventorySlotParams) parse(IDUintArray *[]uint, body *[]byte) error {
|
|
|
|
var inputParams InventorySlotParams
|
|
|
|
params.IDArray = *IDUintArray
|
|
|
|
if len(*body) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := json.Unmarshal(*body, &inputParams)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
params.Item = inputParams.Item
|
|
|
|
params.Quantity = inputParams.Quantity
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (inventorySlot InventorySlot) create(db *gorm.DB) error {
|
2025-04-28 12:38:15 -05:00
|
|
|
result := db.Create(&inventorySlot)
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-04-29 17:54:32 -05:00
|
|
|
func (inventorySlot *InventorySlot) getAssociations(db *gorm.DB) {
|
|
|
|
db.Model(&inventorySlot).Association("Item").Find(&inventorySlot.Item)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func (inventorySlot *InventorySlot) get(db *gorm.DB, inputInventorySlot uint) {
|
2025-04-29 17:54:32 -05:00
|
|
|
db.Where("id = ?", inputInventorySlot).Take(&inventorySlot)
|
|
|
|
inventorySlot.getAssociations(db)
|
|
|
|
}
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func (inventorySlot InventorySlot) update(db *gorm.DB) error {
|
|
|
|
// Get the original InventorySlot object
|
|
|
|
var originalInventorySlot InventorySlot
|
|
|
|
result := db.Model(&InventorySlot{}).Where("id = ?", inventorySlot.Model.ID).Take(&originalInventorySlot)
|
|
|
|
if result.Error != nil {
|
|
|
|
return result.Error
|
|
|
|
}
|
|
|
|
// Set the static values
|
|
|
|
originalInventorySlot.Quantity = inventorySlot.Quantity
|
|
|
|
// Set the associated values by grabbing them from the database
|
|
|
|
itemsError := db.Model(&originalInventorySlot).Association("Item").Replace(&inventorySlot.Item)
|
2025-04-29 17:54:32 -05:00
|
|
|
if itemsError != nil {
|
|
|
|
return itemsError
|
|
|
|
}
|
2025-05-15 10:13:35 -05:00
|
|
|
result = db.Model(&originalInventorySlot).Update("item", originalInventorySlot.Item)
|
|
|
|
if result.Error != nil {
|
|
|
|
err := errors.New("new item already exists")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
db.Save(&originalInventorySlot)
|
2025-04-29 17:54:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func (inventorySlot InventorySlot) delete(db *gorm.DB) error {
|
|
|
|
result := db.Unscoped().Delete(&inventorySlot)
|
2025-04-28 12:38:15 -05:00
|
|
|
if result.Error != nil {
|
2025-04-29 17:54:32 -05:00
|
|
|
return result.Error
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func Create(db *gorm.DB, params InventorySlotParams) error {
|
2025-05-16 22:51:54 -05:00
|
|
|
newItem := (*item.Get(db, []uint{params.Item}))[0]
|
2025-04-29 17:54:32 -05:00
|
|
|
return InventorySlot{
|
2025-05-15 10:13:35 -05:00
|
|
|
Item: newItem,
|
|
|
|
Quantity: params.Quantity,
|
|
|
|
}.create(db)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
2025-05-07 16:53:00 -05:00
|
|
|
func Get(db *gorm.DB, inputInventorySlots []uint) *[]InventorySlot {
|
2025-04-28 12:38:15 -05:00
|
|
|
var outputInventorySlots []InventorySlot
|
2025-05-15 10:13:35 -05:00
|
|
|
if len(inputInventorySlots) < 1 {
|
|
|
|
db.Model(&InventorySlot{}).Select("id").Find(&inputInventorySlots)
|
|
|
|
}
|
2025-04-28 12:38:15 -05:00
|
|
|
for _, inputInventorySlot := range inputInventorySlots {
|
2025-04-29 17:54:32 -05:00
|
|
|
var outputInventorySlot InventorySlot
|
2025-05-15 10:13:35 -05:00
|
|
|
outputInventorySlot.get(db, inputInventorySlot)
|
2025-04-29 17:54:32 -05:00
|
|
|
outputInventorySlots = append(outputInventorySlots, outputInventorySlot)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-04-29 17:54:32 -05:00
|
|
|
return &outputInventorySlots
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func Update(db *gorm.DB, params InventorySlotParams) error {
|
2025-05-16 22:51:54 -05:00
|
|
|
newItem := (*item.Get(db, []uint{params.Item}))[0]
|
2025-05-15 10:13:35 -05:00
|
|
|
return InventorySlot{
|
|
|
|
Item: newItem,
|
|
|
|
Quantity: params.Quantity,
|
|
|
|
}.update(db)
|
2025-04-29 17:54:32 -05:00
|
|
|
}
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func Delete(db *gorm.DB, inputInventorySlots []uint) error {
|
|
|
|
var inventorySlots []InventorySlot
|
|
|
|
// if len(inputInventorySlots) < 1 {
|
|
|
|
// result := db.Model(&InventorySlot{}).Select("id").Find(&inputInventorySlots)
|
|
|
|
// if result.Error != nil {
|
|
|
|
// return result.Error
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
for _, inputInventorySlot := range inputInventorySlots {
|
|
|
|
var inventorySlot InventorySlot
|
|
|
|
inventorySlot.get(db, inputInventorySlot)
|
|
|
|
inventorySlots = append(inventorySlots, inventorySlot)
|
|
|
|
}
|
|
|
|
for _, inventorySlot := range inventorySlots {
|
|
|
|
err := inventorySlot.delete(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-05-15 10:13:35 -05:00
|
|
|
return nil
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
|
|
|
|
2025-05-15 10:13:35 -05:00
|
|
|
func HandleRequest(method string, db *gorm.DB, IDUintArray *[]uint, body *[]byte) (*[]InventorySlot, error) {
|
|
|
|
var err error
|
|
|
|
var params InventorySlotParams
|
|
|
|
err = params.parse(IDUintArray, body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var result *[]InventorySlot
|
|
|
|
switch method {
|
|
|
|
case "GET":
|
|
|
|
result = Get(db, params.IDArray)
|
|
|
|
case "POST":
|
|
|
|
err = Create(db, params)
|
|
|
|
case "PUT":
|
|
|
|
err = Update(db, params)
|
|
|
|
case "DELETE":
|
|
|
|
err = Delete(db, params.IDArray)
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|
2025-05-15 10:13:35 -05:00
|
|
|
return result, err
|
2025-04-28 12:38:15 -05:00
|
|
|
}
|