-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.go
executable file
·48 lines (46 loc) · 984 Bytes
/
model.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
package service
type Post struct {
ID string
Timestamp int
Name string
Avatar string
Body string
ParentID string
RepostID string
RoomID string
UpvoterIDs map[string]bool
DownvoterIDs map[string]bool
ReplyCount int
}
func (p *Post) Upvote(accountID string) bool {
voted := p.IsUpvoted(accountID)
if !voted {
p.UpvoterIDs[accountID] = true
} else {
delete(p.UpvoterIDs, accountID)
}
return voted
}
func (p *Post) Downvote(accountID string) bool {
voted := p.IsDownvoted(accountID)
if !voted {
p.DownvoterIDs[accountID] = true
} else {
delete(p.DownvoterIDs, accountID)
}
return voted
}
func (p Post) IsUpvoted(accountID string) bool {
_, ok := p.UpvoterIDs[accountID]
return ok
}
func (p Post) IsDownvoted(accountID string) bool {
_, ok := p.DownvoterIDs[accountID]
return ok
}
func (p Post) DownvoteCount() int {
return len(p.DownvoterIDs)
}
func (p Post) UpvoteCount() int {
return len(p.UpvoterIDs)
}