From f7e0a713f629f603cf04d6815d290b551a0dcfea Mon Sep 17 00:00:00 2001 From: The Magician Date: Fri, 4 Oct 2024 13:44:22 +0100 Subject: [PATCH] Implement search command --- sevenkeys/database/locate.go | 36 ++++++++++++++++++++++++++++++++++++ sevenkeys/logic/search.go | 29 ++++++++++++++++++++++++++++- sevenkeys/main.go | 15 ++++++++++++--- 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/sevenkeys/database/locate.go b/sevenkeys/database/locate.go index 82c685b..6b9ae61 100644 --- a/sevenkeys/database/locate.go +++ b/sevenkeys/database/locate.go @@ -61,3 +61,39 @@ func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error return results, nil } + +func GetAllStoredCards(db *sql.DB) ([]LocateCardResult, error) { + var results []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` + rows, err := db.Query(query) + defer rows.Close() + if err != nil { + return results, err + } + + var result LocateCardResult + for rows.Next() { + err := rows.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 results, err + } + + results = append(results, result) + } + + return results, nil +} diff --git a/sevenkeys/logic/search.go b/sevenkeys/logic/search.go index a15b252..ec89ea1 100644 --- a/sevenkeys/logic/search.go +++ b/sevenkeys/logic/search.go @@ -30,7 +30,7 @@ type SearchCriteria struct { type InsertSearchOptions map[string]string func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearchOptions, error) { - var searchOptions InsertSearchOptions = make(map[string]string) + var searchOptions InsertSearchOptions = make(InsertSearchOptions) cardPrintings, err := database.GetAllCardPrintings(db) if err != nil { @@ -61,6 +61,33 @@ func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearc return searchOptions, err } +type StorageSearchOptions map[string]int + +func GetAllStorageSearchOptions(db *sql.DB) (StorageSearchOptions, error) { + var searchOptions StorageSearchOptions = make(StorageSearchOptions) + + storedCards, err := database.GetAllStoredCards(db) + if err != nil { + return searchOptions, nil + } + + 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" + } + + searchOptions[searchString] = storedCard.CardLocationId + } + + return searchOptions, nil +} + func GenericSearch[pk string | int](options map[string]pk) (pk, string, error) { var value pk diff --git a/sevenkeys/main.go b/sevenkeys/main.go index 7779962..7a6edc2 100644 --- a/sevenkeys/main.go +++ b/sevenkeys/main.go @@ -14,6 +14,7 @@ import ( const ( UpdateSubcommand string = "update" ImportSubcommand string = "import" + SearchSubcommand string = "search" ) func main() { @@ -29,15 +30,21 @@ func main() { // TODO: Decide in what form we need to retain this functionality if any //cli.MainCliLoop(db) - importCmd := flag.NewFlagSet("import", flag.ExitOnError) + importCmd := flag.NewFlagSet(ImportSubcommand, flag.ExitOnError) storageArea := importCmd.String("storagearea", "", "The name of the StorageArea where cards should be imported.") + //searchCmd := flag.NewFlagSet(SearchSubcommand, flag.ExitOnError) + //name := searchCmd.String("name", "", "The card name to search for.") + if len(flag.Args()) == 0 { fmt.Fprintln(os.Stderr, "Please specify a subcommand.") os.Exit(1) } switch flag.Args()[0] { + case UpdateSubcommand: + cli.RunUpdateCheck(db) + break case ImportSubcommand: importCmd.Parse(flag.Args()[1:]) @@ -54,8 +61,10 @@ func main() { err = logic.ImportDelverLensCards(db, delverLensCards, storageAreaId) logic.Check(err) break - case UpdateSubcommand: - cli.RunUpdateCheck(db) + case SearchSubcommand: + searchOptions, err := logic.GetAllStorageSearchOptions(db) + logic.Check(err) + logic.GenericSearch(searchOptions) break default: fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])