Fix search-storage command and add remove command

This commit is contained in:
The Magician 2024-10-29 22:07:06 +00:00
parent 0dd67f3bb8
commit b30525c2d4
5 changed files with 88 additions and 10 deletions

View File

@ -20,6 +20,33 @@ type LocateCardResult struct {
Position int 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) { func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error) {
var results []LocateCardResult var results []LocateCardResult

View File

@ -30,6 +30,17 @@ func GetBinderLocationDescription(position int) string {
return fmt.Sprintf(" on page %d in %s slot %d", page, frontOrBack, slot) 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) { func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]database.LocateCardResult, error) {
results, err := database.GetLocateResults(db, cardNames) results, err := database.GetLocateResults(db, cardNames)
if err != nil { if err != nil {

View File

@ -71,16 +71,7 @@ func GetAllStorageSearchOptions(db *sql.DB) (StorageSearchOptions, error) {
} }
for _, storedCard := range storedCards { for _, storedCard := range storedCards {
searchString := fmt.Sprintf("%s (%s %s) [%s]", storedCard.CardName, storedCard.SetCode, storedCard.CollectorNumber, storedCard.Language) searchString := GetLocationDescription(storedCard)
if storedCard.IsFoil {
searchString += " FOIL"
}
if storedCard.IsPromo {
searchString += " PROMO"
}
searchOptions[searchString] = storedCard.CardLocationId searchOptions[searchString] = storedCard.CardLocationId
} }

View File

@ -120,3 +120,12 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
return id, nil 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)
}

View File

@ -2,8 +2,10 @@ package main
import ( import (
"bufio" "bufio"
"errors"
"flag" "flag"
"fmt" "fmt"
"log"
"os" "os"
"sevenkeys/database" "sevenkeys/database"
"sevenkeys/delverlens" "sevenkeys/delverlens"
@ -35,6 +37,8 @@ const (
ImportSubcommand string = "import" ImportSubcommand string = "import"
SearchPrintingsSubcommand string = "search-printings" SearchPrintingsSubcommand string = "search-printings"
SearchStorageSubcommand string = "search-storage" SearchStorageSubcommand string = "search-storage"
RemoveSubcommand string = "remove"
ReplaceSubcommand string = "replace"
DeckSubcommand string = "deck" DeckSubcommand string = "deck"
) )
@ -185,6 +189,42 @@ func main() {
logic.Check(err) logic.Check(err)
fmt.Println(id) fmt.Println(id)
break 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: case DeckSubcommand:
deckCmd := flag.NewFlagSet(DeckSubcommand, flag.ExitOnError) deckCmd := flag.NewFlagSet(DeckSubcommand, flag.ExitOnError)
deckCmd.Parse(flag.Args()[1:]) deckCmd.Parse(flag.Args()[1:])