mirror of
https://github.com/wgh136/nysoure.git
synced 2025-12-16 15:51:14 +00:00
Add replies field to CommentView and update logic to fetch comment replies
This commit is contained in:
@@ -25,13 +25,14 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type CommentView struct {
|
type CommentView struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
Content string `json:"content"`
|
Content string `json:"content"`
|
||||||
CreatedAt time.Time `json:"created_at"`
|
CreatedAt time.Time `json:"created_at"`
|
||||||
User UserView `json:"user"`
|
User UserView `json:"user"`
|
||||||
Images []ImageView `json:"images"`
|
Images []ImageView `json:"images"`
|
||||||
ReplyCount uint `json:"reply_count"`
|
ReplyCount uint `json:"reply_count"`
|
||||||
ContentTruncated bool `json:"content_truncated"`
|
ContentTruncated bool `json:"content_truncated"`
|
||||||
|
Replies []CommentView `json:"replies,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Comment) ToView() *CommentView {
|
func (c *Comment) ToView() *CommentView {
|
||||||
|
|||||||
@@ -122,9 +122,18 @@ func ListResourceComments(resourceID uint, page int) ([]model.CommentView, int,
|
|||||||
res := make([]model.CommentView, 0, len(comments))
|
res := make([]model.CommentView, 0, len(comments))
|
||||||
for _, c := range comments {
|
for _, c := range comments {
|
||||||
v := *c.ToView()
|
v := *c.ToView()
|
||||||
var truncated bool
|
v.Content, v.ContentTruncated = restrictCommentLength(v.Content)
|
||||||
v.Content, truncated = restrictCommentLength(v.Content)
|
replies, _, err := dao.GetCommentReplies(c.ID, 1, 3)
|
||||||
v.ContentTruncated = truncated
|
if err != nil {
|
||||||
|
log.Error("Error getting replies for comment:", err)
|
||||||
|
return nil, 0, model.NewInternalServerError("Error getting replies for comment")
|
||||||
|
}
|
||||||
|
v.Replies = make([]model.CommentView, 0, len(replies))
|
||||||
|
for _, r := range replies {
|
||||||
|
rv := *r.ToView()
|
||||||
|
rv.Content, rv.ContentTruncated = restrictCommentLength(rv.Content)
|
||||||
|
v.Replies = append(v.Replies, rv)
|
||||||
|
}
|
||||||
res = append(res, v)
|
res = append(res, v)
|
||||||
}
|
}
|
||||||
return res, totalPages, nil
|
return res, totalPages, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user