2024-05-30 13:32:04 +00:00
|
|
|
package logic
|
|
|
|
|
2024-05-31 19:32:44 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2024-09-26 12:17:55 +00:00
|
|
|
"errors"
|
2024-05-31 19:32:44 +00:00
|
|
|
"sevenkeys/database"
|
|
|
|
)
|
2024-05-30 13:32:04 +00:00
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
var ErrCouldNotGetStorageAreaId error = errors.New("Could not get storage area ID.")
|
|
|
|
|
|
|
|
func GetStorageAreaId(db *sql.DB, storageAreaName string) (int, error) {
|
|
|
|
id, err := database.GetStorageAreaIdByName(db, storageAreaName)
|
|
|
|
if err == nil {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == database.ErrStorageAreaDoesNotExist {
|
|
|
|
storageOptions, err := GetStorageAreaSearchOptions(db)
|
|
|
|
if err != nil {
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, _, err := GenericSearch(storageOptions)
|
|
|
|
if err == nil {
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, ErrCouldNotGetStorageAreaId
|
|
|
|
}
|
|
|
|
|
|
|
|
return id, ErrCouldNotGetStorageAreaId
|
|
|
|
}
|
2024-09-17 10:25:51 +00:00
|
|
|
|
2024-08-20 11:57:36 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
type StorageAreaSearchOptions map[string]int
|
|
|
|
|
2024-09-17 10:25:51 +00:00
|
|
|
func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
|
2024-09-26 12:17:55 +00:00
|
|
|
var options StorageAreaSearchOptions = make(map[string]int)
|
2024-08-20 17:00:23 +00:00
|
|
|
|
|
|
|
storageAreas, err := database.GetAllStorageAreas(db)
|
|
|
|
if err != nil {
|
2024-09-17 10:25:51 +00:00
|
|
|
return options, err
|
2024-08-20 17:00:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, area := range storageAreas {
|
2024-09-17 10:25:51 +00:00
|
|
|
options[area.Name] = area.Id
|
2024-08-20 17:00:23 +00:00
|
|
|
}
|
|
|
|
|
2024-09-17 10:25:51 +00:00
|
|
|
return options, nil
|
2024-08-20 17:00:23 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
|
2024-09-05 09:53:14 +00:00
|
|
|
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
|
2024-05-31 19:32:44 +00:00
|
|
|
if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-05-31 19:32:44 +00:00
|
|
|
}
|
2024-05-30 13:32:04 +00:00
|
|
|
|
2024-09-05 09:53:14 +00:00
|
|
|
cardLocation.Position = lastPosition + 1
|
2024-05-30 13:32:04 +00:00
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
id, err := database.InsertCardLocation(db, cardLocation)
|
2024-05-31 19:32:44 +00:00
|
|
|
if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-05-31 19:32:44 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
return id, nil
|
2024-05-30 13:32:04 +00:00
|
|
|
}
|
2024-09-16 16:44:57 +00:00
|
|
|
|
2024-09-26 12:17:55 +00:00
|
|
|
func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error {
|
2024-09-16 16:44:57 +00:00
|
|
|
cardLocation.Id = cardLocationId
|
|
|
|
err := database.InsertCardInExistingLocation(db, cardLocation)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
|
2024-09-16 16:44:57 +00:00
|
|
|
storageAreaType, err := database.GetStorageAreaTypeById(db, cardLocation.StorageAreaId)
|
|
|
|
if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-09-16 16:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if storageAreaType == database.StorageAreaTypeBinder {
|
|
|
|
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId)
|
|
|
|
|
2024-10-01 13:56:13 +00:00
|
|
|
if err == database.ErrNoEmptySlotsInBinder {
|
2024-10-10 14:43:56 +00:00
|
|
|
id, err := storeAfterLastCard(db, cardLocation)
|
2024-09-16 16:44:57 +00:00
|
|
|
if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-09-16 16:44:57 +00:00
|
|
|
}
|
2024-10-10 14:43:56 +00:00
|
|
|
|
|
|
|
return id, nil
|
2024-09-16 16:44:57 +00:00
|
|
|
} else if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-09-16 16:44:57 +00:00
|
|
|
} else {
|
2024-09-26 12:17:55 +00:00
|
|
|
err = storeInEmptySlot(db, cardLocation, nextEmptySlotId)
|
2024-09-16 16:44:57 +00:00
|
|
|
if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-09-16 16:44:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
return int64(nextEmptySlotId), nil
|
2024-09-16 16:44:57 +00:00
|
|
|
}
|
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
id, err := storeAfterLastCard(db, cardLocation)
|
2024-09-16 16:44:57 +00:00
|
|
|
if err != nil {
|
2024-10-10 14:43:56 +00:00
|
|
|
return -1, err
|
2024-09-16 16:44:57 +00:00
|
|
|
}
|
2024-09-26 12:17:55 +00:00
|
|
|
|
2024-10-10 14:43:56 +00:00
|
|
|
return id, nil
|
2024-09-16 16:44:57 +00:00
|
|
|
}
|