-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmembers.go
111 lines (96 loc) · 2.85 KB
/
members.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
package stores
import (
"context"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
type MembersStore struct {
*store
}
const MEMBERS Collection = "members"
func newMembersStore(ctx context.Context, client *mongo.Client, database string) *MembersStore {
_ = client.Database(database).CreateCollection(ctx, string(MEMBERS))
s := &store{
Collection: client.Database(database).Collection(string(MEMBERS)),
ctx: ctx,
}
return &MembersStore{s}
}
func (c *Client) GetMembersStore() (*MembersStore, bool) {
storeInterface, ok := c.GetCollection(MEMBERS)
if !ok {
return nil, false
}
return storeInterface.(*MembersStore), ok
}
func (s *MembersStore) Get(id string) (*mongo.Cursor, error) {
return s.Aggregate(s.ctx, bson.A{
bson.D{{Key: "$match", Value: bson.D{{Key: "_id", Value: id}}}},
bson.D{{Key: "$lookup", Value: bson.D{
{Key: "from", Value: "members"},
{Key: "localField", Value: "recruiter"},
{Key: "foreignField", Value: "_id"},
{Key: "as", Value: "recruiter"},
}}},
bson.D{{Key: "$unwind", Value: bson.D{{Key: "path", Value: "$recruiter"}, {Key: "preserveNullAndEmptyArrays", Value: true}}}},
})
}
func (s *MembersStore) GetRandom(max int, maxRank int) ([]map[string]interface{}, error) {
cur, err := s.Aggregate(s.ctx, bson.A{
bson.D{
{Key: "$match",
Value: bson.D{
{Key: "rank",
Value: bson.D{
{Key: "$lte", Value: maxRank},
{Key: "$ne", Value: 0},
},
},
},
},
},
bson.D{{Key: "$sample", Value: bson.D{{Key: "size", Value: max}}}},
})
if err != nil {
return nil, err
}
members := []map[string]interface{}{}
for cur.Next(s.ctx) {
member := map[string]interface{}{}
if err := cur.Decode(&member); err != nil {
return nil, err
}
members = append(members, member)
}
return members, nil
}
func (s *MembersStore) List(filter interface{}, page, max int) (*mongo.Cursor, error) {
pipeline := bson.A{
bson.D{{Key: "$match", Value: filter}},
bson.D{{Key: "$lookup", Value: bson.D{
{Key: "from", Value: "members"},
{Key: "localField", Value: "recruiter"},
{Key: "foreignField", Value: "_id"},
{Key: "as", Value: "recruiter"},
}}},
bson.D{{Key: "$unwind", Value: bson.D{{Key: "path", Value: "$recruiter"}, {Key: "preserveNullAndEmptyArrays", Value: true}}}},
}
if page > 0 {
pipeline = append(pipeline,
bson.D{{Key: "$skip", Value: (page - 1) * max}},
bson.D{{Key: "$limit", Value: max}},
)
}
return s.Aggregate(s.ctx, pipeline)
}
func (s *MembersStore) Upsert(id string, member any) error {
opts := options.Replace().SetUpsert(true)
if _, err := s.ReplaceOne(s.ctx, bson.D{{Key: "_id", Value: id}}, member, opts); err != nil {
return err
}
return nil
}
func (s *MembersStore) Delete(id string) error {
return s.FindOneAndDelete(s.ctx, bson.D{{Key: "_id", Value: id}}).Err()
}