Skip to content

Commit

Permalink
refactor: Update post list api query
Browse files Browse the repository at this point in the history
  • Loading branch information
seungdeok committed Aug 31, 2023
1 parent 9e700bd commit a00fa3f
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/post/dto/find-post.dto.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export class FindPostDto {
page: number;
categoryName: string;
limit: number;
}
2 changes: 2 additions & 0 deletions src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ export class PostController {
findAll(
@Query('page') page: string,
@Query('categoryName') categoryName: string,
@Query('limit') limit: string,
) {
return this.postService.findAll({
page: Number(page) || 1,
categoryName: categoryName || '',
limit: Number(limit) || 10,
});
}

Expand Down
7 changes: 4 additions & 3 deletions src/post/post.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export class PostService {
'post.modified_at',
'category.id',
'category.name',
]);
])
.orderBy('post.modified_at', 'DESC');

if (findPostDto.categoryName) {
query.andWhere('category.name = :categoryName', {
Expand All @@ -46,8 +47,8 @@ export class PostService {
}

const [list, total] = await query
.skip((findPostDto.page - 1) * 10)
.take(10)
.skip((findPostDto.page - 1) * findPostDto.limit)
.take(findPostDto.limit)
.getManyAndCount();

return {
Expand Down

0 comments on commit a00fa3f

Please sign in to comment.