-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
executable file
·128 lines (115 loc) · 3.17 KB
/
service.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
package service
import (
"go.mongodb.org/mongo-driver/bson/primitive"
"log"
)
type Service interface {
Get(id string) (*Post, error)
Feed(first int, after string) (*[]Post, error)
Child(parentID string, first int, after string) (*[]Post, error)
Rooms(roomID string, first int, after string) (*[]Post, error)
Create(name string, avatar string, body string, parentID string, repostID string, roomID string) (*Post, error)
Upvote(accountID string, postID string) (*Post, error)
Downvote(accountID string, postID string) (*Post, error)
}
func NewService(repo Repository) Service {
return &service{
repo: repo,
}
}
type service struct {
repo Repository
}
func (i *service) Get(id string) (*Post, error) {
post, err := i.repo.FindByID(id)
if err != nil {
log.Println("Repository Error:", err)
return nil, err
}
return post, nil
}
func (i *service) Feed(first int, after string) (*[]Post, error) {
//TODO: first after default value in resolver, not implementation
postList, err := i.repo.FindByParentID("", first, after, true)
if err != nil {
log.Println("Repository Error:", err)
return nil, err
}
return postList, nil
}
func (i *service) Child(parentID string, first int, after string) (*[]Post, error) {
postList, err := i.repo.FindByParentID(parentID, first, after, false)
if err != nil {
log.Println("Repository Error:", err)
return nil, err
}
return postList, nil
}
func (i *service) Rooms(roomID string, first int, after string) (*[]Post, error) {
postList, err := i.repo.FindByRoomID(roomID, first, after, true)
if err != nil {
log.Println("Repository Error:", err)
return nil, err
}
return postList, nil
}
func (i *service) Create(name string, avatar string, body string, parentID string, repostID string, roomID string) (*Post, error) {
id := primitive.NewObjectID()
post := Post{
ID: id.Hex(),
Timestamp: int(id.Timestamp().Unix()),
Name: name,
Avatar: avatar,
Body: body,
ParentID: parentID,
RepostID: repostID,
RoomID: roomID,
UpvoterIDs: map[string]bool{},
DownvoterIDs: map[string]bool{},
ReplyCount: 0,
}
if err := i.repo.Save(post); err != nil{
log.Println("Repository Error:", err)
return nil, err
}
parent, err := i.repo.FindByID(parentID)
if err != nil{
log.Println("Repository Error:", err)
return nil, err
}
parent.ReplyCount++
i.repo.Save(*parent)
return &post, nil
}
func (i *service) Upvote(accountID string, postID string) (*Post, error) {
post, err := i.repo.FindByID(postID)
if err != nil {
log.Println("Repository Error:", err)
return nil, err
}
if post.IsDownvoted(accountID) {
post.Downvote(accountID)
}
post.Upvote(accountID)
if err := i.repo.Save(*post); err != nil{
log.Println("Repository Error:", err)
return nil, err
}
return post, nil
}
func (i *service) Downvote(accountID string, postID string) (*Post, error) {
post, err := i.repo.FindByID(postID)
if err != nil {
log.Println("Repository Error:", err)
return nil, err
}
if post.IsUpvoted(accountID) {
post.Upvote(accountID)
}
post.Downvote(accountID)
if err := i.repo.Save(*post); err != nil{
log.Println("Repository Error:", err)
return nil, err
}
return post, nil
}