feat: community RPC 从内存存储迁移到 ent 数据库
This commit is contained in:
@@ -1,45 +1,78 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/community/rpc/internal/models"
|
||||
"juwan-backend/app/community/rpc/pb"
|
||||
"juwan-backend/pkg/types"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
)
|
||||
|
||||
func nowUnix(in int64) int64 {
|
||||
if in > 0 {
|
||||
return in
|
||||
func toTextArray(s []string) types.TextArray {
|
||||
if len(s) == 0 {
|
||||
return types.TextArray{Valid: true}
|
||||
}
|
||||
return types.TextArray{
|
||||
Elements: s,
|
||||
Dims: []pgtype.ArrayDimension{{Length: int32(len(s)), LowerBound: 1}},
|
||||
Valid: true,
|
||||
}
|
||||
return time.Now().Unix()
|
||||
}
|
||||
|
||||
func postLikeKey(postID, userID int64) string {
|
||||
return strconv.FormatInt(postID, 10) + ":" + strconv.FormatInt(userID, 10)
|
||||
func entPostToPb(p *models.Posts) *pb.Posts {
|
||||
out := &pb.Posts{
|
||||
Id: p.ID,
|
||||
AuthorId: p.AuthorID,
|
||||
AuthorRole: p.AuthorRole,
|
||||
Title: p.Title,
|
||||
Content: p.Content,
|
||||
Images: p.Images.Elements,
|
||||
Tags: p.Tags.Elements,
|
||||
LikeCount: int64(p.LikeCount),
|
||||
CommentCount: int64(p.CommentCount),
|
||||
Pinned: p.Pinned,
|
||||
CreatedAt: p.CreatedAt.Unix(),
|
||||
UpdatedAt: p.UpdatedAt.Unix(),
|
||||
}
|
||||
if p.LinkedOrderID != nil {
|
||||
out.LinkedOrderId = *p.LinkedOrderID
|
||||
}
|
||||
if p.QuotedPostID != nil {
|
||||
out.QuotedPostId = *p.QuotedPostID
|
||||
}
|
||||
if p.DeletedAt != nil {
|
||||
out.DeletedAt = p.DeletedAt.Unix()
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func commentLikeKey(commentID, userID int64) string {
|
||||
return strconv.FormatInt(commentID, 10) + ":" + strconv.FormatInt(userID, 10)
|
||||
func entCommentToPb(c *models.Comments) *pb.Comments {
|
||||
out := &pb.Comments{
|
||||
Id: c.ID,
|
||||
PostId: c.PostID,
|
||||
AuthorId: c.AuthorID,
|
||||
Content: c.Content,
|
||||
LikeCount: int64(c.LikeCount),
|
||||
CreatedAt: c.CreatedAt.Unix(),
|
||||
}
|
||||
if c.DeletedAt != nil {
|
||||
out.DeletedAt = c.DeletedAt.Unix()
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func sortPostsDesc(posts []*pb.Posts) {
|
||||
sort.Slice(posts, func(i, j int) bool {
|
||||
if posts[i].Pinned != posts[j].Pinned {
|
||||
return posts[i].Pinned
|
||||
}
|
||||
if posts[i].CreatedAt == posts[j].CreatedAt {
|
||||
return posts[i].Id > posts[j].Id
|
||||
}
|
||||
return posts[i].CreatedAt > posts[j].CreatedAt
|
||||
})
|
||||
func entPostLikeToPb(l *models.PostLikes) *pb.PostLikes {
|
||||
return &pb.PostLikes{
|
||||
PostId: l.PostID,
|
||||
UserId: l.UserID,
|
||||
CreatedAt: l.CreatedAt.Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
func sortCommentsAsc(comments []*pb.Comments) {
|
||||
sort.Slice(comments, func(i, j int) bool {
|
||||
if comments[i].CreatedAt == comments[j].CreatedAt {
|
||||
return comments[i].Id < comments[j].Id
|
||||
}
|
||||
return comments[i].CreatedAt < comments[j].CreatedAt
|
||||
})
|
||||
func entCommentLikeToPb(l *models.CommentLikes) *pb.CommentLikes {
|
||||
return &pb.CommentLikes{
|
||||
CommentId: l.CommentID,
|
||||
UserId: l.UserID,
|
||||
CreatedAt: l.CreatedAt.Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user