Skip to content

Commit 1f60023

Browse files
committed
feat: add get all posts endpoint
1 parent a9f6e90 commit 1f60023

File tree

5 files changed

+115
-1
lines changed

5 files changed

+115
-1
lines changed

controller/post_controller.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type (
1717
GetPostById(ctx *gin.Context)
1818
DeletePostById(ctx *gin.Context)
1919
UpdatePostById(ctx *gin.Context)
20+
GetAllPosts(ctx *gin.Context)
2021
}
2122

2223
postController struct {
@@ -113,3 +114,28 @@ func (c *postController) UpdatePostById(ctx *gin.Context) {
113114
res := utils.BuildResponseSuccess(dto.MESSAGE_SUCCESS_UPDATE_POST, result)
114115
ctx.JSON(http.StatusOK, res)
115116
}
117+
118+
func (c *postController) GetAllPosts(ctx *gin.Context) {
119+
var req dto.PaginationRequest
120+
if err := ctx.ShouldBind(&req); err != nil {
121+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
122+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
123+
return
124+
}
125+
126+
posts, err := c.postService.GetAllPosts(ctx.Request.Context(), req)
127+
if err != nil {
128+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_ALL_POSTS, err.Error(), nil)
129+
ctx.JSON(http.StatusBadRequest, res)
130+
return
131+
}
132+
133+
res := utils.Response{
134+
Status: true,
135+
Message: dto.MESSAGE_SUCCESS_GET_ALL_POSTS,
136+
Data: posts.Data,
137+
Meta: posts.PaginationResponse,
138+
}
139+
140+
ctx.JSON(http.StatusOK, res)
141+
}

dto/post_dto.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package dto
33
import (
44
"errors"
55

6+
"github.com/Lab-RPL-ITS/twitter-clone-api/entity"
67
"github.com/google/uuid"
78
)
89

@@ -12,12 +13,14 @@ const (
1213
MESSAGE_FAILED_CREATE_POST = "failed create post"
1314
MESSAGE_FAILED_GET_POST_ID = "failed get post id"
1415
MESSAGE_FAILED_UPDATE_POST = "failed update post"
16+
MESSAGE_FAILED_GET_ALL_POSTS = "failed get all posts"
1517

1618
// Succcess
1719
MESSAGE_SUCCESS_CREATE_POST = "success create post"
1820
MESSAGE_SUCCESS_GET_POST_BY_ID = "success get post by id"
1921
MESSAGE_SUCCESS_DELETE_POST = "success delete post"
2022
MESSAGE_SUCCESS_UPDATE_POST = "success update post"
23+
MESSAGE_SUCCESS_GET_ALL_POSTS = "success get all posts"
2124
)
2225

2326
var (
@@ -44,4 +47,14 @@ type (
4447
PostUpdateRequest struct {
4548
Text string `json:"text" form:"text" binding:"required"`
4649
}
50+
51+
PostPaginationResponse struct {
52+
Data []PostResponse `json:"data"`
53+
PaginationResponse
54+
}
55+
56+
GetAllPostsRepositoryResponse struct {
57+
Posts []entity.Post `json:"posts"`
58+
PaginationResponse
59+
}
4760
)

repository/post_repository.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package repository
33
import (
44
"context"
55

6+
"github.com/Lab-RPL-ITS/twitter-clone-api/dto"
67
"github.com/Lab-RPL-ITS/twitter-clone-api/entity"
78
"github.com/google/uuid"
89
"gorm.io/gorm"
@@ -14,6 +15,7 @@ type (
1415
GetPostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) (entity.Post, error)
1516
DeletePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID) error
1617
UpdatePostById(ctx context.Context, tx *gorm.DB, postId uuid.UUID, post entity.Post) (entity.Post, error)
18+
GetAllPostsWithPagination(ctx context.Context, tx *gorm.DB, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
1719
}
1820

1921
postRepository struct {
@@ -86,3 +88,39 @@ func (r *postRepository) UpdatePostById(ctx context.Context, tx *gorm.DB, postId
8688

8789
return post, nil
8890
}
91+
92+
func (r *postRepository) GetAllPostsWithPagination(ctx context.Context, tx *gorm.DB, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error) {
93+
if tx == nil {
94+
tx = r.db
95+
}
96+
97+
var posts []entity.Post
98+
var err error
99+
var count int64
100+
101+
req.Default()
102+
103+
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User")
104+
if req.Search != "" {
105+
query = query.Where("text LIKE ?", "%"+req.Search+"%")
106+
}
107+
108+
if err := query.Count(&count).Error; err != nil {
109+
return dto.GetAllPostsRepositoryResponse{}, err
110+
}
111+
112+
if err := query.Scopes(Paginate(req)).Find(&posts).Error; err != nil {
113+
return dto.GetAllPostsRepositoryResponse{}, err
114+
}
115+
116+
totalPage := TotalPage(count, int64(req.PerPage))
117+
return dto.GetAllPostsRepositoryResponse{
118+
Posts: posts,
119+
PaginationResponse: dto.PaginationResponse{
120+
Page: req.Page,
121+
PerPage: req.PerPage,
122+
Count: count,
123+
MaxPage: totalPage,
124+
},
125+
}, err
126+
}

routes/post_route.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ func Post(route *gin.Engine, injector *do.Injector) {
1616
routes := route.Group("/api/post")
1717
{
1818
// Post
19-
routes.POST("/", middleware.Authenticate(jwtService), postController.CreatePost)
19+
routes.POST("", middleware.Authenticate(jwtService), postController.CreatePost)
2020
routes.GET("/:post_id", postController.GetPostById)
2121
routes.DELETE("/:post_id", middleware.Authenticate(jwtService), postController.DeletePostById)
2222
routes.PATCH("/:post_id", middleware.Authenticate(jwtService), postController.UpdatePostById)
23+
routes.GET("", postController.GetAllPosts)
2324
}
2425
}

service/post_service.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type (
1515
GetPostById(ctx context.Context, postId uuid.UUID) (dto.PostResponse, 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)
18+
GetAllPosts(ctx context.Context, req dto.PaginationRequest) (dto.PostPaginationResponse, error)
1819
}
1920

2021
postService struct {
@@ -133,3 +134,38 @@ func (s *postService) UpdatePostById(ctx context.Context, userId string, postId
133134
},
134135
}, nil
135136
}
137+
138+
func (s *postService) GetAllPosts(ctx context.Context, req dto.PaginationRequest) (dto.PostPaginationResponse, error) {
139+
dataWithPaginate, err := s.postRepo.GetAllPostsWithPagination(ctx, nil, req)
140+
if err != nil {
141+
return dto.PostPaginationResponse{}, err
142+
}
143+
144+
var data []dto.PostResponse
145+
for _, post := range dataWithPaginate.Posts {
146+
datum := dto.PostResponse{
147+
ID: post.ID.String(),
148+
Text: post.Text,
149+
ParentID: post.ParentID,
150+
User: dto.UserResponse{
151+
ID: post.UserID.String(),
152+
Name: post.User.Name,
153+
Bio: post.User.Bio,
154+
UserName: post.User.Username,
155+
ImageUrl: post.User.ImageUrl,
156+
},
157+
}
158+
159+
data = append(data, datum)
160+
}
161+
162+
return dto.PostPaginationResponse{
163+
Data: data,
164+
PaginationResponse: dto.PaginationResponse{
165+
Page: dataWithPaginate.Page,
166+
PerPage: dataWithPaginate.PerPage,
167+
MaxPage: dataWithPaginate.MaxPage,
168+
Count: dataWithPaginate.Count,
169+
},
170+
}, nil
171+
}

0 commit comments

Comments
 (0)