/
repository.go
139 lines (108 loc) · 3.46 KB
/
repository.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package notifications
import (
"context"
"time"
"github.com/golang/glog"
apimongo "github.com/pruh/api/mongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
)
// Repository accesses notifications store
type Repository struct {
Mongo *mongo.Client
}
const (
dbName = "ApiDB"
notifCollectionName = "notifications"
)
// GetAll returns all notifications
func (r *Repository) GetAll() ([]Notification, error) {
glog.Info("Querying for all notifications")
collection := r.Mongo.Database(dbName).Collection(notifCollectionName)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
cur, err := collection.Find(ctx, bson.D{})
if err != nil {
glog.Errorf("Error while performing DB find. %s", err)
return nil, err
}
defer cur.Close(ctx)
notifs := []Notification{}
for cur.Next(ctx) {
var notif Notification
err := cur.Decode(¬if)
if err != nil {
glog.Errorf("Cannot decode notification. %s", err)
return nil, err
}
notifs = append(notifs, notif)
}
if err := cur.Err(); err != nil {
glog.Errorf("Cursor has some error. %s", err)
return nil, err
}
return notifs, nil
}
// GetOne returns notification by ID or nil
func (r *Repository) GetOne(uuid apimongo.UUID) (*Notification, error) {
glog.Infof("Querying for notification with UUID: %s", uuid)
collection := r.Mongo.Database(dbName).Collection(notifCollectionName)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
var notif Notification
err := collection.FindOne(ctx, bson.M{"_id": uuid}).Decode(¬if)
if err != nil {
if err == mongo.ErrNoDocuments {
return nil, nil
}
glog.Errorf("Cannot decode notification. %s", err)
return nil, err
}
return ¬if, nil
}
// CreateOne creates new notification for specified params
func (r *Repository) CreateOne(notification Notification) bool {
glog.Infof("Creating new notification: %+v", notification)
collection := r.Mongo.Database(dbName).Collection(notifCollectionName)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_, err := collection.InsertOne(ctx, notification)
if err != nil {
glog.Errorf("Failed to insert notification. %s", err)
return false
}
return true
}
// DeleteOne deletes notification with ID and returns true if record was removed
func (r *Repository) DeleteOne(uuid apimongo.UUID) (bool, error) {
glog.Infof("Deleting notification with UUID: %s", uuid)
collection := r.Mongo.Database(dbName).Collection(notifCollectionName)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
res, err := collection.DeleteOne(ctx, bson.M{"_id": uuid})
if err != nil {
glog.Errorf("Failed to delete notification. %s", err)
return false, err
}
if res.DeletedCount == 0 {
return false, nil
}
return true, nil
}
// DeleteAll deletes notifications with IDs and returns true if any record was removed
func (r *Repository) DeleteAll(uuids []apimongo.UUID) (bool, error) {
glog.Infof("Deleting notifications with UUID: %v", uuids)
collection := r.Mongo.Database(dbName).Collection(notifCollectionName)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
filter := bson.M{"_id": bson.M{"$in": uuids}}
res, err := collection.DeleteMany(ctx, filter)
if err != nil {
glog.Error("Failed to delete notifications. ", err)
return false, err
}
if res.DeletedCount == 0 {
return false, nil
}
return true, nil
}