-
Notifications
You must be signed in to change notification settings - Fork 0
/
card.go
45 lines (37 loc) · 1.1 KB
/
card.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
package mongo
import (
"github.com/Nerzal/CardsOfBinokee-Server/pkg/core"
"github.com/Nerzal/CardsOfBinokee-Server/pkg/repository"
"github.com/globalsign/mgo/bson"
"github.com/go-bongo/bongo"
)
const collectionName = "cards"
// NewCardRepository creates a new instance of repository.CardRepository
func NewCardRepository() (repository.CardRepository, error) {
dialInfo := getDialInfo()
connection, err := bongo.Connect(&bongo.Config{DialInfo: dialInfo})
return &cardRepository{connection}, err
}
type cardRepository struct {
connection *bongo.Connection
}
func (repo *cardRepository) GetCards() []core.Card {
var result []core.Card
results := repo.connection.Collection(collectionName).Find(bson.M{})
var card core.Card
for results.Next(&card) {
result = append(result, card)
}
return result
}
func (repo *cardRepository) SaveCards(cards []core.Card) error {
collection := repo.connection.Collection(collectionName)
collection.Delete(bson.M{})
for _, card := range cards {
err := collection.Save(&card)
if err != nil {
return err
}
}
return nil
}