forked from digisan/event-mgr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
participate.go
186 lines (162 loc) · 5.13 KB
/
participate.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
package eventmgr
import (
"errors"
"fmt"
"strings"
"github.com/dgraph-io/badger/v4"
bh "github.com/digisan/db-helper/badger"
. "github.com/digisan/go-generics"
lk "github.com/digisan/logkit"
)
// Participate a Post, such as thumb, vote etc
const (
SEP_V_Category = "##"
SEP_V_Participant = "^^"
SEP_V_Map = "::"
)
type EventParticipate struct {
evtID string // K: event id
mCatPtps map[string][]string // V: map for each category's statistic, category can be "thumb", "vote-item" etc. Participants is [user] list
fnDbStore func(*EventParticipate) error
}
func newEventParticipate(evtID string) *EventParticipate {
return &EventParticipate{
evtID: evtID,
mCatPtps: make(map[string][]string),
fnDbStore: bh.UpsertOneObject[EventParticipate],
}
}
func NewEventParticipate(evtID string, useExisting bool) (*EventParticipate, error) {
if p, err := Participate(evtID); err == nil && p != nil {
if useExisting {
return p, err
}
return nil, fmt.Errorf("event <%s> is already existing, cannot be NewEventParticipate", evtID)
}
return newEventParticipate(evtID), nil
}
func (ep EventParticipate) String() string {
sb := strings.Builder{}
sb.WriteString("--EventID: " + ep.evtID)
for cat, ptps := range ep.mCatPtps {
sb.WriteString(fmt.Sprintf("\n----Category:%s\tParticipants:%v", cat, ptps))
}
return sb.String()
}
func (ep *EventParticipate) Key() []byte {
return []byte(ep.evtID)
}
// cat1::user11^^user12^^user13##cat2::user21^^user22##...
func (ep *EventParticipate) Value() []byte {
sb := strings.Builder{}
for cat, ptps := range ep.mCatPtps {
sb.WriteString(cat + SEP_V_Map)
for i, p := range ptps {
sb.WriteString(IF(i < len(ptps)-1, p+SEP_V_Participant, p))
}
sb.WriteString(SEP_V_Category)
}
return []byte(strings.TrimSuffix(sb.String(), SEP_V_Category))
}
func (ep *EventParticipate) Marshal(at any) (forKey, forValue []byte) {
forKey = ep.Key()
lk.FailOnErrWhen(len(forKey) == 0, "%v", errors.New("empty event for participants"))
forValue = ep.Value()
return
}
func (ep *EventParticipate) Unmarshal(dbKey, dbVal []byte) (any, error) {
ep.evtID = string(dbKey)
dbValStr := string(dbVal)
if ep.mCatPtps == nil {
ep.mCatPtps = make(map[string][]string)
}
for _, catItem := range strings.Split(dbValStr, SEP_V_Category) {
ss := strings.SplitN(catItem, SEP_V_Map, 2)
cat, ptps := ss[0], ss[1]
ep.mCatPtps[cat] = IF(len(ptps) == 0, []string{}, strings.Split(ptps, SEP_V_Participant))
}
ep.fnDbStore = bh.UpsertOneObject[EventParticipate]
return ep, nil
}
func (ep *EventParticipate) BadgerDB() *badger.DB {
return DbGrp.IDPtps
}
/////////////////////////////////////////////////////////////////////////////
func (ep *EventParticipate) AddParticipants(category string, participants ...string) error {
if !EventHappened(ep.evtID) {
return fmt.Errorf("<%s> is not existing, cannot add participants", ep.evtID)
}
ep.mCatPtps[category] = append(ep.mCatPtps[category], participants...)
ep.mCatPtps[category] = Settify(ep.mCatPtps[category]...)
return ep.fnDbStore(ep)
}
func (ep *EventParticipate) RmParticipants(category string, toRemove ...string) (int, error) {
if !EventHappened(ep.evtID) {
return -1, fmt.Errorf("<%s> is not existing, cannot remove participants", ep.evtID)
}
ptps := ep.mCatPtps[category]
prevN := len(ptps)
ptps = FilterMap(ptps, func(i int, e string) bool {
return len(e) > 0 && NotIn(e, toRemove...)
}, func(i int, e string) string {
return e
})
ep.mCatPtps[category] = ptps
if err := ep.fnDbStore(ep); err != nil {
return -1, err
}
return prevN - len(ptps), nil
}
func (ep *EventParticipate) HasParticipant(category, participant string) bool {
ptps, err := ep.Participants(category)
if err != nil {
return false
}
return In(participant, ptps...)
}
func (ep *EventParticipate) ToggleParticipant(category, participant string) (bool, error) {
if ep.HasParticipant(category, participant) {
n, err := ep.RmParticipants(category, participant)
if err != nil {
return false, err
}
if n != 1 {
return false, fmt.Errorf("[%d] != 1, participant [%s] is not removed properly", n, participant)
}
return false, nil
} else {
if err := ep.AddParticipants(category, participant); err != nil {
return false, err
}
return true, nil
}
}
func (ep *EventParticipate) Participants(category string) ([]string, error) {
if !EventHappened(ep.evtID) {
return []string{}, fmt.Errorf("<%s> is not existing, cannot get participants", ep.evtID)
}
if ep.mCatPtps == nil {
return []string{}, nil
}
if _, ok := ep.mCatPtps[category]; !ok {
return []string{}, nil
}
return ep.mCatPtps[category], nil
}
////////////////////////////////////////////////////////////////////////////////////////////
func Participate(evtID string) (*EventParticipate, error) {
if !EventHappened(evtID) {
return nil, fmt.Errorf("<%s> is not existing, its participate cannot be fetched", evtID)
}
ep, err := bh.GetOneObject[EventParticipate]([]byte(evtID))
if err != nil {
return nil, err
}
if ep == nil {
ep = newEventParticipate(evtID)
}
return ep, err
}
func deleteParticipate(evtID string) (int, error) {
return bh.DeleteObjects[EventParticipate]([]byte(evtID))
}