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"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteria) bool {
|
func filterPrinting(scryfallCard database.ScryfallCard, searchCriteria SearchCriteria) bool {
|
||||||
if searchCriteria.SetCode != "" && !strings.Contains(printing.SetCode, searchCriteria.SetCode) {
|
if searchCriteria.SetCode != "" && !strings.Contains(scryfallCard.ScryfallSetCode, searchCriteria.SetCode) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if searchCriteria.CollectorNumber != "" && !strings.Contains(printing.CollectorNumber, searchCriteria.CollectorNumber) {
|
if searchCriteria.CollectorNumber != "" && !strings.Contains(scryfallCard.CollectorNumber, searchCriteria.CollectorNumber) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if searchCriteria.Foil == False && printing.IsFoil {
|
if searchCriteria.Promo == False && scryfallCard.IsPromo {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if searchCriteria.Foil == True && !printing.IsFoil {
|
if searchCriteria.Promo == True && !scryfallCard.IsPromo {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if searchCriteria.Promo == False && printing.IsPromo {
|
if searchCriteria.Language != "" && scryfallCard.Language != searchCriteria.Language {
|
||||||
return true
|
|
||||||
}
|
|
||||||
if searchCriteria.Promo == True && !printing.IsPromo {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
if searchCriteria.Language != "" && printing.Language != searchCriteria.Language {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -50,14 +50,13 @@ func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]dat
|
||||||
var filteredResults []database.LocateCardResult
|
var filteredResults []database.LocateCardResult
|
||||||
|
|
||||||
for _, result := range results {
|
for _, result := range results {
|
||||||
printing := database.CardPrinting{
|
scryfallCard := database.ScryfallCard{
|
||||||
SetCode: result.SetCode,
|
ScryfallSetCode: result.SetCode,
|
||||||
IsFoil: result.IsFoil,
|
IsPromo: result.IsPromo,
|
||||||
IsPromo: result.IsPromo,
|
Language: result.Language,
|
||||||
Language: result.Language,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filter := filterPrinting(printing, criteria)
|
filter := filterPrinting(scryfallCard, criteria)
|
||||||
if filter {
|
if filter {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -77,9 +76,6 @@ func GetLocationDescription(location database.LocateCardResult) string {
|
||||||
location.CollectorNumber,
|
location.CollectorNumber,
|
||||||
location.Language)
|
location.Language)
|
||||||
|
|
||||||
if location.IsFoil {
|
|
||||||
description += " FOIL"
|
|
||||||
}
|
|
||||||
if location.IsPromo {
|
if location.IsPromo {
|
||||||
description += " PROMO"
|
description += " PROMO"
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,6 @@ const (
|
||||||
type SearchCriteria struct {
|
type SearchCriteria struct {
|
||||||
SetCode string
|
SetCode string
|
||||||
CollectorNumber string
|
CollectorNumber string
|
||||||
Foil Triadic
|
|
||||||
Promo Triadic
|
Promo Triadic
|
||||||
Language string
|
Language string
|
||||||
}
|
}
|
||||||
|
@ -31,30 +30,30 @@ type CardPrintingSearchOptions map[string]string
|
||||||
func GetAllCardPrintingSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (CardPrintingSearchOptions, error) {
|
func GetAllCardPrintingSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (CardPrintingSearchOptions, error) {
|
||||||
var searchOptions CardPrintingSearchOptions = make(CardPrintingSearchOptions)
|
var searchOptions CardPrintingSearchOptions = make(CardPrintingSearchOptions)
|
||||||
|
|
||||||
cardPrintings, err := database.GetAllCardPrintings(db)
|
scryfallCards, err := database.GetAllScryfallCards(db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return searchOptions, err
|
return searchOptions, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, printing := range cardPrintings {
|
for _, scryfallCard := range scryfallCards {
|
||||||
// Filter based on search criteria
|
// Filter based on search criteria
|
||||||
filter := filterPrinting(printing, searchCriteria)
|
filter := filterPrinting(scryfallCard, searchCriteria)
|
||||||
if filter {
|
if filter {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct search option string
|
// 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 {
|
if scryfallCard.IsPromo {
|
||||||
searchString += " FOIL"
|
|
||||||
}
|
|
||||||
|
|
||||||
if printing.IsPromo {
|
|
||||||
searchString += " PROMO"
|
searchString += " PROMO"
|
||||||
}
|
}
|
||||||
|
|
||||||
searchOptions[searchString] = printing.Id
|
searchOptions[searchString] = scryfallCard.Id
|
||||||
}
|
}
|
||||||
|
|
||||||
return searchOptions, err
|
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.")
|
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.")
|
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:])
|
searchPrintingsCmd.Parse(flag.Args()[1:])
|
||||||
|
|
||||||
|
@ -119,13 +119,15 @@ func main() {
|
||||||
Language: "en",
|
Language: "en",
|
||||||
}
|
}
|
||||||
|
|
||||||
if *foil == "Y" {
|
/*
|
||||||
searchCriteria.Foil = logic.True
|
if *foil == "Y" {
|
||||||
} else if *foil == "N" {
|
searchCriteria.Foil = logic.True
|
||||||
searchCriteria.Foil = logic.False
|
} else if *foil == "N" {
|
||||||
} else {
|
searchCriteria.Foil = logic.False
|
||||||
searchCriteria.Foil = logic.Either
|
} else {
|
||||||
}
|
searchCriteria.Foil = logic.Either
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
searchOptions, err := logic.GetAllCardPrintingSearchOptions(db, searchCriteria)
|
searchOptions, err := logic.GetAllCardPrintingSearchOptions(db, searchCriteria)
|
||||||
logic.Check(err)
|
logic.Check(err)
|
||||||
|
|
|
@ -12,6 +12,8 @@ import (
|
||||||
"sevenkeys/logic"
|
"sevenkeys/logic"
|
||||||
"sevenkeys/logic/scryfall"
|
"sevenkeys/logic/scryfall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
sqlerr "github.com/go-mysql/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func scryfallLog(msg string) {
|
func scryfallLog(msg string) {
|
||||||
|
@ -130,8 +132,8 @@ func isPaper(card scryfall.Card) bool {
|
||||||
return paper
|
return paper
|
||||||
}
|
}
|
||||||
|
|
||||||
func getScryfallCard(card scryfall.Card) []database.CardPrinting {
|
func getScryfallCard(card scryfall.Card) database.ScryfallCard {
|
||||||
return database.CardPrinting{
|
return database.ScryfallCard{
|
||||||
Id: card.Id,
|
Id: card.Id,
|
||||||
Name: card.Name,
|
Name: card.Name,
|
||||||
ScryfallSetCode: card.Set,
|
ScryfallSetCode: card.Set,
|
||||||
|
@ -175,7 +177,7 @@ func updateCards(db *sql.DB, bulkData scryfall.BulkData) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
scryfallCard := getScryfallCard(card)
|
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 we already have this card in the database, then we can just skip it
|
||||||
if ok, insertErr := sqlerr.Error(err); ok {
|
if ok, insertErr := sqlerr.Error(err); ok {
|
||||||
if insertErr == sqlerr.ErrDupeKey {
|
if insertErr == sqlerr.ErrDupeKey {
|
||||||
|
|
Loading…
Reference in New Issue