/
mongo_user.go
executable file
·206 lines (169 loc) · 5.13 KB
/
mongo_user.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package repository
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"github.com/semka95/shortener/backend/domain"
"github.com/semka95/shortener/backend/store"
)
type mongoUserRepository struct {
Conn *mongo.Database
logger *zap.Logger
tracer trace.Tracer
}
// NewMongoUserRepository will create an object that represent the user.Repository interface
func NewMongoUserRepository(c *mongo.Client, db string, logger *zap.Logger, tracer trace.Tracer) domain.UserRepository {
return &mongoUserRepository{
Conn: c.Database(db),
logger: logger,
tracer: tracer,
}
}
func (m *mongoUserRepository) fetch(ctx context.Context, command interface{}) ([]*domain.User, error) {
ctx, span := m.tracer.Start(ctx, "repository fetch")
defer span.End()
cur, err := m.Conn.RunCommandCursor(ctx, command)
if err != nil {
span.RecordError(err)
return nil, fmt.Errorf("can't execute command: %w", err)
}
defer func(ctx context.Context) {
err = cur.Close(ctx)
if err != nil {
m.logger.Error("Can't close cursor: ", zap.Error(err))
}
}(ctx)
result := make([]*domain.User, 0)
for cur.Next(ctx) {
elem := new(domain.User)
if err = cur.Decode(elem); err != nil {
span.RecordError(err)
return nil, fmt.Errorf("can't unmarshal document into User: %w", err)
}
result = append(result, elem)
}
if err = cur.Err(); err != nil {
span.RecordError(err)
return nil, fmt.Errorf("user cursor error: %w", err)
}
return result, nil
}
func (m *mongoUserRepository) GetByID(ctx context.Context, id primitive.ObjectID) (*domain.User, error) {
ctx, span := m.tracer.Start(
ctx,
"repository GetByID",
trace.WithAttributes(
attribute.String("userid", id.Hex())),
)
defer span.End()
command := bson.D{
primitive.E{Key: "find", Value: "user"},
primitive.E{Key: "limit", Value: 1},
primitive.E{Key: "filter", Value: bson.D{primitive.E{Key: "_id", Value: id}}},
}
list, err := m.fetch(ctx, command)
if err != nil {
span.RecordError(err)
return nil, fmt.Errorf("user get error: %w: %s", domain.ErrInternalServerError, err.Error())
}
if len(list) == 0 {
span.RecordError(domain.ErrNotFound)
return nil, fmt.Errorf("user was not found: %w", domain.ErrNotFound)
}
return list[0], nil
}
func (m *mongoUserRepository) Create(ctx context.Context, user *domain.User) error {
ctx, span := m.tracer.Start(
ctx,
"repository Create",
trace.WithAttributes(
attribute.String("userid", user.ID.Hex())),
)
defer span.End()
_, err := m.Conn.Collection("user").InsertOne(ctx, user)
if err != nil {
span.RecordError(err)
return fmt.Errorf("user store error: %w: %s", domain.ErrInternalServerError, err.Error())
}
return nil
}
func (m *mongoUserRepository) Delete(ctx context.Context, id primitive.ObjectID) error {
ctx, span := m.tracer.Start(
ctx,
"repository Delete",
trace.WithAttributes(
attribute.String("userid", id.Hex())),
)
defer span.End()
filter := bson.D{
primitive.E{Key: "_id", Value: id},
}
delRes, err := m.Conn.Collection("user").DeleteOne(ctx, filter)
if err != nil {
span.RecordError(err)
return fmt.Errorf("user delete error: %w: %s", domain.ErrInternalServerError, err.Error())
}
if delRes.DeletedCount == 0 {
err = fmt.Errorf("user was not deleted: %w", domain.ErrNoAffected)
span.RecordError(err)
return err
}
return nil
}
func (m *mongoUserRepository) Update(ctx context.Context, user *domain.User) error {
ctx, span := m.tracer.Start(
ctx,
"repository Update",
trace.WithAttributes(
attribute.String("userid", user.ID.Hex())),
)
defer span.End()
filter := bson.D{
primitive.E{Key: "_id", Value: user.ID},
}
doc, err := store.StructToDoc(&user)
if err != nil {
span.RecordError(err)
return fmt.Errorf("can't convert User to bson.D: %w, %s", domain.ErrInternalServerError, err.Error())
}
update := bson.D{primitive.E{Key: "$set", Value: doc}}
updRes, err := m.Conn.Collection("user").UpdateOne(ctx, filter, update)
if err != nil {
span.RecordError(err)
return fmt.Errorf("user update error: %w: %s", domain.ErrInternalServerError, err.Error())
}
if updRes.ModifiedCount == 0 {
err = fmt.Errorf("user was not updated: %w", domain.ErrNoAffected)
span.RecordError(err)
return err
}
return nil
}
func (m *mongoUserRepository) GetByEmail(ctx context.Context, email string) (*domain.User, error) {
ctx, span := m.tracer.Start(
ctx,
"repository GetByEmail",
)
defer span.End()
command := bson.D{
primitive.E{Key: "find", Value: "user"},
primitive.E{Key: "limit", Value: 1},
primitive.E{Key: "filter", Value: bson.D{primitive.E{Key: "email", Value: email}}},
}
list, err := m.fetch(ctx, command)
if err != nil {
span.RecordError(err)
return nil, fmt.Errorf("user get error: %w: %s", domain.ErrInternalServerError, err.Error())
}
if len(list) == 0 {
span.RecordError(domain.ErrNotFound)
return nil, fmt.Errorf("user with email %s was not found: %w", email, domain.ErrNotFound)
}
span.SetAttributes(attribute.String("userid", list[0].ID.Hex()))
return list[0], nil
}