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

View File

@ -26,22 +26,27 @@ func GetLastPositionInStorageArea(db *sql.DB, storageAreaId int) (int, error) {
return lastPosition, nil return lastPosition, nil
} }
func InsertCardLocation(db *sql.DB, storageLocation CardLocation) error { func InsertCardLocation(db *sql.DB, storageLocation CardLocation) (int64, error) {
query := `INSERT INTO CardLocation query := `INSERT INTO CardLocation
(CardPrintingId, StorageAreaId, Position) (CardPrintingId, StorageAreaId, Position)
VALUES (?, ?, ?);` VALUES (?, ?, ?);`
insert, err := db.Prepare(query) insert, err := db.Prepare(query)
if err != nil { 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 { 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 { func InsertCardInExistingLocation(db *sql.DB, cardLocation CardLocation) error {

View File

@ -59,20 +59,20 @@ func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
return options, nil 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) lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId)
if err != nil { if err != nil {
return err return -1, err
} }
cardLocation.Position = lastPosition + 1 cardLocation.Position = lastPosition + 1
err = database.InsertCardLocation(db, cardLocation) id, err := database.InsertCardLocation(db, cardLocation)
if err != nil { if err != nil {
return err return -1, err
} }
return nil return id, nil
} }
func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error { 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 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) storageAreaType, err := database.GetStorageAreaTypeById(db, cardLocation.StorageAreaId)
if err != nil { if err != nil {
return err return -1, err
} }
if storageAreaType == database.StorageAreaTypeBinder { if storageAreaType == database.StorageAreaTypeBinder {
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId) nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId)
if err == database.ErrNoEmptySlotsInBinder { if err == database.ErrNoEmptySlotsInBinder {
err = storeAfterLastCard(db, cardLocation) id, err := storeAfterLastCard(db, cardLocation)
if err != nil { if err != nil {
return err return -1, err
} }
return id, nil
} else if err != nil { } else if err != nil {
return err return -1, err
} else { } else {
err = storeInEmptySlot(db, cardLocation, nextEmptySlotId) err = storeInEmptySlot(db, cardLocation, nextEmptySlotId)
if err != nil { 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 { if err != nil {
return err return -1, err
} }
return nil return id, nil
} }

View File

@ -13,6 +13,7 @@ import (
const ( const (
UpdateSubcommand string = "update" UpdateSubcommand string = "update"
CreateStorageAreaSubcommand string = "createstorage" CreateStorageAreaSubcommand string = "createstorage"
StoreSubcommand string = "store"
ImportSubcommand string = "import" ImportSubcommand string = "import"
SearchSubcommand string = "search" SearchSubcommand string = "search"
DeckSubcommand string = "deck" DeckSubcommand string = "deck"
@ -58,6 +59,29 @@ func main() {
err := logic.CreateStorageArea(db, storageArea) err := logic.CreateStorageArea(db, storageArea)
logic.Check(err) 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 break
case ImportSubcommand: case ImportSubcommand:
importCmd := flag.NewFlagSet(ImportSubcommand, flag.ExitOnError) importCmd := flag.NewFlagSet(ImportSubcommand, flag.ExitOnError)
@ -82,13 +106,15 @@ func main() {
case SearchSubcommand: case SearchSubcommand:
searchOptions, err := logic.GetAllStorageSearchOptions(db) searchOptions, err := logic.GetAllStorageSearchOptions(db)
logic.Check(err) logic.Check(err)
logic.GenericSearch(searchOptions) id, _, err := logic.GenericSearch(searchOptions)
logic.Check(err)
fmt.Println(id)
break 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:])
filename := deckCmd.Args()[0] //filename := deckCmd.Args()[0]
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])