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
0 commit comments