-
Notifications
You must be signed in to change notification settings - Fork 4
/
user_repo.go
73 lines (64 loc) · 1.75 KB
/
user_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
// Code generated by repogen. DO NOT EDIT.
package main
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func NewUserRepository(collection *mongo.Collection) UserRepository {
return &UserRepositoryMongo{
collection: collection,
}
}
type UserRepositoryMongo struct {
collection *mongo.Collection
}
func (r *UserRepositoryMongo) InsertOne(arg0 context.Context, arg1 *UserModel) (interface{}, error) {
result, err := r.collection.InsertOne(arg0, arg1)
if err != nil {
return nil, err
}
return result.InsertedID, nil
}
func (r *UserRepositoryMongo) FindByUsername(arg0 context.Context, arg1 string) (*UserModel, error) {
var entity UserModel
if err := r.collection.FindOne(arg0, bson.M{
"username": arg1,
}, options.FindOne().SetSort(bson.M{})).Decode(&entity); err != nil {
return nil, err
}
return &entity, nil
}
func (r *UserRepositoryMongo) UpdateDisplayNameByID(arg0 context.Context, arg1 string, arg2 primitive.ObjectID) (bool, error) {
result, err := r.collection.UpdateOne(arg0, bson.M{
"_id": arg2,
}, bson.M{
"$set": bson.M{
"display_name": arg1,
},
})
if err != nil {
return false, err
}
return result.MatchedCount > 0, nil
}
func (r *UserRepositoryMongo) DeleteByCity(arg0 context.Context, arg1 string) (int, error) {
result, err := r.collection.DeleteMany(arg0, bson.M{
"city": arg1,
})
if err != nil {
return 0, err
}
return int(result.DeletedCount), nil
}
func (r *UserRepositoryMongo) CountByCity(arg0 context.Context, arg1 string) (int, error) {
count, err := r.collection.CountDocuments(arg0, bson.M{
"city": arg1,
})
if err != nil {
return 0, err
}
return int(count), nil
}