Skip to content

Commit 60f0ac9

Browse files
committed
feat: add filter by is liked user post
1 parent 0830c64 commit 60f0ac9

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

controller/user_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ func (c *userController) GetUserPosts(ctx *gin.Context) {
149149
return
150150
}
151151

152-
var req dto.PaginationRequest
152+
var req dto.UserPostsPaginationRequest
153153
if err := ctx.ShouldBind(&req); err != nil {
154154
res := utils.BuildResponseFailed(dto.MESSAGE_FAILED_GET_POST_DATA_FROM_BODY, err.Error(), nil)
155155
ctx.AbortWithStatusJSON(http.StatusBadRequest, res)

dto/user_dto.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,9 @@ type (
7070
CheckUsernameRequest struct {
7171
UserName string `json:"username" form:"username" binding:"required"`
7272
}
73+
74+
UserPostsPaginationRequest struct {
75+
PaginationRequest
76+
IsLiked bool `form:"is_liked"`
77+
}
7378
)

repository/post_repository.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type (
1515
DeletePostById(ctx context.Context, tx *gorm.DB, postId uint64) error
1616
UpdatePostById(ctx context.Context, tx *gorm.DB, postId uint64, post entity.Post) (entity.Post, error)
1717
GetAllPostsWithPagination(ctx context.Context, tx *gorm.DB, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
18-
GetAllPostsWithPaginationByUsername(ctx context.Context, tx *gorm.DB, username string, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
18+
GetAllPostsWithPaginationByUsername(ctx context.Context, tx *gorm.DB, username string, req dto.UserPostsPaginationRequest) (dto.GetAllPostsRepositoryResponse, error)
1919
GetAllPostRepliesWithPagination(ctx context.Context, tx *gorm.DB, postId uint64, req dto.PaginationRequest) (dto.GetAllRepliesRepositoryResponse, error)
2020
UpdateLikesCount(ctx context.Context, tx *gorm.DB, postId uint64, count int) error
2121
}
@@ -175,7 +175,7 @@ func (r *postRepository) UpdateLikesCount(ctx context.Context, tx *gorm.DB, post
175175
return nil
176176
}
177177

178-
func (r *postRepository) GetAllPostsWithPaginationByUsername(ctx context.Context, tx *gorm.DB, username string, req dto.PaginationRequest) (dto.GetAllPostsRepositoryResponse, error) {
178+
func (r *postRepository) GetAllPostsWithPaginationByUsername(ctx context.Context, tx *gorm.DB, username string, req dto.UserPostsPaginationRequest) (dto.GetAllPostsRepositoryResponse, error) {
179179
if tx == nil {
180180
tx = r.db
181181
}
@@ -186,7 +186,12 @@ func (r *postRepository) GetAllPostsWithPaginationByUsername(ctx context.Context
186186

187187
req.Default()
188188

189-
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Where("posts.parent_id IS NULL").Where("\"User\".username = ?", username).Order("created_at DESC")
189+
query := tx.WithContext(ctx).Model(&entity.Post{}).Joins("User").Where("posts.parent_id IS NULL").Where("\"User\".username = ?", username)
190+
if req.IsLiked {
191+
query = query.Joins("INNER JOIN likes ON likes.post_id = posts.id AND likes.user_id = posts.user_id")
192+
}
193+
query = query.Order("created_at DESC")
194+
190195
if req.Search != "" {
191196
query = query.Where("text LIKE ?", "%"+req.Search+"%")
192197
}
@@ -195,7 +200,10 @@ func (r *postRepository) GetAllPostsWithPaginationByUsername(ctx context.Context
195200
return dto.GetAllPostsRepositoryResponse{}, err
196201
}
197202

198-
if err := query.Scopes(Paginate(req)).Find(&posts).Error; err != nil {
203+
if err := query.Scopes(Paginate(dto.PaginationRequest{
204+
Page: req.Page,
205+
PerPage: req.PerPage,
206+
})).Find(&posts).Error; err != nil {
199207
return dto.GetAllPostsRepositoryResponse{}, err
200208
}
201209

service/user_service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type (
1919
Verify(ctx context.Context, req dto.UserLoginRequest) (dto.UserLoginResponse, error)
2020
GetUserByUsername(ctx context.Context, username string) (dto.UserResponse, error)
2121
UpdateUser(ctx context.Context, userId string, req dto.UserProfileUpdateRequest) (dto.UserResponse, error)
22-
GetUserPosts(ctx context.Context, username string, req dto.PaginationRequest) (dto.PostPaginationResponse, error)
22+
GetUserPosts(ctx context.Context, username string, req dto.UserPostsPaginationRequest) (dto.PostPaginationResponse, error)
2323
}
2424

2525
userService struct {
@@ -165,7 +165,7 @@ func (s *userService) UpdateUser(ctx context.Context, userId string, req dto.Use
165165
}, nil
166166
}
167167

168-
func (s *userService) GetUserPosts(ctx context.Context, username string, req dto.PaginationRequest) (dto.PostPaginationResponse, error) {
168+
func (s *userService) GetUserPosts(ctx context.Context, username string, req dto.UserPostsPaginationRequest) (dto.PostPaginationResponse, error) {
169169
dataWithPaginate, err := s.postRepo.GetAllPostsWithPaginationByUsername(ctx, nil, username, req)
170170
if err != nil {
171171
return dto.PostPaginationResponse{}, err

0 commit comments

Comments
 (0)