Skip to content

Commit 7e75210

Browse files
committed
feat: add retrieve post replies with pagination
1 parent d930158 commit 7e75210

File tree

3 files changed

+96
-14
lines changed

3 files changed

+96
-14
lines changed

dto/post_dto.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
var (
2727
ErrCreatePost = errors.New("failed to create post")
2828
ErrGetPostById = errors.New("post not found")
29+
ErrGetPostReplies = errors.New("failed to get post replies")
2930
ErrParseParentID = errors.New("failed to parse parent id")
3031
ErrDeletePostById = errors.New("failed to delete post")
3132
ErrUpdatePostById = errors.New("failed to update post")
@@ -44,6 +45,12 @@ type (
4445
User UserResponse `json:"user"`
4546
}
4647

48+
PostRepliesResponse struct {
49+
PostResponse
50+
Replies []PostResponse `json:"replies"`
51+
PaginationResponse
52+
}
53+
4754
PostUpdateRequest struct {
4855
Text string `json:"text" form:"text" binding:"required"`
4956
}
@@ -57,4 +64,9 @@ type (
5764
Posts []entity.Post `json:"posts"`
5865
PaginationResponse
5966
}
67+
68+
GetAllRepliesRepositoryResponse struct {
69+
Replies []entity.Post `json:"replies"`
70+
PaginationResponse
71+
}
6072
)

repository/post_repository.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type (
1616
DeletePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) error
1717
UpdatePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID, post entity.Post) (entity.Post, error)
1818
GetAllPostsWithPagination(ctx context.Context, tx *gorm.DB, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
19+
GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uuid.UUID, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error)
1920
}
2021

2122
postRepository struct {
@@ -124,3 +125,39 @@ func (r *postRepository) GetAllPostsWithPagination(ctx context.Context, tx *gorm
124125
},
125126
}, err
126127
}
128+
129+
func (r *postRepository) GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uuid.UUID, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error) {
130+
if tx == nil {
131+
tx = r.db
132+
}
133+
134+
var replies []entity.Post
135+
var err error
136+
var count int64
137+
138+
req.Default()
139+
140+
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Where("posts.parent_id = ?", postId).Order("created_at DESC")
141+
if req.Search != "" {
142+
query = query.Where("text LIKE ?", "%"+req.Search+"%")
143+
}
144+
145+
if err := query.Count(&count).Error; err != nil {
146+
return dto.GetAllRepliesRepositoryResponse{}, err
147+
}
148+
149+
if err := query.Scopes(Paginate(req)).Find(&replies).Error; err != nil {
150+
return dto.GetAllRepliesRepositoryResponse{}, err
151+
}
152+
153+
totalPage := TotalPage(count, int64(req.PerPage))
154+
return dto.GetAllRepliesRepositoryResponse{
155+
Replies: replies,
156+
PaginationResponse: dto.PaginationResponse{
157+
Page: req.Page,
158+
PerPage: req.PerPage,
159+
Count: count,
160+
MaxPage: totalPage,
161+
},
162+
}, err
163+
}

service/post_service.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
type (
1313
PostService interface {
1414
CreatePost(ctx context.Context, userId string, req dto.PostCreateRequest) (dto.PostResponse, error)
15-
GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostResponse, error)
15+
GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostRepliesResponse, error)
1616
DeletePostById(ctx context.Context, postId uuid.UUID) error
1717
UpdatePostById(ctx context.Context, userId string, postId uuid.UUID, req dto.PostUpdateRequest) (dto.PostResponse, error)
1818
GetAllPosts(ctx context.Context, req dto.PaginationRequest) (dto.PostPaginationResponse, error)
@@ -71,24 +71,57 @@ func (s *postService) CreatePost(ctx context.Context, userId string, req dto.Pos
7171
}, nil
7272
}
7373

74-
func (s *postService) GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostResponse, error) {
74+
func (s *postService) GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostRepliesResponse, error) {
7575
post, err := s.postRepo.GetPostById(ctx, nil, postId)
7676
if err != nil {
77-
return dto.PostResponse{}, dto.ErrGetPostById
77+
return dto.PostRepliesResponse{}, dto.ErrGetPostById
7878
}
7979

80-
return dto.PostResponse{
81-
ID: post.ID.String(),
82-
Text: post.Text,
83-
ParentID: post.ParentID,
84-
User: dto.UserResponse{
85-
ID: post.UserID.String(),
86-
Name: post.User.Name,
87-
Bio: post.User.Bio,
88-
UserName: post.User.Username,
89-
ImageUrl: post.User.ImageUrl,
80+
replies, err := s.postRepo.GetAllPostRepliesWithPagination(ctx, nil, postId, dto.PaginationRequest{})
81+
if err != nil {
82+
return dto.PostRepliesResponse{}, dto.ErrGetPostReplies
83+
}
84+
85+
var data []dto.PostResponse
86+
for _, reply := range replies.Replies {
87+
datum := dto.PostResponse{
88+
ID: reply.ID.String(),
89+
Text: reply.Text,
90+
ParentID: reply.ParentID,
91+
User: dto.UserResponse{
92+
ID: reply.UserID.String(),
93+
Name: reply.User.Name,
94+
Bio: reply.User.Bio,
95+
UserName: reply.User.Username,
96+
ImageUrl: reply.User.ImageUrl,
97+
},
98+
}
99+
100+
data = append(data, datum)
101+
}
102+
103+
return dto.PostRepliesResponse{
104+
PostResponse: dto.PostResponse{
105+
ID: post.ID.String(),
106+
Text: post.Text,
107+
ParentID: post.ParentID,
108+
User: dto.UserResponse{
109+
ID: post.UserID.String(),
110+
Name: post.User.Name,
111+
Bio: post.User.Bio,
112+
UserName: post.User.Username,
113+
ImageUrl: post.User.ImageUrl,
114+
},
115+
},
116+
Replies: data,
117+
PaginationResponse: dto.PaginationResponse{
118+
Page: replies.Page,
119+
PerPage: replies.PerPage,
120+
MaxPage: replies.MaxPage,
121+
Count: replies.Count,
122+
},
90123
},
91-
}, nil
124+
nil
92125
}
93126

94127
func (s *postService) DeletePostById(ctx context.Context, postId uuid.UUID) error {

0 commit comments

Comments
 (0)