Implement search command

This commit is contained in:
The Magician 2024-10-04 13:44:22 +01:00
parent c0c290916a
commit f7e0a713f6
3 changed files with 76 additions and 4 deletions

View File

@ -61,3 +61,39 @@ func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error
return results, nil 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
}

View File

@ -30,7 +30,7 @@ type SearchCriteria struct {
type InsertSearchOptions map[string]string type InsertSearchOptions map[string]string
func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearchOptions, error) { 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) cardPrintings, err := database.GetAllCardPrintings(db)
if err != nil { if err != nil {
@ -61,6 +61,33 @@ func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearc
return searchOptions, err 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) { func GenericSearch[pk string | int](options map[string]pk) (pk, string, error) {
var value pk var value pk

View File

@ -14,6 +14,7 @@ import (
const ( const (
UpdateSubcommand string = "update" UpdateSubcommand string = "update"
ImportSubcommand string = "import" ImportSubcommand string = "import"
SearchSubcommand string = "search"
) )
func main() { func main() {
@ -29,15 +30,21 @@ func main() {
// TODO: Decide in what form we need to retain this functionality if any // TODO: Decide in what form we need to retain this functionality if any
//cli.MainCliLoop(db) //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.") 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 { if len(flag.Args()) == 0 {
fmt.Fprintln(os.Stderr, "Please specify a subcommand.") fmt.Fprintln(os.Stderr, "Please specify a subcommand.")
os.Exit(1) os.Exit(1)
} }
switch flag.Args()[0] { switch flag.Args()[0] {
case UpdateSubcommand:
cli.RunUpdateCheck(db)
break
case ImportSubcommand: case ImportSubcommand:
importCmd.Parse(flag.Args()[1:]) importCmd.Parse(flag.Args()[1:])
@ -54,8 +61,10 @@ func main() {
err = logic.ImportDelverLensCards(db, delverLensCards, storageAreaId) err = logic.ImportDelverLensCards(db, delverLensCards, storageAreaId)
logic.Check(err) logic.Check(err)
break break
case UpdateSubcommand: case SearchSubcommand:
cli.RunUpdateCheck(db) searchOptions, err := logic.GetAllStorageSearchOptions(db)
logic.Check(err)
logic.GenericSearch(searchOptions)
break break
default: default:
fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1]) fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])