Insert card into first available binder slot

This commit is contained in:
The Magician 2024-09-16 17:44:57 +01:00
parent 61127a1f92
commit b2946d00e2
4 changed files with 89 additions and 2 deletions

View File

@ -29,7 +29,7 @@ func MainCliLoop(db *sql.DB) {
// TODO: Add the ability to modify this
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.Either,
Foil: logic.True,
Promo: logic.Either,
Language: "en",
}

View File

@ -43,3 +43,19 @@ func InsertCardLocation(db *sql.DB, storageLocation CardLocation) error {
return nil
}
func InsertCardInExistingLocation(db *sql.DB, cardLocation CardLocation) error {
query := `UPDATE CardLocation SET CardPrintingId = ? WHERE Id = ?;`
update, err := db.Prepare(query)
if err != nil {
return err
}
_, err = update.Exec(cardLocation.CardPrintingId, cardLocation.Id)
if err != nil {
return err
}
return nil
}

View File

@ -34,6 +34,34 @@ func GetAllStorageAreas(db *sql.DB) ([]StorageArea, error) {
return storageAreas, nil
}
func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) {
var storageType string
query := `SELECT StorageType FROM StorageArea WHERE Id = ?;`
row := db.QueryRow(query, storageAreaId)
err := row.Scan(&storageType)
if err != nil {
return storageType, err
}
return storageType, nil
}
func GetNextEmptySlotInBinder(db *sql.DB, storageAreaId int) (int, error) {
query := `SELECT Id FROM CardLocation WHERE CardPrintingId IS NULL AND StorageAreaId = ? ORDER BY Position ASC LIMIT 1;`
var emptySlotId int
err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId)
if err == sql.ErrNoRows {
return -1, nil
} else if err != nil {
return 0, err
}
return emptySlotId, nil
}
func InsertStorageArea(db *sql.DB, storageArea StorageArea) error {
query := `INSERT INTO StorageArea (Name, StorageType) VALUES (?, ?);`

View File

@ -59,7 +59,7 @@ func SelectStorageArea(db *sql.DB) (database.StorageArea, error) {
return selectedStorageArea, nil
}
func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
func StoreAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
if err != nil {
return err
@ -74,3 +74,46 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
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
}