generated from CyanAsterisk/.github
/
mongo.go
158 lines (144 loc) · 4.01 KB
/
mongo.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
package mongo
import (
"context"
"github.com/CyanAsterisk/FreeCar/server/shared/consts"
"github.com/CyanAsterisk/FreeCar/server/shared/errno"
"github.com/CyanAsterisk/FreeCar/server/shared/id"
"github.com/CyanAsterisk/FreeCar/server/shared/kitex_gen/base"
mgutil "github.com/CyanAsterisk/FreeCar/server/shared/mongo"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type Manager struct {
col *mongo.Collection
}
const (
accountIDField = "accountid"
profileField = "profile"
identityStatusField = profileField + ".identitystatus"
photoBlobIDField = "photoblobid"
)
// ProfileRecord defines the profile record in db.
type ProfileRecord struct {
AccountID string `bson:"accountid"`
Profile *base.Profile `bson:"profile"`
PhotoBlobID string `bson:"photoblobid"`
}
// NewManager creates a mongo manager.
func NewManager(db *mongo.Database) *Manager {
return &Manager{col: db.Collection(consts.ProfileCollection)}
}
// GetProfile gets profile for an account.
func (m *Manager) GetProfile(c context.Context, aid id.AccountID) (*ProfileRecord, error) {
res := m.col.FindOne(c, byAccountID(aid))
if err := res.Err(); err != nil {
if err == mongo.ErrNoDocuments {
return nil, errno.RecordNotFound
}
return nil, err
}
var pr ProfileRecord
err := res.Decode(&pr)
if err != nil {
return nil, err
}
return &pr, nil
}
// GetProfiles gets profiles.
func (m *Manager) GetProfiles(c context.Context, limit int64) ([]*ProfileRecord, error) {
filter := bson.M{}
opt := options.Find().SetLimit(limit)
res, err := m.col.Find(c, filter, opt)
if err != nil {
return nil, err
}
var pfs []*ProfileRecord
for res.Next(c) {
var pr ProfileRecord
err := res.Decode(&pr)
if err != nil {
return nil, err
}
pfs = append(pfs, &pr)
}
return pfs, nil
}
// GetPendingProfiles gets peding profiles.
func (m *Manager) GetPendingProfiles(c context.Context) ([]*ProfileRecord, error) {
filter := bson.M{
identityStatusField: base.IdentityStatus_PENDING,
}
opt := options.Find()
res, err := m.col.Find(c, filter, opt)
if err != nil {
return nil, err
}
var pfs []*ProfileRecord
for res.Next(c) {
var pr ProfileRecord
err := res.Decode(&pr)
if err != nil {
return nil, err
}
pfs = append(pfs, &pr)
}
return pfs, nil
}
// UpdateProfile updates profile for an account.
func (m *Manager) UpdateProfile(c context.Context, aid id.AccountID, prevState base.IdentityStatus, p *base.Profile) error {
filter := bson.M{
identityStatusField: prevState,
}
if prevState == base.IdentityStatus_UNSUBMITTED {
filter = mgutil.ZeroOrDoesNotExist(identityStatusField, prevState)
}
filter[accountIDField] = aid
_, err := m.col.UpdateOne(c, filter, mgutil.Set(bson.M{
accountIDField: aid,
profileField: p,
}), options.Update().SetUpsert(true))
if mongo.IsDuplicateKeyError(err) {
return errno.RecordAlreadyExist
}
return err
}
// UpdateProfilePhoto updates profile photo blob id.
func (m *Manager) UpdateProfilePhoto(c context.Context, aid id.AccountID, bid id.BlobID) error {
_, err := m.col.UpdateOne(c, bson.M{
accountIDField: aid,
}, mgutil.Set(bson.M{
accountIDField: aid,
photoBlobIDField: bid,
}), options.Update().SetUpsert(true))
return err
}
// UpdateProfileStatus updates profile status.
func (m *Manager) UpdateProfileStatus(c context.Context, aid id.AccountID, status base.IdentityStatus) error {
_, err := m.col.UpdateOne(c, bson.M{
accountIDField: aid.String(),
}, mgutil.Set(bson.M{
accountIDField: aid.String(),
identityStatusField: status,
}), options.Update().SetUpsert(true))
return err
}
// DeleteProfile delete profile
func (m *Manager) DeleteProfile(c context.Context, aid id.AccountID) error {
filter := bson.M{
accountIDField: aid.String(),
}
result, err := m.col.DeleteOne(c, filter)
if err != nil {
return err
}
if result.DeletedCount == 0 {
return errno.RecordNotFound
}
return nil
}
func byAccountID(aid id.AccountID) bson.M {
return bson.M{
accountIDField: aid,
}
}