Add "store" command to add cards individually

This commit is contained in:
The Magician 2024-10-10 15:43:56 +01:00
parent 6a4ff5dffd
commit 2ef339bbce
4 changed files with 66 additions and 31 deletions

View File

@ -21,15 +21,17 @@ func MainCliLoop(db *sql.DB) {
Promo: logic.Either,
Language: "en",
}
var insertSearchOptions logic.InsertSearchOptions
//var insertSearchOptions logic.InsertSearchOptions
// TODO: Add the ability to modify this
/*
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.True,
Promo: logic.Either,
Language: "en",
}
*/
for {
ShowSplashScreen()
@ -41,7 +43,7 @@ func MainCliLoop(db *sql.DB) {
command = GetStringResponse("SEVENKEYS $")
var err error
//var err error
switch command {
/*
@ -96,7 +98,7 @@ func MainCliLoop(db *sql.DB) {
break
*/
case "i", "insert":
err := logic.StoreCard(db, cardLocation)
_, err := logic.StoreCard(db, cardLocation)
logic.Check(err)
copiesInserted++
break

View File

@ -26,22 +26,27 @@ func GetLastPositionInStorageArea(db *sql.DB, storageAreaId int) (int, error) {
return lastPosition, nil
}
func InsertCardLocation(db *sql.DB, storageLocation CardLocation) error {
func InsertCardLocation(db *sql.DB, storageLocation CardLocation) (int64, error) {
query := `INSERT INTO CardLocation
(CardPrintingId, StorageAreaId, Position)
VALUES (?, ?, ?);`
insert, err := db.Prepare(query)
if err != nil {
return err
return -1, err
}
_, err = insert.Exec(storageLocation.CardPrintingId, storageLocation.StorageAreaId, storageLocation.Position)
result, err := insert.Exec(storageLocation.CardPrintingId, storageLocation.StorageAreaId, storageLocation.Position)
if err != nil {
return err
return -1, err
}
return nil
id, err := result.LastInsertId()
if err != nil {
return -1, err
}
return id, nil
}
func InsertCardInExistingLocation(db *sql.DB, cardLocation CardLocation) error {

View File

@ -59,20 +59,20 @@ func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
return options, nil
}
func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) error {
func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
if err != nil {
return err
return -1, err
}
cardLocation.Position = lastPosition + 1
err = database.InsertCardLocation(db, cardLocation)
id, err := database.InsertCardLocation(db, cardLocation)
if err != nil {
return err
return -1, err
}
return nil
return id, nil
}
func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error {
@ -85,36 +85,38 @@ func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocati
return nil
}
func StoreCard(db *sql.DB, cardLocation database.CardLocation) error {
func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
storageAreaType, err := database.GetStorageAreaTypeById(db, cardLocation.StorageAreaId)
if err != nil {
return err
return -1, err
}
if storageAreaType == database.StorageAreaTypeBinder {
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId)
if err == database.ErrNoEmptySlotsInBinder {
err = storeAfterLastCard(db, cardLocation)
id, err := storeAfterLastCard(db, cardLocation)
if err != nil {
return err
return -1, err
}
return id, nil
} else if err != nil {
return err
return -1, err
} else {
err = storeInEmptySlot(db, cardLocation, nextEmptySlotId)
if err != nil {
return err
return -1, err
}
}
return nil
return int64(nextEmptySlotId), nil
}
err = storeAfterLastCard(db, cardLocation)
id, err := storeAfterLastCard(db, cardLocation)
if err != nil {
return err
return -1, err
}
return nil
return id, nil
}

View File

@ -13,6 +13,7 @@ import (
const (
UpdateSubcommand string = "update"
CreateStorageAreaSubcommand string = "createstorage"
StoreSubcommand string = "store"
ImportSubcommand string = "import"
SearchSubcommand string = "search"
DeckSubcommand string = "deck"
@ -58,6 +59,29 @@ func main() {
err := logic.CreateStorageArea(db, storageArea)
logic.Check(err)
break
case StoreSubcommand:
storeCmd := flag.NewFlagSet(StoreSubcommand, flag.ExitOnError)
storageArea := storeCmd.String("storagearea", "",
"The name of the StorageArea the card should be inserted to.")
id := storeCmd.String("id", "", "The CardPrintingId of the card to store.")
storeCmd.Parse(flag.Args()[1:])
storageAreaId, err := logic.GetStorageAreaId(db, *storageArea)
if err == logic.ErrCouldNotGetStorageAreaId {
fmt.Fprintf(os.Stderr, "[sevenkeys] No storage area was selected, exiting.\n")
os.Exit(1)
}
cardLocation := database.CardLocation{
CardPrintingId: *id,
StorageAreaId: storageAreaId,
}
cardLocationId, err := logic.StoreCard(db, cardLocation)
logic.Check(err)
fmt.Printf("%d\n", cardLocationId)
break
case ImportSubcommand:
importCmd := flag.NewFlagSet(ImportSubcommand, flag.ExitOnError)
@ -82,13 +106,15 @@ func main() {
case SearchSubcommand:
searchOptions, err := logic.GetAllStorageSearchOptions(db)
logic.Check(err)
logic.GenericSearch(searchOptions)
id, _, err := logic.GenericSearch(searchOptions)
logic.Check(err)
fmt.Println(id)
break
case DeckSubcommand:
deckCmd := flag.NewFlagSet(DeckSubcommand, flag.ExitOnError)
deckCmd.Parse(flag.Args()[1:])
filename := deckCmd.Args()[0]
//filename := deckCmd.Args()[0]
break
default:
fmt.Fprintf(os.Stderr, "Unrecognized subcommand: %s\n", os.Args[1])