Import sets into database
This commit is contained in:
parent
3f9d3a9dd6
commit
75b425c849
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sevenkeys/database"
|
||||
"sevenkeys/database/operations"
|
||||
"sevenkeys/scryfall/methods"
|
||||
)
|
||||
|
||||
func check(err error) {
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
sets, err := methods.GetSets()
|
||||
check(err)
|
||||
|
||||
db := database.GetDatabaseFromConfig("config.json")
|
||||
err = operations.InsertOrUpdateSets(db, sets)
|
||||
check(err)
|
||||
|
||||
// Download icon files to cache/ directory
|
||||
}
|
|
@ -2,64 +2,15 @@ package operations
|
|||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log"
|
||||
"sevenkeys/scryfall/types"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ErrGamepieceExists error = errors.New("Gamepiece already exists in database")
|
||||
|
||||
func insertGamepiece(db *sql.DB, cardName string) (int, error) {
|
||||
gamepieceInsertQuery := "INSERT INTO Gamepiece (Name) VALUES (?);"
|
||||
gamepieceInsert, err := db.Prepare(gamepieceInsertQuery)
|
||||
defer gamepieceInsert.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
existingGamepiece, err := GetGamepieceByName(db, cardName)
|
||||
if err != sql.ErrNoRows {
|
||||
return existingGamepiece.Id, ErrGamepieceExists
|
||||
}
|
||||
|
||||
result, err := gamepieceInsert.Exec(cardName)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
gamepieceId, err := result.LastInsertId()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
return int(gamepieceId), nil
|
||||
}
|
||||
|
||||
func insertCardPrinting(db *sql.DB, gamepieceId int, setCode string, imageUrl string) {
|
||||
cardPrintingInsertQuery := "INSERT INTO CardPrinting (GamepieceId, SetCode, ImageUrl) VALUES (?, ?, ?);"
|
||||
cardPrintingInsert, err := db.Prepare(cardPrintingInsertQuery)
|
||||
defer cardPrintingInsert.Close()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = cardPrintingInsert.Exec(gamepieceId, setCode, imageUrl)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func InsertCards(db *sql.DB, cards []types.Card) {
|
||||
for index := range cards {
|
||||
card := cards[index]
|
||||
gamepieceId, _ := insertGamepiece(db, card.Name)
|
||||
insertCardPrinting(db, gamepieceId, card.Set, card.ImageUris["png"])
|
||||
}
|
||||
}
|
||||
|
||||
func InsertOrUpdateCacheTimestampByType(db *sql.DB, cacheType string, stamp time.Time) error {
|
||||
query := `INSERT INTO CacheTimestamps (CacheType, Stamp) VALUES (?, ?) ON DUPLICATE KEY UPDATE Stamp = ?;`
|
||||
query := `INSERT INTO CacheTimestamps (CacheType, Stamp)
|
||||
VALUES (?, ?)
|
||||
ON DUPLICATE KEY
|
||||
UPDATE Stamp = ?;`
|
||||
|
||||
insertOrUpdate, err := db.Prepare(query)
|
||||
defer insertOrUpdate.Close()
|
||||
|
@ -74,3 +25,25 @@ func InsertOrUpdateCacheTimestampByType(db *sql.DB, cacheType string, stamp time
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func InsertOrUpdateSets(db *sql.DB, sets []types.Set) error {
|
||||
query := `INSERT INTO ExpansionSet (SetCode, Name, CardCount, IconSvgUri)
|
||||
VALUES (?, ?, ?, ?)
|
||||
ON DUPLICATE KEY
|
||||
Update Name = ?, CardCount = ?, IconSvgUri = ?;`
|
||||
|
||||
insertOrUpdate, err := db.Prepare(query)
|
||||
defer insertOrUpdate.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, set := range sets {
|
||||
_, err = insertOrUpdate.Exec(set.Code, set.Name, set.CardCount, set.IconSvgUri, set.Name, set.CardCount, set.IconSvgUri)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -1,3 +1,36 @@
|
|||
package methods
|
||||
|
||||
func GetSets() {}
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"sevenkeys/scryfall/types"
|
||||
)
|
||||
|
||||
const SETS_API_URL string = "https://api.scryfall.com/sets"
|
||||
|
||||
func GetSets() ([]types.Set, error) {
|
||||
response, err := http.Get(SETS_API_URL)
|
||||
if err != nil {
|
||||
return []types.Set{}, nil
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return []types.Set{}, errors.New("HTTP request failed with code: " + string(response.StatusCode))
|
||||
}
|
||||
|
||||
defer response.Body.Close()
|
||||
setsBytes, err := io.ReadAll(response.Body)
|
||||
if err != nil {
|
||||
return []types.Set{}, err
|
||||
}
|
||||
|
||||
var setList types.SetList
|
||||
err = json.Unmarshal(setsBytes, &setList)
|
||||
if err != nil {
|
||||
return []types.Set{}, err
|
||||
}
|
||||
|
||||
return setList.Data, nil
|
||||
}
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package types
|
||||
|
||||
type SetList struct {
|
||||
Object string `json:"object"`
|
||||
HasMore bool `json:"has_more"`
|
||||
Data []Set `json:"data"`
|
||||
}
|
||||
|
||||
type Set struct {
|
||||
Code string `json:"code"`
|
||||
Name string `json:"name"`
|
||||
CardCount int `json:"card_count"`
|
||||
IconSvgUri string `json:"icon_svg_uri"`
|
||||
}
|
Loading…
Reference in New Issue