Do not query scryfall if card is already in collection

This commit is contained in:
Florian Baumann 2022-12-02 14:03:51 +01:00
parent c7eaa28731
commit 5913f30f3e

View File

@ -2,9 +2,10 @@ package serra
import ( import (
"fmt" "fmt"
"strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/bson"
) )
func init() { func init() {
@ -27,37 +28,43 @@ var addCmd = &cobra.Command{
// Loop over different cards // Loop over different cards
for _, card := range cards { for _, card := range cards {
// Fetch card from scryfall
c, err := fetch_card(card)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
continue
}
// Write card to mongodb // Check if card is already in collection
c.SerraCount = count co, _ := coll.storage_find(bson.D{{"set", strings.Split(card, "/")[0]}, {"collectornumber", strings.Split(card, "/")[1]}}, bson.D{})
err = coll.storage_add(c)
// If duplicate key, increase count of card if len(co) >= 1 {
if mongo.IsDuplicateKeyError(err) { c := co[0]
if unique { if unique {
LogMessage(fmt.Sprintf("Not adding \"%s\" (%s, %.2f Eur) to Collection because it already exists.", c.Name, c.Rarity, c.Prices.Eur), "red") LogMessage(fmt.Sprintf("Not adding \"%s\" (%s, %.2f Eur) to Collection because it already exists.", c.Name, c.Rarity, c.Prices.Eur), "red")
continue continue
} }
modify_count_of_card(coll, c, count) modify_count_of_card(coll, &c, count)
continue // Give feedback of successfully added card
} LogMessage(fmt.Sprintf("%dx \"%s\" (%s, %.2f Eur) added to Collection.", c.SerraCount, c.Name, c.Rarity, c.Prices.Eur), "green")
// If error, print error and continue // If card is not already in collection, fetching from scyfall
if err != nil { } else {
LogMessage(fmt.Sprintf("%v", err), "red")
continue
}
// Give feedback of successfully added card // Fetch card from scryfall
LogMessage(fmt.Sprintf("%dx \"%s\" (%s, %.2f Eur) added to Collection.", c.SerraCount, c.Name, c.Rarity, c.Prices.Eur), "green") c, err := fetch_card(card)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
continue
}
// Write card to mongodb
c.SerraCount = count
err = coll.storage_add(c)
if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red")
continue
}
// Give feedback of successfully added card
LogMessage(fmt.Sprintf("%dx \"%s\" (%s, %.2f Eur) added to Collection.", c.SerraCount, c.Name, c.Rarity, c.Prices.Eur), "green")
}
} }
storage_disconnect(client) storage_disconnect(client)
return nil return nil