Update code to use Cardtrader blueprint database
This commit is contained in:
parent
99ba02ef05
commit
62c7e621d5
|
@ -1,4 +1,4 @@
|
|||
cache/
|
||||
config.development.json
|
||||
config.production.json
|
||||
config.dev.json
|
||||
config.prod.json
|
||||
sevenkeys.sql
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
productLocation := database.ProductLocation{
|
||||
CardtraderBlueprintId: cardtraderBlueprintId,
|
||||
StorageAreaId: storageAreaId,
|
||||
}
|
||||
|
||||
StoreCard(db, cardLocation)
|
||||
StoreProduct(db, productLocation)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
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,
|
||||
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:
|
||||
|
|
Loading…
Reference in New Issue