Skip to content

Commit f08fab2

Browse files
committed
feat: add pagination support to GetPostById endpoint
1 parent 96c2ae9 commit f08fab2

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

controller/post_controller.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ func (c *postController) CreatePost(ctx *gin.Context) {
5252
}
5353

5454
func (c *postController) GetPostById(ctx *gin.Context) {
55+
var req dto.PaginationRequest
56+
if err := ctx.ShouldBind(&req); err != nil {
57+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
58+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
59+
return
60+
}
61+
5562
postIdStr := ctx.Param("post_id")
5663
postId, err := strconv.ParseUint(postIdStr, 10, 64)
5764
if err != nil {
@@ -60,7 +67,7 @@ func (c *postController) GetPostById(ctx *gin.Context) {
6067
return
6168
}
6269

63-
result, err := c.postService.GetPostById(ctx.Request.Context(), postId)
70+
result, err := c.postService.GetPostById(ctx.Request.Context(), postId, req)
6471
if err != nil {
6572
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_ID, err.Error(), nil)
6673
ctx.JSON(http.StatusBadRequest, res)

service/post_service.go

Lines changed: 3 additions & 3 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.PostRepliesPaginationResponse, error)
15+
GetPostById(ctx context.Context, postId uint64, req dto.PaginationRequest) (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)
@@ -72,13 +72,13 @@ func (s *postService) CreatePost(ctx context.Context, userId string, req dto.Pos
7272
}, nil
7373
}
7474

75-
func (s *postService) GetPostById(ctx context.Context, postId uint64) (dto.PostRepliesPaginationResponse, error) {
75+
func (s *postService) GetPostById(ctx context.Context, postId uint64, req dto.PaginationRequest) (dto.PostRepliesPaginationResponse, error) {
7676
post, err := s.postRepo.GetPostById(ctx, nil, postId)
7777
if err != nil {
7878
return dto.PostRepliesPaginationResponse{}, dto.ErrGetPostById
7979
}
8080

81-
replies, err := s.postRepo.GetAllPostRepliesWithPagination(ctx, nil, postId, dto.PaginationRequest{})
81+
replies, err := s.postRepo.GetAllPostRepliesWithPagination(ctx, nil, postId, req)
8282
if err != nil {
8383
return dto.PostRepliesPaginationResponse{}, dto.ErrGetPostReplies
8484
}

0 commit comments

Comments
 (0)