Update code to use Cardtrader blueprint database

This commit is contained in:
The Magician 2025-03-26 12:51:27 +00:00
parent 99ba02ef05
commit 62c7e621d5
9 changed files with 130 additions and 116 deletions

View File

@ -1,4 +1,4 @@
cache/ cache/
config.development.json config.dev.json
config.production.json config.prod.json
sevenkeys.sql sevenkeys.sql

View File

@ -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
}

View File

@ -72,3 +72,15 @@ func GetAllCardtraderBlueprints(db *sql.DB) ([]cardtrader.Blueprint, error) {
return blueprints, nil 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
}

View File

@ -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
}

View File

@ -71,7 +71,7 @@ func GetStorageAreaTypeById(db *sql.DB, storageAreaId int) (string, error) {
} }
func GetNextEmptySlotInBinder(db *sql.DB, storageAreaId int) (int, 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 var emptySlotId int
err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId) err := db.QueryRow(query, storageAreaId).Scan(&emptySlotId)

View File

@ -7,7 +7,7 @@ import (
) )
type DelverLensCard struct { type DelverLensCard struct {
ScryfallID string ScryfallId string
IsFoil bool IsFoil bool
} }
@ -38,7 +38,7 @@ func ParseExportFile(filename string) ([]DelverLensCard, error) {
} }
card := DelverLensCard{ card := DelverLensCard{
ScryfallID: record[0], ScryfallId: record[0],
IsFoil: record[1] == "Foil", IsFoil: record[1] == "Foil",
} }
cards = append(cards, card) cards = append(cards, card)

View File

@ -2,25 +2,27 @@ package logic
import ( import (
"database/sql" "database/sql"
"log"
"sevenkeys/database" "sevenkeys/database"
"sevenkeys/delverlens" "sevenkeys/delverlens"
) )
func ImportDelverLensCards(db *sql.DB, cards []delverlens.DelverLensCard, storageAreaId int) error { func ImportDelverLensCards(db *sql.DB, cards []delverlens.DelverLensCard, storageAreaId int) error {
for _, card := range cards { for _, card := range cards {
var cardPrintingId string cardtraderBlueprintId, err := database.GetCardtraderBlueprintIdByScryfallId(db, card.ScryfallId)
if card.IsFoil { if err == sql.ErrNoRows {
cardPrintingId = card.ScryfallID + "f" log.Printf("import: No Cardtrader product found for Scryfall ID: %s, inserting placeholder\n", card.ScryfallId)
} else { cardtraderBlueprintId = -1
cardPrintingId = card.ScryfallID + "n" } else if err != nil {
return err
} }
cardLocation := database.CardLocation{ productLocation := database.ProductLocation{
CardPrintingId: cardPrintingId, CardtraderBlueprintId: cardtraderBlueprintId,
StorageAreaId: storageAreaId, StorageAreaId: storageAreaId,
} }
StoreCard(db, cardLocation) StoreProduct(db, productLocation)
} }
return nil return nil

View File

@ -59,15 +59,15 @@ func GetStorageAreaSearchOptions(db *sql.DB) (StorageAreaSearchOptions, error) {
return options, nil return options, nil
} }
func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) { func storeAfterLastCard(db *sql.DB, productLocation database.ProductLocation) (int64, error) {
lastPosition, err := database.GetLastPositionInStorageArea(db, cardLocation.StorageAreaId) lastPosition, err := database.GetLastPositionInStorageArea(db, productLocation.StorageAreaId)
if err != nil { if err != nil {
return -1, err 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 { if err != nil {
return -1, err return -1, err
} }
@ -75,9 +75,9 @@ func storeAfterLastCard(db *sql.DB, cardLocation database.CardLocation) (int64,
return id, nil return id, nil
} }
func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocationId int) error { func storeInEmptySlot(db *sql.DB, productLocation database.ProductLocation, productLocationId int) error {
cardLocation.Id = cardLocationId productLocation.Id = productLocationId
err := database.InsertCardInExistingLocation(db, cardLocation) err := database.InsertProductInExistingLocation(db, productLocation)
if err != nil { if err != nil {
return err return err
} }
@ -85,17 +85,17 @@ func storeInEmptySlot(db *sql.DB, cardLocation database.CardLocation, cardLocati
return nil return nil
} }
func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) { func StoreProduct(db *sql.DB, productLocation database.ProductLocation) (int64, error) {
storageAreaType, err := database.GetStorageAreaTypeById(db, cardLocation.StorageAreaId) storageAreaType, err := database.GetStorageAreaTypeById(db, productLocation.StorageAreaId)
if err != nil { if err != nil {
return -1, err return -1, err
} }
if storageAreaType == database.StorageAreaTypeBinder { if storageAreaType == database.StorageAreaTypeBinder {
nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, cardLocation.StorageAreaId) nextEmptySlotId, err := database.GetNextEmptySlotInBinder(db, productLocation.StorageAreaId)
if err == database.ErrNoEmptySlotsInBinder { if err == database.ErrNoEmptySlotsInBinder {
id, err := storeAfterLastCard(db, cardLocation) id, err := storeAfterLastCard(db, productLocation)
if err != nil { if err != nil {
return -1, err return -1, err
} }
@ -104,7 +104,7 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
} else if err != nil { } else if err != nil {
return -1, err return -1, err
} else { } else {
err = storeInEmptySlot(db, cardLocation, nextEmptySlotId) err = storeInEmptySlot(db, productLocation, nextEmptySlotId)
if err != nil { if err != nil {
return -1, err return -1, err
} }
@ -113,7 +113,7 @@ func StoreCard(db *sql.DB, cardLocation database.CardLocation) (int64, error) {
return int64(nextEmptySlotId), nil return int64(nextEmptySlotId), nil
} }
id, err := storeAfterLastCard(db, cardLocation) id, err := storeAfterLastCard(db, productLocation)
if err != nil { if err != nil {
return -1, err return -1, err
} }
@ -121,11 +121,11 @@ 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 { func Replace(db *sql.DB, productLocationId int, cardtraderBlueprintId int) error {
cardLocation := database.CardLocation{ productLocation := database.ProductLocation{
Id: cardLocationId, Id: productLocationId,
CardPrintingId: cardPrintingId, CardtraderBlueprintId: cardtraderBlueprintId,
} }
return database.InsertCardInExistingLocation(db, cardLocation) return database.InsertProductInExistingLocation(db, productLocation)
} }

View File

@ -26,6 +26,8 @@ const (
) )
func main() { func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
var profile string var profile string
flag.StringVar(&profile, "profile", "development", "The database profile to use.") flag.StringVar(&profile, "profile", "development", "The database profile to use.")
@ -60,9 +62,9 @@ func main() {
break break
case StoreSubcommand: case StoreSubcommand:
storeCmd := flag.NewFlagSet(StoreSubcommand, flag.ExitOnError) 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.") "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:]) storeCmd.Parse(flag.Args()[1:])
@ -72,14 +74,14 @@ func main() {
os.Exit(1) os.Exit(1)
} }
cardLocation := database.CardLocation{ productLocation := database.ProductLocation{
CardPrintingId: *id, CardtraderBlueprintId: *id,
StorageAreaId: storageAreaId, StorageAreaId: storageAreaId,
} }
cardLocationId, err := logic.StoreCard(db, cardLocation) productLocationId, err := logic.StoreProduct(db, productLocation)
logic.Check(err) logic.Check(err)
fmt.Printf("%d\n", cardLocationId) fmt.Printf("%d\n", productLocationId)
break break
case ImportSubcommand: case ImportSubcommand:
@ -147,8 +149,8 @@ func main() {
case AddSubcommand: case AddSubcommand:
addCmd := flag.NewFlagSet(AddSubcommand, flag.ExitOnError) addCmd := flag.NewFlagSet(AddSubcommand, flag.ExitOnError)
cardPrintingId := addCmd.String("card-printing-id", "", "The ID of the card printing to add to storage.") cardtraderBlueprintId := addCmd.Int("blueprint-id", -1, "The Cardtrader Bluepritn ID of the product to add to storage.")
storageArea := addCmd.String("storagearea", "", storageArea := addCmd.String("storage-area", "",
"The name of the StorageArea where cards should be imported.") "The name of the StorageArea where cards should be imported.")
addCmd.Parse(flag.Args()[1:]) addCmd.Parse(flag.Args()[1:])
@ -160,11 +162,11 @@ func main() {
} }
logic.Check(err) logic.Check(err)
cardLocation := database.CardLocation{ productLocation := database.ProductLocation{
CardPrintingId: *cardPrintingId, CardtraderBlueprintId: *cardtraderBlueprintId,
StorageAreaId: storageAreaId, StorageAreaId: storageAreaId,
} }
logic.StoreCard(db, cardLocation) logic.StoreProduct(db, productLocation)
break break
case RemoveSubcommand: case RemoveSubcommand:
removeCmd := flag.NewFlagSet(RemoveSubcommand, flag.ExitOnError) removeCmd := flag.NewFlagSet(RemoveSubcommand, flag.ExitOnError)
@ -188,18 +190,15 @@ func main() {
replaceCmd := flag.NewFlagSet(ReplaceSubcommand, flag.ExitOnError) replaceCmd := flag.NewFlagSet(ReplaceSubcommand, flag.ExitOnError)
cardLocationId := replaceCmd.Int("card-location-id", -1, "The card location to replace the card at.") 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:]) replaceCmd.Parse(flag.Args()[1:])
if *cardLocationId == -1 { if *cardLocationId == -1 {
log.Fatal(errors.New("No CardLocationId given.")) 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) logic.Check(err)
break break
default: default: