/
user_repository.go
154 lines (127 loc) · 3.31 KB
/
user_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package repositories
import (
"context"
"errors"
"github.com/febrihidayan/go-architecture-monorepo/services/user/domain/entities"
"github.com/febrihidayan/go-architecture-monorepo/services/user/internal/repositories/mongo/mappers"
"github.com/febrihidayan/go-architecture-monorepo/services/user/internal/repositories/mongo/models"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type UserRepository struct {
db *mongo.Database
}
func NewUserRepository(db *mongo.Database) UserRepository {
return UserRepository{db: db}
}
func (x *UserRepository) Create(ctx context.Context, payload *entities.User) error {
_, err := x.db.Collection(models.User{}.TableName()).InsertOne(ctx, mappers.ToModelUser(payload))
if err != nil {
return err
}
return nil
}
func (x *UserRepository) Find(ctx context.Context, id string) (*entities.User, error) {
var item models.User
err := x.db.Collection(models.User{}.TableName()).FindOne(ctx, bson.M{"_id": id}).Decode(&item)
if err != nil {
return nil, errors.New("user not found")
}
return mappers.ToDomainUser(&item), nil
}
func (x *UserRepository) FindByEmail(ctx context.Context, email string) (*entities.User, error) {
var item models.User
err := x.db.Collection(models.User{}.TableName()).FindOne(ctx, bson.M{"email": email}).Decode(&item)
if err != nil {
return nil, errors.New("user not found")
}
return mappers.ToDomainUser(&item), nil
}
func (x *UserRepository) GetAll(ctx context.Context, params *entities.UserQueryParams) ([]*entities.User, int, error) {
var (
filter = mongo.Pipeline{}
match bson.D
skip = (params.Page - 1) * params.PerPage
)
if params.Search != "" {
match = append(match, bson.D{{"name", primitive.Regex{
Pattern: params.Search,
Options: "i",
}}}...)
}
if len(match) > 0 {
filter = append(filter, mongo.Pipeline{
bson.D{{
"$match", match,
}},
}...)
}
filter = append(filter, mongo.Pipeline{
bson.D{{
"$sort", bson.D{
{"created_at", -1},
},
}},
bson.D{{
"$facet", bson.D{
{"total", bson.A{
bson.D{{
"$count", "count",
}},
}},
{"data", bson.A{
bson.D{{
"$addFields", bson.D{
{"_id", "$_id"},
},
}},
}},
},
}},
bson.D{{
"$unwind", "$total",
}},
bson.D{{
"$project", bson.D{
{"data", bson.D{
{"$slice", bson.A{
"$data", skip, bson.D{
{"$ifNull", bson.A{
params.PerPage, "$total.count",
}},
},
}},
}},
{"page", bson.D{
{"$literal", skip/params.PerPage + 1},
}},
{"per_page", bson.D{
{"$literal", params.PerPage},
}},
{"total", "$total.count"},
},
}},
}...)
cursor, err := x.db.Collection(models.User{}.TableName()).Aggregate(ctx, filter)
if err != nil {
return nil, 0, err
}
defer cursor.Close(ctx)
var result models.UserMeta
for cursor.Next(ctx) {
if err := cursor.Decode(&result); err != nil {
return nil, result.Total, err
}
}
return mappers.ToListDomainUser(result.Data), result.Total, nil
}
func (x *UserRepository) Update(ctx context.Context, payload *entities.User) error {
_, err := x.db.Collection(models.User{}.TableName()).ReplaceOne(ctx, bson.M{
"_id": payload.ID.String(),
}, mappers.ToModelUser(payload))
if err != nil {
return err
}
return nil
}