diff --git a/sevenkeys/.gitignore b/sevenkeys/.gitignore index ecf93a9..969fad0 100644 --- a/sevenkeys/.gitignore +++ b/sevenkeys/.gitignore @@ -1,4 +1,4 @@ cache/ -config.development.json -config.production.json +config.dev.json +config.prod.json sevenkeys.sql diff --git a/sevenkeys/database/cardstoragelocation.go b/sevenkeys/database/cardstoragelocation.go deleted file mode 100644 index 747d83c..0000000 --- a/sevenkeys/database/cardstoragelocation.go +++ /dev/null @@ -1,66 +0,0 @@ -package database - -import ( - "database/sql" -) - -type CardLocation struct { - Id int - CardPrintingId string - StorageAreaId int - Position int -} - -func GetLastPositionInStorageArea(db *sql.DB, storageAreaId int) (int, error) { - query := "SELECT Position FROM CardLocation WHERE StorageAreaId = ? ORDER BY Position DESC LIMIT 1;" - - var lastPosition int - err := db.QueryRow(query, storageAreaId).Scan(&lastPosition) - - if err == sql.ErrNoRows { - return 0, nil - } else if err != nil { - return 0, err - } - - return lastPosition, nil -} - -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 -1, err - } - - result, err := insert.Exec(storageLocation.CardPrintingId, storageLocation.StorageAreaId, storageLocation.Position) - if err != nil { - return -1, err - } - - id, err := result.LastInsertId() - if err != nil { - return -1, err - } - - return id, nil -} - -func InsertCardInExistingLocation(db *sql.DB, cardLocation CardLocation) error { - query := `UPDATE CardLocation SET CardPrintingId = ? WHERE Id = ?;` - - update, err := db.Prepare(query) - if err != nil { - return err - } - - _, err = update.Exec(cardLocation.CardPrintingId, cardLocation.Id) - if err != nil { - return err - } - - return nil -} diff --git a/sevenkeys/database/cardtraderblueprint.go b/sevenkeys/database/cardtraderblueprint.go index cefe6b5..bda45af 100644 --- a/sevenkeys/database/cardtraderblueprint.go +++ b/sevenkeys/database/cardtraderblueprint.go @@ -72,3 +72,15 @@ func GetAllCardtraderBlueprints(db *sql.DB) ([]cardtrader.Blueprint, error) { return blueprints, nil } + +func GetCardtraderBlueprintIdByScryfallId(db *sql.DB, scryfallId string) (int, error) { + query := `SELECT Id FROM CardtraderBlueprint WHERE ScryfallCardId = ?;` + + var cardtraderBlueprintId int + err := db.QueryRow(query, scryfallId).Scan(&cardtraderBlueprintId) + if err != nil { + return cardtraderBlueprintId, err + } + + return cardtraderBlueprintId, nil +} diff --git a/sevenkeys/database/productlocation.go b/sevenkeys/database/productlocation.go new file mode 100644 index 0000000..5f9b3b3 --- /dev/null +++ b/sevenkeys/database/productlocation.go @@ -0,0 +1,67 @@ +package database + +import ( + "database/sql" +) + +type ProductLocation struct { + Id int + CardtraderBlueprintId int + StorageAreaId int + Position int + CardtraderProductId int +} + +func GetLastPositionInStorageArea(db *sql.DB, storageAreaId int) (int, error) { + query := "SELECT Position FROM ProductLocation WHERE StorageAreaId = ? ORDER BY Position DESC LIMIT 1;" + + var lastPosition int + err := db.QueryRow(query, storageAreaId).Scan(&lastPosition) + + if err == sql.ErrNoRows { + return 0, nil + } else if err != nil { + return 0, err + } + + return lastPosition, nil +} + +func InsertProductLocation(db *sql.DB, productLocation ProductLocation) (int64, error) { + query := `INSERT INTO ProductLocation + (CardtraderBlueprintId, StorageAreaId, Position) + VALUES (?, ?, ?);` + + insert, err := db.Prepare(query) + if err != nil { + return -1, err + } + + result, err := insert.Exec(productLocation.CardtraderBlueprintId, productLocation.StorageAreaId, productLocation.Position) + if err != nil { + return -1, err + } + + id, err := result.LastInsertId() + if err != nil { + return -1, err + } + + return id, nil +} + +func InsertProductInExistingLocation(db *sql.DB, productLocation ProductLocation) error { + query := `UPDATE ProductLocation SET CardtraderBlueprintId = ? WHERE Id = ?;` + + update, err := db.Prepare(query) + if err != nil { + return err + } + + _, err = update.Exec(productLocation.CardtraderBlueprintId, productLocation.Id) + if err != nil { + return err + } + + return nil +} diff --git a/sevenkeys/database/storagearea.go b/sevenkeys/database/storagearea.go index a55119f..86ce8e1 100644 --- a/sevenkeys/database/storagearea.go +++ b/sevenkeys/database/storagearea.go @@ -71,7 +71,7 @@ func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) { } func GetNextEmptySlotInBinder(db *sql.DB, storageAreaId int) (int, error) { - query := `SELECT Id FROM CardLocation WHERE CardPrintingId IS NULL AND StorageAreaId = ? ORDER BY Position ASC LIMIT 1;` + query := `SELECT Id FROM ProductLocation WHERE CardtraderBlueprintId IS NULL AND StorageAreaId = ? ORDER BY Position ASC LIMIT 1;` var emptySlotId int err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId) diff --git a/sevenkeys/delverlens/delverlens.go b/sevenkeys/delverlens/delverlens.go index 9cc5282..fd71ec6 100644 --- a/sevenkeys/delverlens/delverlens.go +++ b/sevenkeys/delverlens/delverlens.go @@ -7,7 +7,7 @@ import ( ) type DelverLensCard struct { - ScryfallID string + ScryfallId string IsFoil bool } @@ -38,7 +38,7 @@ func ParseExportFile(filename string) ([]DelverLensCard, error) { } card := DelverLensCard{ - ScryfallID: record[0], + ScryfallId: record[0], IsFoil: record[1] == "Foil", } cards = append(cards, card) diff --git a/sevenkeys/logic/import.go b/sevenkeys/logic/import.go index 5352a29..f7d1f8f 100644 --- a/sevenkeys/logic/import.go +++ b/sevenkeys/logic/import.go @@ -2,25 +2,27 @@ package logic import ( "database/sql" + "log" "sevenkeys/database" "sevenkeys/delverlens" ) func ImportDelverLensCards(db *sql.DB, cards []delverlens.DelverLensCard, storageAreaId int) error { for _, card := range cards { - var cardPrintingId string - if card.IsFoil { - cardPrintingId = card.ScryfallID + "f" - } else { - cardPrintingId = card.ScryfallID + "n" + cardtraderBlueprintId, err := database.GetCardtraderBlueprintIdByScryfallId(db, card.ScryfallId) + if err == sql.ErrNoRows { + log.Printf("import: No Cardtrader product found for Scryfall ID: %s, inserting placeholder\n", card.ScryfallId) + cardtraderBlueprintId = -1 + } else if err != nil { + return err } - cardLocation := database.CardLocation{ - CardPrintingId: cardPrintingId, - StorageAreaId: storageAreaId, + productLocation := database.ProductLocation{ + CardtraderBlueprintId: cardtraderBlueprintId, + StorageAreaId: storageAreaId, } - StoreCard(db, cardLocation) + StoreProduct(db, productLocation) } return nil diff --git a/sevenkeys/logic/storage.go b/sevenkeys/logic/storage.go index f9fca57..7f81864 100644 --- a/sevenkeys/logic/storage.go +++ b/sevenkeys/logic/storage.go @@ -59,15 +59,15 @@ func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) { return options, nil } -func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) { - lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId) +func storeAfterLastCard(db *sql.DB, productLocation database.ProductLocation) (int64, error) { + lastPosition, err := database.GetLastPositionInStorageArea(db, productLocation.StorageAreaId) if err != nil { return -1, err } - cardLocation.Position = lastPosition + 1 + productLocation.Position = lastPosition + 1 - id, err := database.InsertCardLocation(db, cardLocation) + id, err := database.InsertProductLocation(db, productLocation) if err != nil { return -1, err } @@ -75,9 +75,9 @@ func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) (int64, return id, nil } -func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error { - cardLocation.Id = cardLocationId - err := database.InsertCardInExistingLocation(db, cardLocation) +func storeInEmptySlot(db *sql.DB, productLocation database.ProductLocation, productLocationId int) error { + productLocation.Id = productLocationId + err := database.InsertProductInExistingLocation(db, productLocation) if err != nil { return err } @@ -85,17 +85,17 @@ func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocati return nil } -func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) { - storageAreaType, err := database.GetStorageAreaTypeById(db, cardLocation.StorageAreaId) +func StoreProduct(db *sql.DB, productLocation database.ProductLocation) (int64, error) { + storageAreaType, err := database.GetStorageAreaTypeById(db, productLocation.StorageAreaId) if err != nil { return -1, err } if storageAreaType == database.StorageAreaTypeBinder { - nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId) + nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, productLocation.StorageAreaId) if err == database.ErrNoEmptySlotsInBinder { - id, err := storeAfterLastCard(db, cardLocation) + id, err := storeAfterLastCard(db, productLocation) if err != nil { return -1, err } @@ -104,7 +104,7 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) { } else if err != nil { return -1, err } else { - err = storeInEmptySlot(db, cardLocation, nextEmptySlotId) + err = storeInEmptySlot(db, productLocation, nextEmptySlotId) if err != nil { return -1, err } @@ -113,7 +113,7 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) { return int64(nextEmptySlotId), nil } - id, err := storeAfterLastCard(db, cardLocation) + id, err := storeAfterLastCard(db, productLocation) if err != nil { return -1, err } @@ -121,11 +121,11 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) { return id, nil } -func Replace(db *sql.DB, cardLocationId int, cardPrintingId string) error { - cardLocation := database.CardLocation{ - Id: cardLocationId, - CardPrintingId: cardPrintingId, +func Replace(db *sql.DB, productLocationId int, cardtraderBlueprintId int) error { + productLocation := database.ProductLocation{ + Id: productLocationId, + CardtraderBlueprintId: cardtraderBlueprintId, } - return database.InsertCardInExistingLocation(db, cardLocation) + return database.InsertProductInExistingLocation(db, productLocation) } diff --git a/sevenkeys/main.go b/sevenkeys/main.go index 025617e..1909169 100644 --- a/sevenkeys/main.go +++ b/sevenkeys/main.go @@ -26,6 +26,8 @@ const ( ) func main() { + log.SetFlags(log.LstdFlags | log.Lshortfile) + var profile string flag.StringVar(&profile, "profile", "development", "The database profile to use.") @@ -60,9 +62,9 @@ func main() { break case StoreSubcommand: storeCmd := flag.NewFlagSet(StoreSubcommand, flag.ExitOnError) - storageArea := storeCmd.String("storagearea", "", + id := storeCmd.Int("id", -1, "The CardPrintingId of the card to store.") + storageArea := storeCmd.String("storage-area", "", "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:]) @@ -72,14 +74,14 @@ func main() { os.Exit(1) } - cardLocation := database.CardLocation{ - CardPrintingId: *id, - StorageAreaId: storageAreaId, + productLocation := database.ProductLocation{ + CardtraderBlueprintId: *id, + StorageAreaId: storageAreaId, } - cardLocationId, err := logic.StoreCard(db, cardLocation) + productLocationId, err := logic.StoreProduct(db, productLocation) logic.Check(err) - fmt.Printf("%d\n", cardLocationId) + fmt.Printf("%d\n", productLocationId) break case ImportSubcommand: @@ -147,8 +149,8 @@ func main() { case AddSubcommand: addCmd := flag.NewFlagSet(AddSubcommand, flag.ExitOnError) - cardPrintingId := addCmd.String("card-printing-id", "", "The ID of the card printing to add to storage.") - storageArea := addCmd.String("storagearea", "", + cardtraderBlueprintId := addCmd.Int("blueprint-id", -1, "The Cardtrader Bluepritn ID of the product to add to storage.") + storageArea := addCmd.String("storage-area", "", "The name of the StorageArea where cards should be imported.") addCmd.Parse(flag.Args()[1:]) @@ -160,11 +162,11 @@ func main() { } logic.Check(err) - cardLocation := database.CardLocation{ - CardPrintingId: *cardPrintingId, - StorageAreaId: storageAreaId, + productLocation := database.ProductLocation{ + CardtraderBlueprintId: *cardtraderBlueprintId, + StorageAreaId: storageAreaId, } - logic.StoreCard(db, cardLocation) + logic.StoreProduct(db, productLocation) break case RemoveSubcommand: removeCmd := flag.NewFlagSet(RemoveSubcommand, flag.ExitOnError) @@ -188,18 +190,15 @@ func main() { 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.") + cardtraderBlueprintId := replaceCmd.Int("blueprint-id", -1, "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) + err := logic.Replace(db, *cardLocationId, *cardtraderBlueprintId) logic.Check(err) break default: