-
Notifications
You must be signed in to change notification settings - Fork 3
/
lookup_pic_comment_vote_handler.go
102 lines (85 loc) · 2.44 KB
/
lookup_pic_comment_vote_handler.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
package handlers
import (
"context"
"pixur.org/pixur/api"
"pixur.org/pixur/be/schema"
"pixur.org/pixur/be/status"
"pixur.org/pixur/be/tasks"
)
// TODO: add tests
func (s *serv) handleLookupPicCommentVote(ctx context.Context, req *api.LookupPicCommentVoteRequest) (
*api.LookupPicCommentVoteResponse, status.S) {
var picId schema.Varint
if req.PicId != "" {
if err := picId.DecodeAll(req.PicId); err != nil {
return nil, status.InvalidArgument(err, "Unable to decode pic id")
}
}
var commentId schema.Varint
if req.CommentId != "" {
if err := commentId.DecodeAll(req.CommentId); err != nil {
return nil, status.InvalidArgument(err, "Unable to decode comment id")
}
}
var userId schema.Varint
if req.UserId != "" {
if err := userId.DecodeAll(req.UserId); err != nil {
return nil, status.InvalidArgument(err, "Unable to decode user id")
}
}
task := &tasks.FindPicCommentVotesTask{
Beg: s.db,
Now: s.now,
PicId: int64(picId),
// this is wrong, fix it
CommentId: int64Addr(int64(commentId)),
ObjectUserId: int64Addr(int64(userId)),
}
if sts := s.runner.Run(ctx, task); sts != nil {
return nil, sts
}
if task.PicCommentVote == nil {
return &api.LookupPicCommentVoteResponse{}, nil
}
// todo: fix
return &api.LookupPicCommentVoteResponse{
Vote: apiPicCommentVote(task.PicCommentVote[0]),
}, nil
}
// TODO: add tests
func (s *serv) handleFindPicCommentVotes(ctx context.Context, req *api.FindPicCommentVotesRequest) (
*api.FindPicCommentVotesResponse, status.S) {
var picId schema.Varint
if req.PicId != "" {
if err := picId.DecodeAll(req.PicId); err != nil {
return nil, status.InvalidArgument(err, "Unable to decode pic id")
}
}
task := &tasks.FindPicCommentVotesTask{
Beg: s.db,
PicId: int64(picId),
}
if req.CommentId != "" {
var commentId schema.Varint
if err := commentId.DecodeAll(req.CommentId); err != nil {
return nil, status.InvalidArgument(err, "Unable to decode comment id")
}
task.CommentId = int64Addr(int64(commentId))
}
if req.UserId != "" {
var userId schema.Varint
if err := userId.DecodeAll(req.UserId); err != nil {
return nil, status.InvalidArgument(err, "Unable to decode user id")
}
task.ObjectUserId = int64Addr(int64(userId))
}
if sts := s.runner.Run(ctx, task); sts != nil {
return nil, sts
}
return &api.FindPicCommentVotesResponse{
Vote: apiPicCommentVotes(nil, task.PicCommentVote),
}, nil
}
func int64Addr(n int64) *int64 {
return &n
}