-
Notifications
You must be signed in to change notification settings - Fork 1
/
mongo_repo.go
73 lines (60 loc) · 1.66 KB
/
mongo_repo.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
package gomongocrud
import (
"context"
"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
type MongoRepo struct {
Client *mongo.Client
}
func (repo *MongoRepo) Store(ctx context.Context, task *Task) error {
if _, err := tasksCollection(repo.Client).InsertOne(ctx, task); err != nil {
return errors.WithStack(err)
}
return nil
}
func (repo *MongoRepo) FetchAll(ctx context.Context) ([]Task, error) {
cursor, err := tasksCollection(repo.Client).Find(ctx, bson.M{})
if err != nil {
return nil, errors.WithStack(err)
}
var tasks []Task
if err := cursor.All(ctx, &tasks); err != nil {
return nil, errors.WithStack(err)
}
return tasks, nil
}
func (repo *MongoRepo) GetByID(ctx context.Context, id string) (*Task, error) {
var task Task
if err := tasksCollection(repo.Client).FindOne(ctx, bson.M{"_id": id}).Decode(&task); err != nil {
if err == mongo.ErrNoDocuments {
return nil, ErrRecordNotFound
}
return nil, errors.WithStack(err)
}
return &task, nil
}
func (repo *MongoRepo) Update(ctx context.Context, task *Task) error {
res, err := tasksCollection(repo.Client).ReplaceOne(ctx, bson.M{"_id": task.ID}, task)
if err != nil {
return errors.WithStack(err)
}
if res.ModifiedCount == 0 {
return ErrRecordNotFound
}
return nil
}
func (repo *MongoRepo) Delete(ctx context.Context, id string) error {
res, err := tasksCollection(repo.Client).DeleteOne(ctx, bson.M{"_id": id})
if err != nil {
return errors.WithStack(err)
}
if res.DeletedCount == 0 {
return ErrRecordNotFound
}
return nil
}
func tasksCollection(c *mongo.Client) *mongo.Collection {
return c.Database("tasks").Collection("tasks")
}