removal of cards
This commit is contained in:
parent
c002fbafce
commit
e207f4fbd4
3
serra.go
3
serra.go
@ -13,6 +13,7 @@ func main() {
|
||||
|
||||
Usage:
|
||||
serra add <card>...
|
||||
serra remove <card>...
|
||||
serra cards
|
||||
serra set <set>
|
||||
serra sets
|
||||
@ -28,6 +29,8 @@ Options:
|
||||
|
||||
if args["add"].(bool) {
|
||||
serra.Add(args["<card>"].([]string))
|
||||
} else if args["remove"].(bool) {
|
||||
serra.Remove(args["<card>"].([]string))
|
||||
} else if args["cards"].(bool) {
|
||||
serra.Cards()
|
||||
} else if args["sets"].(bool) {
|
||||
|
||||
@ -1,12 +1,14 @@
|
||||
package serra
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
func increase_count_of_card(coll *Collection, c *Card) error {
|
||||
func modify_count_of_card(coll *Collection, c *Card, amount int64) error {
|
||||
|
||||
// find already existing card
|
||||
sort := bson.D{{"_id", 1}}
|
||||
@ -20,10 +22,27 @@ func increase_count_of_card(coll *Collection, c *Card) error {
|
||||
// update card amount
|
||||
update_filter := bson.M{"_id": bson.M{"$eq": c.ID}}
|
||||
update := bson.M{
|
||||
"$set": bson.M{"serra_count": stored_card.SerraCount + 1},
|
||||
"$set": bson.M{"serra_count": stored_card.SerraCount + amount},
|
||||
}
|
||||
coll.storage_update(update_filter, update)
|
||||
|
||||
LogMessage(fmt.Sprintf("Updating Card \"%s\" amount to %d", stored_card.Name, stored_card.SerraCount+1), "purple")
|
||||
LogMessage(fmt.Sprintf("Updating Card \"%s\" amount to %d", stored_card.Name, stored_card.SerraCount+amount), "purple")
|
||||
return nil
|
||||
}
|
||||
|
||||
func find_card_by_setcollectornumber(coll *Collection, setcode string, collectornumber string) (*Card, error) {
|
||||
|
||||
sort := bson.D{{"_id", 1}}
|
||||
c, _ := strconv.ParseInt(collectornumber, 10, 64)
|
||||
search_filter := bson.D{{"set", setcode}, {"collectornumber", c}}
|
||||
stored_cards, err := coll.storage_find(search_filter, sort)
|
||||
if err != nil {
|
||||
return &Card{}, err
|
||||
}
|
||||
|
||||
if len(stored_cards) < 1 {
|
||||
return &Card{}, errors.New("Card not found")
|
||||
}
|
||||
|
||||
return &stored_cards[0], nil
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package serra
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
@ -34,7 +35,7 @@ func Add(cards []string) {
|
||||
|
||||
// If duplicate key, increase count of card
|
||||
if mongo.IsDuplicateKeyError(err) {
|
||||
increase_count_of_card(coll, c)
|
||||
modify_count_of_card(coll, c, 1)
|
||||
continue
|
||||
}
|
||||
|
||||
@ -50,6 +51,35 @@ func Add(cards []string) {
|
||||
storage_disconnect(client)
|
||||
}
|
||||
|
||||
// Remove
|
||||
func Remove(cards []string) {
|
||||
LogMessage(fmt.Sprintf("Serra %v\n", version), "green")
|
||||
|
||||
client := storage_connect()
|
||||
coll := &Collection{client.Database("serra").Collection("cards")}
|
||||
|
||||
// Loop over different cards
|
||||
for _, card := range cards {
|
||||
// Fetch card from scryfall
|
||||
c, err := find_card_by_setcollectornumber(coll, strings.Split(card, "/")[0], strings.Split(card, "/")[1])
|
||||
if err != nil {
|
||||
LogMessage(fmt.Sprintf("%v", err), "red")
|
||||
continue
|
||||
}
|
||||
|
||||
if c.SerraCount > 1 {
|
||||
// update
|
||||
modify_count_of_card(coll, c, -1)
|
||||
} else {
|
||||
coll.storage_remove(bson.M{"_id": c.ID})
|
||||
LogMessage(fmt.Sprintf("\"%s\" (%.2f Eur) removed from the Collection.", c.Name, c.Prices.Eur), "green")
|
||||
}
|
||||
// delete
|
||||
|
||||
}
|
||||
storage_disconnect(client)
|
||||
}
|
||||
|
||||
func Cards() {
|
||||
LogMessage(fmt.Sprintf("Serra %v\n", version), "green")
|
||||
|
||||
|
||||
@ -61,6 +61,17 @@ func (coll Collection) storage_find(filter, sort bson.D) ([]Card, error) {
|
||||
|
||||
}
|
||||
|
||||
func (coll Collection) storage_remove(filter bson.M) error {
|
||||
|
||||
_, err := coll.DeleteOne(context.TODO(), filter)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
func (coll Collection) storage_aggregate(groupstage bson.D) ([]primitive.M, error) {
|
||||
|
||||
// db.cards.aggregate([ {$group: { _id: "$setname", sum: { $sum: "$prices.eur"}}}])
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user