/
repo.go
105 lines (93 loc) · 3.14 KB
/
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
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
package user
import (
"github.com/ayo-ajayi/bloggy/db"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type UserRepo struct {
collection *mongo.Collection
}
func NewUserRepo(collection *mongo.Collection) *UserRepo {
return &UserRepo{collection}
}
func (repo *UserRepo) IsExists(filter interface{}, opts ...*options.FindOneOptions) (bool, error) {
ctx, cancel := db.DBReqContext(5)
defer cancel()
err := repo.collection.FindOne(ctx, filter, opts...).Err()
if err != nil {
if err == mongo.ErrNoDocuments {
return false, nil
}
return false, err
}
return true, nil
}
func (repo *UserRepo) CreateUser(user *User) (*mongo.InsertOneResult, error) {
ctx, cancel := db.DBReqContext(20)
defer cancel()
return repo.collection.InsertOne(ctx, user)
}
func (repo *UserRepo) GetUser(filter interface{}, opts ...*options.FindOneOptions) (*User, error) {
ctx, cancel := db.DBReqContext(20)
defer cancel()
var user User
err := repo.collection.FindOne(ctx, filter, opts...).Decode(&user)
if err != nil {
return nil, err
}
return &user, nil
}
func (repo *UserRepo) UpdateUser(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
ctx, cancel := db.DBReqContext(20)
defer cancel()
return repo.collection.UpdateOne(ctx, filter, update, opts...)
}
func (repo *UserRepo) CreateAboutMe(filter interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
ctx, cancel := db.DBReqContext(20)
defer cancel()
return repo.collection.InsertOne(ctx, filter, opts...)
}
func (repo *UserRepo) UpdateAboutMe(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
return repo.UpdateUser(filter, update, opts...)
}
func (repo *UserRepo) GetAboutMe(filter interface{}, opts ...*options.FindOneOptions) (*AboutMe, error) {
ctx, cancel := db.DBReqContext(20)
defer cancel()
var aboutMe AboutMe
err := repo.collection.FindOne(ctx, filter, opts...).Decode(&aboutMe)
if err != nil {
return nil, err
}
return &aboutMe, nil
}
func (repo *UserRepo) CreateMailingList(filter interface{}, opts ...*options.InsertOneOptions) (*mongo.InsertOneResult, error) {
ctx, cancel := db.DBReqContext(20)
defer cancel()
return repo.collection.InsertOne(ctx, filter, opts...)
}
func (repo *UserRepo) GetMailingList(filter interface{}, opts ...*options.FindOneOptions) (*MailingList, error) {
ctx, cancel := db.DBReqContext(20)
defer cancel()
var mailingList MailingList
err := repo.collection.FindOne(ctx, filter, opts...).Decode(&mailingList)
if err != nil {
return nil, err
}
return &mailingList, nil
}
func (repo *UserRepo) UpdateMailingList(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) {
return repo.UpdateUser(filter, update, opts...)
}
func (repo *UserRepo) GetUsers(filter interface{}, opts ...*options.FindOptions) ([]*User, error) {
ctx, cancel := db.DBReqContext(20)
defer cancel()
var users []*User
cursor, err := repo.collection.Find(ctx, filter, opts...)
if err != nil {
return nil, err
}
if err = cursor.All(ctx, &users); err != nil {
return nil, err
}
return users, nil
}