-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
95 lines (82 loc) · 2.05 KB
/
db.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
import (
"context"
"errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
"os"
)
var DbClient *mongo.Client
func SetupDb() {
connectionString := os.Getenv("MONGO_CONNECTION_STRING")
client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(connectionString))
if err != nil {
panic(err)
}
DbClient = client
}
func InsertDeck(deck Deck) (interface{}, error) {
dbName := os.Getenv("DB_NAME")
collectionName := os.Getenv("POKER_COLLECTION_NAME")
coll := DbClient.Database(dbName).Collection(collectionName)
result, err := coll.InsertOne(context.TODO(), deck)
if err != nil {
return result, err
}
return result.InsertedID, err
}
func GetDeck(deckId string) (Deck, error) {
dbName := os.Getenv("DB_NAME")
collectionName := os.Getenv("POKER_COLLECTION_NAME")
coll := DbClient.Database(dbName).Collection(collectionName)
var result Deck
err := coll.FindOne(context.TODO(), bson.D{{"_id", deckId}}).Decode(&result)
return result, err
}
func DrawCardsFromDeck(deckId string, count int) ([]Card, error) {
var cards []Card
deck, err := GetDeck(deckId)
if err != nil {
return nil, err
}
if len(deck.Cards) < count {
return nil, errors.New("deck has less cards than count intended to draw")
}
for i := 0; i < count; i++ {
var removedCard Card
removedCard, deck.Cards = deck.Cards[len(deck.Cards)-1], deck.Cards[:len(deck.Cards)-1]
cards = append(cards, removedCard)
}
dbName := os.Getenv("DB_NAME")
collectionName := os.Getenv("POKER_COLLECTION_NAME")
coll := DbClient.Database(dbName).Collection(collectionName)
var cardCodes []string
for _, card := range cards {
cardCodes = append(cardCodes, card.Code)
}
update := bson.D{{
"$pull",
bson.D{{
"cards",
bson.D{{
"code",
bson.D{{
"$in",
cardCodes,
}},
}},
}},
}, {
"$set",
bson.D{{
"remaining",
len(deck.Cards),
}},
}}
_, err = coll.UpdateOne(context.TODO(), bson.D{{"_id", deckId}}, update)
if err != nil {
return nil, err
}
return cards, nil
}