duplication detection
This commit is contained in:
parent
e087715de5
commit
c910f6a2d2
29
src/serra/helpers.go
Normal file
29
src/serra/helpers.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package serra
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
|
)
|
||||||
|
|
||||||
|
func increase_count_of_card(coll *Collection, c *Card) error {
|
||||||
|
|
||||||
|
// find already existing card
|
||||||
|
sort := bson.D{{"_id", 1}}
|
||||||
|
search_filter := bson.D{{"_id", c.ID}}
|
||||||
|
stored_cards, err := coll.storage_find(search_filter, sort)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
stored_card := stored_cards[0]
|
||||||
|
|
||||||
|
// 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},
|
||||||
|
}
|
||||||
|
coll.storage_update(update_filter, update)
|
||||||
|
|
||||||
|
LogMessage(fmt.Sprintf("Updating Card \"%s\" amount to %d", stored_card.Name, stored_card.SerraCount), "purple")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||||
|
"go.mongodb.org/mongo-driver/mongo"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -30,15 +31,23 @@ func Add(cards []string) {
|
|||||||
|
|
||||||
// Write card to mongodb
|
// Write card to mongodb
|
||||||
err = coll.storage_add(c)
|
err = coll.storage_add(c)
|
||||||
|
|
||||||
|
// If duplicate key, increase count of card
|
||||||
|
if mongo.IsDuplicateKeyError(err) {
|
||||||
|
increase_count_of_card(coll, c)
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
LogMessage(fmt.Sprintf("\"%s\" (%.2f Eur) added to Collection.", c.Name, c.Prices.Eur), "purple")
|
// Give feedback of successfully added card
|
||||||
|
LogMessage(fmt.Sprintf("\"%s\" (%.2f Eur) added to Collection.", c.Name, c.Prices.Eur), "green")
|
||||||
}
|
}
|
||||||
storage_disconnect(client)
|
storage_disconnect(client)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Cards() {
|
func Cards() {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user