package logic import ( "database/sql" "errors" "sevenkeys/database" ) 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 } 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 } type StorageAreaSearchOptions map[string]int func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) { var options StorageAreaSearchOptions = make(map[string]int) 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, productLocation database.ProductLocation) (int64, error) { lastPosition, err := database.GetLastPositionInStorageArea(db, productLocation.StorageAreaId) if err != nil { return -1, err } productLocation.Position = lastPosition + 1 id, err := database.InsertProductLocation(db, productLocation) if err != nil { return -1, err } return id, nil } func storeInEmptySlot(db *sql.DB, productLocation database.ProductLocation, productLocationId int) error { productLocation.Id = productLocationId err := database.InsertProductInExistingLocation(db, productLocation) if err != nil { return err } return nil } func StoreProduct(db *sql.DB, productLocation database.ProductLocation) (int64, error) { storageAreaType, err := database.GetStorageAreaTypeById(db, productLocation.StorageAreaId) if err != nil { return -1, err } if storageAreaType == database.StorageAreaTypeBinder { nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, productLocation.StorageAreaId) if err == database.ErrNoEmptySlotsInBinder { id, err := storeAfterLastCard(db, productLocation) if err != nil { return -1, err } return id, nil } else if err != nil { return -1, err } else { err = storeInEmptySlot(db, productLocation, nextEmptySlotId) if err != nil { return -1, err } } return int64(nextEmptySlotId), nil } id, err := storeAfterLastCard(db, productLocation) if err != nil { return -1, err } return id, nil } func Replace(db *sql.DB, productLocationId int, cardtraderBlueprintId int) error { productLocation := database.ProductLocation{ Id: productLocationId, CardtraderBlueprintId: cardtraderBlueprintId, } return database.InsertProductInExistingLocation(db, productLocation) }