add update structure

This commit is contained in:
Florian Baumann 2021-12-30 21:30:09 +01:00
parent c93cde3c7c
commit b921536ce8
4 changed files with 29 additions and 6 deletions

View File

@ -36,4 +36,8 @@ Options:
serra.Sets() serra.Sets()
} }
if args["update"].(bool) {
serra.Update()
}
} }

View File

@ -124,7 +124,6 @@ type PriceEntry struct {
} }
func fetch_card(path string) (*Card, error) { func fetch_card(path string) (*Card, error) {
// TODO better URL Building... // TODO better URL Building...
resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/cards/%s/", path)) resp, err := http.Get(fmt.Sprintf("https://api.scryfall.com/cards/%s/", path))
if err != nil { if err != nil {

View File

@ -19,7 +19,6 @@ func Add(cards []string) {
// Loop over different cards // Loop over different cards
for _, card := range cards { for _, card := range cards {
// Fetch card from scryfall // Fetch card from scryfall
c, err := fetch_card(card) c, err := fetch_card(card)
if err != nil { if err != nil {
@ -45,7 +44,11 @@ func Cards() {
client := storage_connect() client := storage_connect()
coll := client.Database("serra").Collection("cards") coll := client.Database("serra").Collection("cards")
cards, _ := storage_find(coll)
sort := bson.D{{"collectornumber", 1}}
filter := bson.D{{}}
cards, _ := storage_find(coll, filter, sort)
for _, card := range cards { for _, card := range cards {
fmt.Printf("%s (%s) %.2f\n", card.Name, card.Set, card.Prices.Eur) fmt.Printf("%s (%s) %.2f\n", card.Name, card.Set, card.Prices.Eur)
} }
@ -74,3 +77,20 @@ func Sets() {
storage_disconnect(client) storage_disconnect(client)
} }
func Update() {
LogMessage(fmt.Sprintf("Serra %v\n", version), "green")
client := storage_connect()
coll := client.Database("serra").Collection("cards")
sort := bson.D{{"_id", 1}}
filter := bson.D{{"_id", "0d4f3c1d-d25e-4263-ab2b-19534c852678"}}
cards, _ := storage_find(coll, filter, sort)
for _, card := range cards {
fmt.Printf("%s (%s) %.2f\n", card.Name, card.Set, card.Prices.Eur)
}
storage_disconnect(client)
}

View File

@ -37,10 +37,10 @@ func storage_add(coll *mongo.Collection, card *Card) error {
} }
func storage_find(coll *mongo.Collection) ([]Card, error) { func storage_find(coll *mongo.Collection, filter, sort bson.D) ([]Card, error) {
opts := options.Find().SetSort(bson.D{{"collectornumber", 1}}) opts := options.Find().SetSort(sort)
cursor, err := coll.Find(context.TODO(), bson.D{{}}, opts) cursor, err := coll.Find(context.TODO(), filter, opts)
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }