Skip to content

Commit 68c5b83

Browse files
committed
feat: add get my posts endpoint
1 parent f08fab2 commit 68c5b83

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

controller/post_controller.go

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

2223
postController struct {
@@ -152,3 +153,29 @@ func (c *postController) GetAllPosts(ctx *gin.Context) {
152153

153154
ctx.JSON(http.StatusOK, res)
154155
}
156+
157+
func (c *postController) GetMyPosts(ctx *gin.Context) {
158+
var req dto.PaginationRequest
159+
if err := ctx.ShouldBind(&req); err != nil {
160+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
161+
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)
162+
return
163+
}
164+
165+
userId := ctx.GetString("user_id")
166+
posts, err := c.postService.GetMyPosts(ctx.Request.Context(), userId, req)
167+
if err != nil {
168+
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_MY_POSTS, err.Error(), nil)
169+
ctx.JSON(http.StatusBadRequest, res)
170+
return
171+
}
172+
173+
res := utils.Response{
174+
Status: true,
175+
Message: dto.MESSAGE_SUCCESS_GET_MY_POSTS,
176+
Data: posts.Data,
177+
Meta: posts.PaginationResponse,
178+
}
179+
180+
ctx.JSON(http.StatusOK, res)
181+
}

dto/post_dto.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ const (
1313
MESSAGE_FAILED_GET_POST_ID = "failed get post id"
1414
MESSAGE_FAILED_UPDATE_POST = "failed update post"
1515
MESSAGE_FAILED_GET_ALL_POSTS = "failed get all posts"
16+
MESSAGE_FAILED_GET_MY_POSTS = "failed get my posts"
1617

1718
// Succcess
1819
MESSAGE_SUCCESS_CREATE_POST = "success create post"
1920
MESSAGE_SUCCESS_GET_POST_BY_ID = "success get post by id"
2021
MESSAGE_SUCCESS_DELETE_POST = "success delete post"
2122
MESSAGE_SUCCESS_UPDATE_POST = "success update post"
2223
MESSAGE_SUCCESS_GET_ALL_POSTS = "success get all posts"
24+
MESSAGE_SUCCESS_GET_MY_POSTS = "success get my posts"
2325
)
2426

2527
var (
@@ -29,6 +31,7 @@ var (
2931
ErrParseParentID = errors.New("failed to parse parent id")
3032
ErrDeletePostById = errors.New("failed to delete post")
3133
ErrUpdatePostById = errors.New("failed to update post")
34+
ErrGetMyPosts = errors.New("failed to get my posts")
3235
)
3336

3437
type (

repository/post_repository.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type (
1717
GetAllPostsWithPagination(ctx context.Context, tx *gorm.DB, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
1818
GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uint64, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error)
1919
UpdateLikesCount(ctx context.Context, tx *gorm.DB, postId uint64, count int) error
20+
GetMyPosts(ctx context.Context, tx *gorm.DB, userId string, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
2021
}
2122

2223
postRepository struct {
@@ -173,3 +174,39 @@ func (r *postRepository) UpdateLikesCount(ctx context.Context, tx *gorm.DB, post
173174

174175
return nil
175176
}
177+
178+
func (r *postRepository) GetMyPosts(ctx context.Context, tx *gorm.DB, userId string, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error) {
179+
if tx == nil {
180+
tx = r.db
181+
}
182+
183+
var posts []entity.Post
184+
var err error
185+
var count int64
186+
187+
req.Default()
188+
189+
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Where("posts.user_id = ?", userId).Order("created_at DESC")
190+
if req.Search != "" {
191+
query = query.Where("text LIKE ?", "%"+req.Search+"%")
192+
}
193+
194+
if err := query.Count(&count).Error; err != nil {
195+
return dto.GetAllPostsRepositoryResponse{}, err
196+
}
197+
198+
if err := query.Scopes(Paginate(req)).Find(&posts).Error; err != nil {
199+
return dto.GetAllPostsRepositoryResponse{}, err
200+
}
201+
202+
totalPage := TotalPage(count, int64(req.PerPage))
203+
return dto.GetAllPostsRepositoryResponse{
204+
Posts: posts,
205+
PaginationResponse: dto.PaginationResponse{
206+
Page: req.Page,
207+
PerPage: req.PerPage,
208+
Count: count,
209+
MaxPage: totalPage,
210+
},
211+
}, err
212+
}

routes/post_route.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func Post(route *gin.Engine, injector *do.Injector) {
1717
{
1818
// Post
1919
routes.POST("", middleware.Authenticate(jwtService), postController.CreatePost)
20+
routes.GET("/me", middleware.Authenticate(jwtService), postController.GetMyPosts)
2021
routes.GET("/:post_id", postController.GetPostById)
2122
routes.DELETE("/:post_id", middleware.Authenticate(jwtService), postController.DeletePostById)
2223
routes.PATCH("/:post_id", middleware.Authenticate(jwtService), postController.UpdatePostById)

service/post_service.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, 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)
19+
GetMyPosts(ctx context.Context, userId string, req dto.PaginationRequest) (dto.PostPaginationResponse, error)
1920
}
2021

2122
postService struct {
@@ -206,3 +207,39 @@ func (s *postService) GetAllPosts(ctx context.Context, req dto.PaginationRequest
206207
},
207208
}, nil
208209
}
210+
211+
func (s *postService) GetMyPosts(ctx context.Context, userId string, req dto.PaginationRequest) (dto.PostPaginationResponse, error) {
212+
posts, err := s.postRepo.GetMyPosts(ctx, nil, userId, req)
213+
if err != nil {
214+
return dto.PostPaginationResponse{}, dto.ErrGetMyPosts
215+
}
216+
217+
var data []dto.PostResponse
218+
for _, post := range posts.Posts {
219+
datum := dto.PostResponse{
220+
ID: post.ID,
221+
Text: post.Text,
222+
TotalLikes: post.TotalLikes,
223+
ParentID: post.ParentID,
224+
User: dto.UserResponse{
225+
ID: post.UserID.String(),
226+
Name: post.User.Name,
227+
Bio: post.User.Bio,
228+
UserName: post.User.Username,
229+
ImageUrl: post.User.ImageUrl,
230+
},
231+
}
232+
233+
data = append(data, datum)
234+
}
235+
236+
return dto.PostPaginationResponse{
237+
Data: data,
238+
PaginationResponse: dto.PaginationResponse{
239+
Page: posts.Page,
240+
PerPage: posts.PerPage,
241+
MaxPage: posts.MaxPage,
242+
Count: posts.Count,
243+
},
244+
}, nil
245+
}

0 commit comments

Comments
 (0)