-
Notifications
You must be signed in to change notification settings - Fork 0
/
comment.go
85 lines (77 loc) · 2.62 KB
/
comment.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
package flags
import (
"context"
"log"
"time"
"github.com/leepala/OldGeneralBackend/Proto/cdr"
flagspb "github.com/leepala/OldGeneralBackend/Proto/flags"
userpb "github.com/leepala/OldGeneralBackend/Proto/user"
"github.com/leepala/OldGeneralBackend/pkg/database"
"github.com/leepala/OldGeneralBackend/pkg/helper"
"github.com/leepala/OldGeneralBackend/pkg/model"
"github.com/leepala/OldGeneralBackend/pkg/user"
)
func (s *server) PostComment(ctx context.Context, in *flagspb.PostCommentRequest) (*flagspb.PostCommentReply, error) {
log.Println("post comment request", in)
comment, err := helper.TypeConverter[model.Comment](in.Comment)
if err != nil {
log.Println("error converting comment", err)
return nil, err
}
comment.UserID = in.Comment.UserInfo.Id
err = database.GetDB().Model(&comment).Save(&comment).Error
if err != nil {
log.Println("error saving comment", err)
return nil, err
}
var reply = &flagspb.PostCommentReply{
RequestId: in.RequestId,
ReplyTime: time.Now().UnixMicro(),
}
return reply, nil
}
func (s *server) FetchComment(ctx context.Context, in *flagspb.FetchCommentRequest) (*flagspb.FetchCommentReply, error) {
log.Println("fetch comment request", in)
var lastCommentTimeStamp int64 = time.Now().UnixMicro() + 1
if in.LastCommentId != "" {
var lastSignInId model.FlagInfo
err := database.GetDB().Model(&model.Comment{}).Where("id = ?", in.LastCommentId).Find(&lastSignInId).Error
if err != nil {
log.Println("error getting last sign in id", err)
return nil, err
}
lastCommentTimeStamp = lastSignInId.CreatedAt
}
var comments []model.Comment
err := database.GetDB().Model(&model.Comment{}).Where("signin_id = ?", in.SigninId).Where("created_at < ?", lastCommentTimeStamp).Order("created_at desc").Limit(int(in.PageSize)).Find(&comments).Error
if err != nil {
log.Println("error getting comments", err)
return nil, err
}
searchUserReq := &userpb.GetUserInfoRequest{
RequestId: in.RequestId,
RequestTime: in.RequestTime,
}
var commentpbs []*cdr.CommentInfo
for _, comment := range comments {
commentpb, err := helper.TypeConverter[cdr.CommentInfo](comment)
if err != nil {
log.Println("error converting comment", err)
return nil, err
}
searchUserReq.UserId = comment.UserID
reply, err := user.GetClient().GetUserInfo(ctx, searchUserReq)
if err != nil {
log.Println("error getting user info", err)
return nil, err
}
commentpb.UserInfo = reply.UserInfo
commentpbs = append(commentpbs, commentpb)
}
var reply = &flagspb.FetchCommentReply{
RequestId: in.RequestId,
ReplyTime: time.Now().UnixMicro(),
Comments: commentpbs,
}
return reply, nil
}