Skip to content

Commit ae1a4d6

Browse files
committed
feat: update GetPostById to return post replies with pagination
1 parent 1e79a7d commit ae1a4d6

File tree

3 files changed

+40
-34
lines changed

3 files changed

+40
-34
lines changed

controller/post_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func (c *postController) CreatePost(ctx *gin.Context) {
5050
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_CREATE_POST, result)
5151
ctx.JSON(http.StatusOK, res)
5252
}
53+
5354
func (c *postController) GetPostById(ctx *gin.Context) {
5455
postIdStr := ctx.Param("post_id")
5556
postId, err := strconv.ParseUint(postIdStr, 10, 64)
@@ -66,7 +67,13 @@ func (c *postController) GetPostById(ctx *gin.Context) {
6667
return
6768
}
6869

69-
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_GET_POST_BY_ID, result)
70+
res := utils.Response{
71+
Status: true,
72+
Message: dto.MESSAGE_SUCCESS_GET_POST_BY_ID,
73+
Data: result.Data,
74+
Meta: result.PaginationResponse,
75+
}
76+
7077
ctx.JSON(http.StatusOK, res)
7178
}
7279

dto/post_dto.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,11 @@ type (
3838
}
3939

4040
PostResponse struct {
41-
ID uint64 `json:"id"`
42-
Text string `json:"text"`
43-
ParentID *uint64 `json:"parent_id"`
44-
User UserResponse `json:"user"`
45-
}
46-
47-
PostRepliesResponse struct {
48-
PostResponse
49-
Replies []PostResponse `json:"replies"`
50-
PaginationResponse
41+
ID uint64 `json:"id"`
42+
Text string `json:"text"`
43+
ParentID *uint64 `json:"parent_id"`
44+
User UserResponse `json:"user"`
45+
Replies []PostResponse `json:"replies,omitempty"`
5146
}
5247

5348
PostUpdateRequest struct {
@@ -59,6 +54,11 @@ type (
5954
PaginationResponse
6055
}
6156

57+
PostRepliesPaginationResponse struct {
58+
Data PostResponse `json:"data"`
59+
PaginationResponse
60+
}
61+
6262
GetAllPostsRepositoryResponse struct {
6363
Posts []entity.Post `json:"posts"`
6464
PaginationResponse

service/post_service.go

Lines changed: 22 additions & 23 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 uint64) (dto.PostRepliesResponse, error)
15+
GetPostById(ctx context.Context, postId uint64) (dto.PostRepliesPaginationResponse, error)
1616
DeletePostById(ctx context.Context, postId uint64) error
1717
UpdatePostById(ctx context.Context, userId string, postId uint64, req dto.PostUpdateRequest) (dto.PostResponse, error)
1818
GetAllPosts(ctx context.Context, req dto.PaginationRequest) (dto.PostPaginationResponse, error)
@@ -71,15 +71,15 @@ 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 uint64) (dto.PostRepliesResponse, error) {
74+
func (s *postService) GetPostById(ctx context.Context, postId uint64) (dto.PostRepliesPaginationResponse, error) {
7575
post, err := s.postRepo.GetPostById(ctx, nil, postId)
7676
if err != nil {
77-
return dto.PostRepliesResponse{}, dto.ErrGetPostById
77+
return dto.PostRepliesPaginationResponse{}, dto.ErrGetPostById
7878
}
7979

8080
replies, err := s.postRepo.GetAllPostRepliesWithPagination(ctx, nil, postId, dto.PaginationRequest{})
8181
if err != nil {
82-
return dto.PostRepliesResponse{}, dto.ErrGetPostReplies
82+
return dto.PostRepliesPaginationResponse{}, dto.ErrGetPostReplies
8383
}
8484

8585
var data []dto.PostResponse
@@ -100,28 +100,27 @@ func (s *postService) GetPostById(ctx context.Context, postId uint64) (dto.PostR
100100
data = append(data, datum)
101101
}
102102

103-
return dto.PostRepliesResponse{
104-
PostResponse: dto.PostResponse{
105-
ID: post.ID,
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-
},
103+
return dto.PostRepliesPaginationResponse{
104+
Data: dto.PostResponse{
105+
ID: post.ID,
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,
115114
},
116115
Replies: data,
117-
PaginationResponse: dto.PaginationResponse{
118-
Page: replies.Page,
119-
PerPage: replies.PerPage,
120-
MaxPage: replies.MaxPage,
121-
Count: replies.Count,
122-
},
123116
},
124-
nil
117+
PaginationResponse: dto.PaginationResponse{
118+
Page: replies.Page,
119+
PerPage: replies.PerPage,
120+
MaxPage: replies.MaxPage,
121+
Count: replies.Count,
122+
},
123+
}, nil
125124
}
126125

127126
func (s *postService) DeletePostById(ctx context.Context, postId uint64) error {

0 commit comments

Comments
 (0)