From b2946d00e23868686ba3ab866c87c71bd288f00c Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 16 Sep 2024 17:44:57 +0100 Subject: [PATCH] Insert card into first available binder slot --- sevenkeys/cli/mainui.go | 2 +- sevenkeys/database/cardstoragelocation.go | 16 ++++++++ sevenkeys/database/storagearea.go | 28 ++++++++++++++ sevenkeys/logic/storage.go | 45 ++++++++++++++++++++++- 4 files changed, 89 insertions(+), 2 deletions(-) diff --git a/sevenkeys/cli/mainui.go b/sevenkeys/cli/mainui.go index a9d3f74..ffbbfa9 100644 --- a/sevenkeys/cli/mainui.go +++ b/sevenkeys/cli/mainui.go @@ -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", } diff --git a/sevenkeys/database/cardstoragelocation.go b/sevenkeys/database/cardstoragelocation.go index bb02c0c..9e23199 100644 --- a/sevenkeys/database/cardstoragelocation.go +++ b/sevenkeys/database/cardstoragelocation.go @@ -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 +} diff --git a/sevenkeys/database/storagearea.go b/sevenkeys/database/storagearea.go index eab522f..1808a50 100644 --- a/sevenkeys/database/storagearea.go +++ b/sevenkeys/database/storagearea.go @@ -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 (?, ?);` diff --git a/sevenkeys/logic/storage.go b/sevenkeys/logic/storage.go index c7fcc16..20f0869 100644 --- a/sevenkeys/logic/storage.go +++ b/sevenkeys/logic/storage.go @@ -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 +}