Update CardPrinting to ScryfallCard
This commit is contained in:
parent
ec6e6b7f6a
commit
d107a92b54
|
@ -1,65 +0,0 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type CardPrinting struct {
|
||||
Id string
|
||||
Name string
|
||||
SetCode string
|
||||
IsFoil bool
|
||||
IsPromo bool
|
||||
CollectorNumber string
|
||||
ImageUrl string
|
||||
Language string
|
||||
}
|
||||
|
||||
func InsertCardPrinting(db *sql.DB, cardPrinting CardPrinting) error {
|
||||
query := `INSERT INTO CardPrinting (
|
||||
Id,
|
||||
Name,
|
||||
SetCode,
|
||||
IsFoil,
|
||||
IsPromo,
|
||||
CollectorNumber,
|
||||
ImageUrl,
|
||||
Language)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
|
||||
|
||||
insert, err := db.Prepare(query)
|
||||
defer insert.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = insert.Exec(cardPrinting.Id, cardPrinting.Name, cardPrinting.SetCode, cardPrinting.IsFoil, cardPrinting.IsPromo, cardPrinting.CollectorNumber, cardPrinting.ImageUrl, cardPrinting.Language)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAllCardPrintings(db *sql.DB) ([]CardPrinting, error) {
|
||||
var cardPrintings []CardPrinting
|
||||
|
||||
query := `SELECT Id, Name, SetCode, IsFoil, IsPromo, CollectorNumber, Language FROM CardPrinting;`
|
||||
rows, err := db.Query(query)
|
||||
defer rows.Close()
|
||||
if err != nil {
|
||||
return cardPrintings, err
|
||||
}
|
||||
|
||||
var printing CardPrinting
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&printing.Id, &printing.Name, &printing.SetCode, &printing.IsFoil, &printing.IsPromo, &printing.CollectorNumber, &printing.Language)
|
||||
if err != nil {
|
||||
return cardPrintings, err
|
||||
}
|
||||
|
||||
cardPrintings = append(cardPrintings, printing)
|
||||
}
|
||||
|
||||
return cardPrintings, nil
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type ScryfallCard struct {
|
||||
Id string
|
||||
Name string
|
||||
ScryfallSetCode string
|
||||
HasFoilPrinting bool
|
||||
IsPromo bool
|
||||
CollectorNumber string
|
||||
ImageUrl string
|
||||
Language string
|
||||
}
|
||||
|
||||
func InsertScryfallCard(db *sql.DB, scryfallCard ScryfallCard) error {
|
||||
query := `INSERT INTO ScryfallCard (
|
||||
Id,
|
||||
Name,
|
||||
ScryfallSetCode,
|
||||
HasFoilPrinting,
|
||||
IsPromo,
|
||||
CollectorNumber,
|
||||
ImageUrl,
|
||||
Language)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?);`
|
||||
|
||||
insert, err := db.Prepare(query)
|
||||
defer insert.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = insert.Exec(scryfallCard.Id,
|
||||
scryfallCard.Name,
|
||||
scryfallCard.ScryfallSetCode,
|
||||
scryfallCard.HasFoilPrinting,
|
||||
scryfallCard.IsPromo,
|
||||
scryfallCard.CollectorNumber,
|
||||
scryfallCard.ImageUrl,
|
||||
scryfallCard.Language)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GetAllScryfallCards(db *sql.DB) ([]ScryfallCard, error) {
|
||||
var scryfallCards []ScryfallCard
|
||||
|
||||
query := `SELECT Id, Name, ScryfallSetCode, HasFoilPrinting, IsPromo, CollectorNumber, Language FROM CardPrinting;`
|
||||
rows, err := db.Query(query)
|
||||
defer rows.Close()
|
||||
if err != nil {
|
||||
return scryfallCards, err
|
||||
}
|
||||
|
||||
var scryfallCard ScryfallCard
|
||||
for rows.Next() {
|
||||
err := rows.Scan(&scryfallCard.Id,
|
||||
&scryfallCard.Name,
|
||||
&scryfallCard.ScryfallSetCode,
|
||||
&scryfallCard.HasFoilPrinting,
|
||||
&scryfallCard.IsPromo,
|
||||
&scryfallCard.CollectorNumber,
|
||||
&scryfallCard.Language)
|
||||
if err != nil {
|
||||
return scryfallCards, err
|
||||
}
|
||||
|
||||
scryfallCards = append(scryfallCards, scryfallCard)
|
||||
}
|
||||
|
||||
return scryfallCards, nil
|
||||
}
|
|
@ -5,30 +5,23 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteria) bool {
|
||||
if searchCriteria.SetCode != "" && !strings.Contains(printing.SetCode, searchCriteria.SetCode) {
|
||||
func filterPrinting(scryfallCard database.ScryfallCard, searchCriteria SearchCriteria) bool {
|
||||
if searchCriteria.SetCode != "" && !strings.Contains(scryfallCard.ScryfallSetCode, searchCriteria.SetCode) {
|
||||
return true
|
||||
}
|
||||
|
||||
if searchCriteria.CollectorNumber != "" && !strings.Contains(printing.CollectorNumber, searchCriteria.CollectorNumber) {
|
||||
if searchCriteria.CollectorNumber != "" && !strings.Contains(scryfallCard.CollectorNumber, searchCriteria.CollectorNumber) {
|
||||
return true
|
||||
}
|
||||
|
||||
if searchCriteria.Foil == False && printing.IsFoil {
|
||||
if searchCriteria.Promo == False && scryfallCard.IsPromo {
|
||||
return true
|
||||
}
|
||||
if searchCriteria.Foil == True && !printing.IsFoil {
|
||||
if searchCriteria.Promo == True && !scryfallCard.IsPromo {
|
||||
return true
|
||||
}
|
||||
|
||||
if searchCriteria.Promo == False && printing.IsPromo {
|
||||
return true
|
||||
}
|
||||
if searchCriteria.Promo == True && !printing.IsPromo {
|
||||
return true
|
||||
}
|
||||
|
||||
if searchCriteria.Language != "" && printing.Language != searchCriteria.Language {
|
||||
if searchCriteria.Language != "" && scryfallCard.Language != searchCriteria.Language {
|
||||
return true
|
||||
}
|
||||
|
||||
|
|
|
@ -50,14 +50,13 @@ func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]dat
|
|||
var filteredResults []database.LocateCardResult
|
||||
|
||||
for _, result := range results {
|
||||
printing := database.CardPrinting{
|
||||
SetCode: result.SetCode,
|
||||
IsFoil: result.IsFoil,
|
||||
IsPromo: result.IsPromo,
|
||||
Language: result.Language,
|
||||
scryfallCard := database.ScryfallCard{
|
||||
ScryfallSetCode: result.SetCode,
|
||||
IsPromo: result.IsPromo,
|
||||
Language: result.Language,
|
||||
}
|
||||
|
||||
filter := filterPrinting(printing, criteria)
|
||||
filter := filterPrinting(scryfallCard, criteria)
|
||||
if filter {
|
||||
continue
|
||||
}
|
||||
|
@ -77,9 +76,6 @@ func GetLocationDescription(location database.LocateCardResult) string {
|
|||
location.CollectorNumber,
|
||||
location.Language)
|
||||
|
||||
if location.IsFoil {
|
||||
description += " FOIL"
|
||||
}
|
||||
if location.IsPromo {
|
||||
description += " PROMO"
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ const (
|
|||
type SearchCriteria struct {
|
||||
SetCode string
|
||||
CollectorNumber string
|
||||
Foil Triadic
|
||||
Promo Triadic
|
||||
Language string
|
||||
}
|
||||
|
@ -31,30 +30,30 @@ type CardPrintingSearchOptions map[string]string
|
|||
func GetAllCardPrintingSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (CardPrintingSearchOptions, error) {
|
||||
var searchOptions CardPrintingSearchOptions = make(CardPrintingSearchOptions)
|
||||
|
||||
cardPrintings, err := database.GetAllCardPrintings(db)
|
||||
scryfallCards, err := database.GetAllScryfallCards(db)
|
||||
if err != nil {
|
||||
return searchOptions, err
|
||||
}
|
||||
|
||||
for _, printing := range cardPrintings {
|
||||
for _, scryfallCard := range scryfallCards {
|
||||
// Filter based on search criteria
|
||||
filter := filterPrinting(printing, searchCriteria)
|
||||
filter := filterPrinting(scryfallCard, searchCriteria)
|
||||
if filter {
|
||||
continue
|
||||
}
|
||||
|
||||
// Construct search option string
|
||||
searchString := fmt.Sprintf("%s (%s %s) [%s]", printing.Name, printing.SetCode, printing.CollectorNumber, printing.Language)
|
||||
searchString := fmt.Sprintf("%s (%s %s) [%s]",
|
||||
scryfallCard.Name,
|
||||
scryfallCard.ScryfallSetCode,
|
||||
scryfallCard.CollectorNumber,
|
||||
scryfallCard.Language)
|
||||
|
||||
if printing.IsFoil {
|
||||
searchString += " FOIL"
|
||||
}
|
||||
|
||||
if printing.IsPromo {
|
||||
if scryfallCard.IsPromo {
|
||||
searchString += " PROMO"
|
||||
}
|
||||
|
||||
searchOptions[searchString] = printing.Id
|
||||
searchOptions[searchString] = scryfallCard.Id
|
||||
}
|
||||
|
||||
return searchOptions, err
|
||||
|
|
|
@ -108,7 +108,7 @@ func main() {
|
|||
|
||||
setCode := searchPrintingsCmd.String("set-code", "", "The code for the set the card we're searching for belongs to.")
|
||||
collectorNumber := searchPrintingsCmd.String("collector-number", "", "The collector number of the card we're searching for.")
|
||||
foil := searchPrintingsCmd.String("foil", "E", "Whether the card we're searching for is foil.")
|
||||
//foil := searchPrintingsCmd.String("foil", "E", "Whether the card we're searching for is foil.")
|
||||
|
||||
searchPrintingsCmd.Parse(flag.Args()[1:])
|
||||
|
||||
|
@ -119,13 +119,15 @@ func main() {
|
|||
Language: "en",
|
||||
}
|
||||
|
||||
if *foil == "Y" {
|
||||
searchCriteria.Foil = logic.True
|
||||
} else if *foil == "N" {
|
||||
searchCriteria.Foil = logic.False
|
||||
} else {
|
||||
searchCriteria.Foil = logic.Either
|
||||
}
|
||||
/*
|
||||
if *foil == "Y" {
|
||||
searchCriteria.Foil = logic.True
|
||||
} else if *foil == "N" {
|
||||
searchCriteria.Foil = logic.False
|
||||
} else {
|
||||
searchCriteria.Foil = logic.Either
|
||||
}
|
||||
*/
|
||||
|
||||
searchOptions, err := logic.GetAllCardPrintingSearchOptions(db, searchCriteria)
|
||||
logic.Check(err)
|
||||
|
|
|
@ -12,6 +12,8 @@ import (
|
|||
"sevenkeys/logic"
|
||||
"sevenkeys/logic/scryfall"
|
||||
"time"
|
||||
|
||||
sqlerr "github.com/go-mysql/errors"
|
||||
)
|
||||
|
||||
func scryfallLog(msg string) {
|
||||
|
@ -130,8 +132,8 @@ func isPaper(card scryfall.Card) bool {
|
|||
return paper
|
||||
}
|
||||
|
||||
func getScryfallCard(card scryfall.Card) []database.CardPrinting {
|
||||
return database.CardPrinting{
|
||||
func getScryfallCard(card scryfall.Card) database.ScryfallCard {
|
||||
return database.ScryfallCard{
|
||||
Id: card.Id,
|
||||
Name: card.Name,
|
||||
ScryfallSetCode: card.Set,
|
||||
|
@ -175,7 +177,7 @@ func updateCards(db *sql.DB, bulkData scryfall.BulkData) error {
|
|||
}
|
||||
|
||||
scryfallCard := getScryfallCard(card)
|
||||
err := database.InsertCardPrinting(db, scryfallCard)
|
||||
err := database.InsertScryfallCard(db, scryfallCard)
|
||||
// If we already have this card in the database, then we can just skip it
|
||||
if ok, insertErr := sqlerr.Error(err); ok {
|
||||
if insertErr == sqlerr.ErrDupeKey {
|
||||
|
|
Loading…
Reference in New Issue