/
user_repo.go
125 lines (105 loc) · 2.88 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
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
package repository
import (
"context"
"fmt"
"github.com/aivative/login-dev/config"
"github.com/aivative/login-dev/repository/model"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
func NewUserRepo(client *mongo.Client) *UserMongoRepo {
mongoConfig := config.MongoConf["mongo-user"]
collection := client.Database(mongoConfig.DBName).Collection(mongoConfig.CollName)
return &UserMongoRepo{
coll: collection,
}
}
type UserMongoRepo struct {
coll *mongo.Collection
}
func (umr *UserMongoRepo) GetUserLoginInfo(ctx context.Context, loginReq model.TLoginReq, userResult *model.TUser) (err error) {
if err = umr.coll.FindOne(ctx, gin.H{"email": loginReq.Email}).
Decode(userResult); err != nil {
return
} else if err != nil {
return
}
return
}
func (umr *UserMongoRepo) CreateUser(ctx context.Context, userPayload model.TCreateUserQuery) error {
ir, err := umr.coll.InsertOne(ctx, userPayload)
if err != nil {
return err
}
if ir.InsertedID == primitive.NilObjectID {
return fmt.Errorf("can't insert user")
}
return nil
}
func (umr *UserMongoRepo) GetUser(ctx context.Context, uid string) (result model.TGetUserResp, err error) {
err = umr.coll.FindOne(ctx, model.TUserID{UserID: uid}).Decode(&result)
return
}
func (umr *UserMongoRepo) DeleteUser(ctx context.Context, uid string) error {
dr, err := umr.coll.DeleteOne(ctx, model.TUserID{UserID: uid})
if err != nil {
return err
}
if dr.DeletedCount == 0 {
return fmt.Errorf("can't delete user")
}
return nil
}
func (umr *UserMongoRepo) UpdateUser(ctx context.Context, uid string, profile model.TUpdateUserReq) error {
ur, err := umr.coll.UpdateOne(ctx, model.TUserID{UserID: uid}, profile)
if err != nil {
return err
}
if ur.MatchedCount == 0 {
return fmt.Errorf("user not found")
}
nilStruct := model.TUpdateUserReq{}
if ur.ModifiedCount == 0 && profile != nilStruct {
return fmt.Errorf("user not updated")
}
return nil
}
// // GetUsers
// func (r *UserMongoRepo) GetUsers(offset int64, result interface{}) error {
// var opt options.FindOptions
// opt.SetLimit(10).SetSkip(offset * 10)
//
// cursor, err := r.coll.Find(r.ctx, bson.M{"user_type": bson.M{"$ne": "ADMIN"}}, &opt)
// if err != nil {
// return err
// }
//
// if cursor.RemainingBatchLength() == 0 {
// return mongo.ErrNoDocuments
// }
//
// if err := cursor.All(r.ctx, result); err != nil {
// return err
// }
//
// return nil
// }
//
// // GetUsersByDistrictID
// func (r *UserMongoRepo) GetUsersByDistrictID(districtID string, result interface{}) error {
// cursor, err := r.coll.Find(r.ctx, bson.M{"district_id": districtID, "user_type": "GARBO"})
// if err != nil {
// return err
// }
//
// if cursor.RemainingBatchLength() == 0 {
// return mongo.ErrNoDocuments
// }
//
// if err := cursor.All(r.ctx, result); err != nil {
// return err
// }
//
// return nil
// }