94 lines
2.0 KiB
Go
94 lines
2.0 KiB
Go
package logic
|
|
|
|
import (
|
|
"database/sql"
|
|
"sevenkeys/database"
|
|
)
|
|
|
|
type StorageAreaSearchOptions map[string]int
|
|
|
|
func CreateStorageArea(db *sql.DB, storageArea database.StorageArea) error {
|
|
// TODO: Check if there's already a storage are with the same name
|
|
// TODO: Check if the type entered is valid
|
|
err := database.InsertStorageArea(db, storageArea)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
|
|
var options StorageAreaSearchOptions
|
|
|
|
storageAreas, err := database.GetAllStorageAreas(db)
|
|
if err != nil {
|
|
return options, err
|
|
}
|
|
|
|
for _, area := range storageAreas {
|
|
options[area.Name] = area.Id
|
|
}
|
|
|
|
return options, nil
|
|
}
|
|
|
|
func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {
|
|
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cardLocation.Position = lastPosition + 1
|
|
|
|
err = database.InsertCardLocation(db, cardLocation)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func StoreInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error {
|
|
cardLocation.Id = cardLocationId
|
|
err := database.InsertCardInExistingLocation(db, cardLocation)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
|
|
storageAreaType, err := database.GetStorageAreaTypeById(db, cardLocation.StorageAreaId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if storageAreaType == database.StorageAreaTypeBinder {
|
|
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId)
|
|
|
|
if err == sql.ErrNoRows {
|
|
err = StoreAfterLastCard(db, cardLocation)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
} else if err != nil {
|
|
return err
|
|
} else {
|
|
err = StoreInEmptySlot(db, cardLocation, nextEmptySlotId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
err = StoreAfterLastCard(db, cardLocation)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|