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,6 +28,25 @@ var addCmd = &cobra.Command{
// Loop over different cards // Loop over different cards
for _, card := range cards { for _, card := range cards {
// Check if card is already in collection
co, _ := coll.storage_find(bson.D{{"set", strings.Split(card, "/")[0]}, {"collectornumber", strings.Split(card, "/")[1]}}, bson.D{})
if len(co) >= 1 {
c := co[0]
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")
continue
}
modify_count_of_card(coll, &c, count)
// 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 card is not already in collection, fetching from scyfall
} else {
// Fetch card from scryfall // Fetch card from scryfall
c, err := fetch_card(card) c, err := fetch_card(card)
if err != nil { if err != nil {
@ -37,20 +57,6 @@ var addCmd = &cobra.Command{
// Write card to mongodb // Write card to mongodb
c.SerraCount = count c.SerraCount = count
err = coll.storage_add(c) err = coll.storage_add(c)
// If duplicate key, increase count of card
if mongo.IsDuplicateKeyError(err) {
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")
continue
}
modify_count_of_card(coll, c, count)
continue
}
// If error, print error and continue
if err != nil { if err != nil {
LogMessage(fmt.Sprintf("%v", err), "red") LogMessage(fmt.Sprintf("%v", err), "red")
continue continue
@ -59,6 +65,7 @@ var addCmd = &cobra.Command{
// Give feedback of successfully added card // 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") 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
}, },