From b30525c2d471279d265ff456b36245ccd08a3050 Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 29 Oct 2024 22:07:06 +0000 Subject: [PATCH] Fix search-storage command and add remove command --- sevenkeys/database/locate.go | 27 ++++++++++++++++++++++++ sevenkeys/logic/locate.go | 11 ++++++++++ sevenkeys/logic/search.go | 11 +--------- sevenkeys/logic/storage.go | 9 ++++++++ sevenkeys/main.go | 40 ++++++++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 10 deletions(-) diff --git a/sevenkeys/database/locate.go b/sevenkeys/database/locate.go index 6b9ae61..38ca346 100644 --- a/sevenkeys/database/locate.go +++ b/sevenkeys/database/locate.go @@ -20,6 +20,33 @@ type LocateCardResult struct { Position int } +func GetLocateResultByCardLocationId(db *sql.DB, cardLocationId int) (LocateCardResult, error) { + var result LocateCardResult + + query := `SELECT CardLocation.Id, + CardPrinting.Name, + CardPrinting.SetCode, + CardPrinting.IsFoil, + CardPrinting.IsPromo, + CardPrinting.CollectorNumber, + CardPrinting.Language, + StorageArea.Id, + StorageArea.StorageType, + StorageArea.Name, + CardLocation.Position + FROM CardLocation + JOIN CardPrinting ON CardLocation.CardPrintingId = CardPrinting.Id + JOIN StorageArea ON CardLocation.StorageAreaId = StorageArea.Id + WHERE CardLocation.Id = ?;` + + err := db.QueryRow(query, cardLocationId).Scan(&result.CardLocationId, &result.CardName, &result.SetCode, &result.IsFoil, &result.IsPromo, &result.CollectorNumber, &result.Language, &result.StorageAreaId, &result.StorageAreaType, &result.StorageAreaName, &result.Position) + if err != nil { + return result, err + } + + return result, nil +} + func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error) { var results []LocateCardResult diff --git a/sevenkeys/logic/locate.go b/sevenkeys/logic/locate.go index cf1a5fe..ccacda2 100644 --- a/sevenkeys/logic/locate.go +++ b/sevenkeys/logic/locate.go @@ -30,6 +30,17 @@ func GetBinderLocationDescription(position int) string { return fmt.Sprintf(" on page %d in %s slot %d", page, frontOrBack, slot) } +func GetCardAtLocation(db *sql.DB, cardLocationId int) (database.LocateCardResult, error) { + var result database.LocateCardResult + + result, err := database.GetLocateResultByCardLocationId(db, cardLocationId) + if err != nil { + return result, err + } + + return result, nil +} + func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]database.LocateCardResult, error) { results, err := database.GetLocateResults(db, cardNames) if err != nil { diff --git a/sevenkeys/logic/search.go b/sevenkeys/logic/search.go index db7db2b..ff7d002 100644 --- a/sevenkeys/logic/search.go +++ b/sevenkeys/logic/search.go @@ -71,16 +71,7 @@ func GetAllStorageSearchOptions(db *sql.DB) (StorageSearchOptions, error) { } for _, storedCard := range storedCards { - searchString := fmt.Sprintf("%s (%s %s) [%s]", storedCard.CardName, storedCard.SetCode, storedCard.CollectorNumber, storedCard.Language) - - if storedCard.IsFoil { - searchString += " FOIL" - } - - if storedCard.IsPromo { - searchString += " PROMO" - } - + searchString := GetLocationDescription(storedCard) searchOptions[searchString] = storedCard.CardLocationId } diff --git a/sevenkeys/logic/storage.go b/sevenkeys/logic/storage.go index 90b02ef..f9fca57 100644 --- a/sevenkeys/logic/storage.go +++ b/sevenkeys/logic/storage.go @@ -120,3 +120,12 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) { return id, nil } + +func Replace(db *sql.DB, cardLocationId int, cardPrintingId string) error { + cardLocation := database.CardLocation{ + Id: cardLocationId, + CardPrintingId: cardPrintingId, + } + + return database.InsertCardInExistingLocation(db, cardLocation) +} diff --git a/sevenkeys/main.go b/sevenkeys/main.go index 8d9eb04..2dd5bee 100644 --- a/sevenkeys/main.go +++ b/sevenkeys/main.go @@ -2,8 +2,10 @@ package main import ( "bufio" + "errors" "flag" "fmt" + "log" "os" "sevenkeys/database" "sevenkeys/delverlens" @@ -35,6 +37,8 @@ const ( ImportSubcommand string = "import" SearchPrintingsSubcommand string = "search-printings" SearchStorageSubcommand string = "search-storage" + RemoveSubcommand string = "remove" + ReplaceSubcommand string = "replace" DeckSubcommand string = "deck" ) @@ -185,6 +189,42 @@ func main() { logic.Check(err) fmt.Println(id) break + case RemoveSubcommand: + removeCmd := flag.NewFlagSet(RemoveSubcommand, flag.ExitOnError) + + cardLocationId := removeCmd.Int("card-location-id", -1, "The card location to remove the card at.") + + removeCmd.Parse(flag.Args()[1:]) + + if *cardLocationId == -1 { + log.Fatal(errors.New("No CardLocationId given.")) + } + + location, err := logic.GetCardAtLocation(db, *cardLocationId) + logic.Check(err) + + err = logic.RemoveFromStorage(db, location) + logic.Check(err) + + break + case ReplaceSubcommand: + replaceCmd := flag.NewFlagSet(ReplaceSubcommand, flag.ExitOnError) + + cardLocationId := replaceCmd.Int("card-location-id", -1, "The card location to replace the card at.") + cardPrintingId := replaceCmd.String("card-printing-id", "", "The card printing to put at the specified location.") + + replaceCmd.Parse(flag.Args()[1:]) + + if *cardLocationId == -1 { + log.Fatal(errors.New("No CardLocationId given.")) + } + if *cardPrintingId == "" { + log.Fatal(errors.New("No CardPrintingId given.")) + } + + err := logic.Replace(db, *cardLocationId, *cardPrintingId) + logic.Check(err) + break case DeckSubcommand: deckCmd := flag.NewFlagSet(DeckSubcommand, flag.ExitOnError) deckCmd.Parse(flag.Args()[1:])