Add card insert code

This commit is contained in:
The Magician 2024-05-21 15:27:05 +01:00
parent af6a9d0feb
commit a7b8b66db3
4 changed files with 32 additions and 3 deletions

View File

@ -46,3 +46,21 @@ func InsertOrUpdateSet(db *sql.DB, set types.Set) error {
return nil
}
func InsertCard(db *sql.DB, card types.Card) error {
query := `INSERT INTO CardPrinting
(Id, Name, SetCode, HasFoil, HasNonFoil, IsReserved, IsRacist, IsPromo, CollectorNumber, Language)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`
insert, err := db.Prepare(query)
if err != nil {
return err
}
_, err = insert.Exec(card.Id, card.Name, card.Set, card.Foil, card.NonFoil, card.Reserved, card.ContentWarning, card.Promo, card.CollectorNumber, card.Language)
if err != nil {
return err
}
return nil
}

View File

@ -22,7 +22,7 @@ func GetCacheTimestampByType(db *sql.DB, cacheType string) (time.Time, error) {
err := db.QueryRow(query, cacheType).Scan(&timestamp)
if err == sql.ErrNoRows {
return time.Unix(0, 0), err
return time.Unix(0, 0), nil
}
stamp, err := time.Parse("2006-01-02 15:04:05", timestamp)

View File

@ -115,6 +115,17 @@ func cacheAllCardsFile(db *sql.DB, uri string, updatedAtTimestamp time.Time) err
return nil
}
func importCards(db *sql.DB, cards []types.Card) error {
for _, card := range cards {
err := operations.InsertCard(db, card)
if err != nil {
return err
}
}
return nil
}
func main() {
log.Println("Connecting to database...")
db := database.GetDatabaseFromConfig("config.json")
@ -169,7 +180,7 @@ func main() {
log.Printf("Unmarshaled file.")
log.Printf("Importing card data into database...")
err = operations.InsertCards(db, allCards)
err = importCards(db, allCards)
check(err)
log.Printf("Imported card data.")
}

View File

@ -19,8 +19,8 @@ CREATE TABLE IF NOT EXISTS CardPrinting (
Name VARCHAR(150) NOT NULL,
SetCode VARCHAR(6) NOT NULL,
FOREIGN KEY (SetCode) REFERENCES ExpansionSet(SetCode),
HasNonFoil BOOLEAN NOT NULL,
HasFoil BOOLEAN NOT NULL,
HasNonFoil BOOLEAN NOT NULL,
IsReserved BOOLEAN NOT NULL,
IsRacist BOOLEAN NOT NULL,
IsPromo BOOLEAN NOT NULL,