From a00fa3f6b91f90a81ddf6bc9f903fc47698f98d7 Mon Sep 17 00:00:00 2001 From: seungdeok Date: Thu, 31 Aug 2023 20:52:49 +0900 Subject: [PATCH] refactor: Update post list api query --- src/post/dto/find-post.dto.ts | 1 + src/post/post.controller.ts | 2 ++ src/post/post.service.ts | 7 ++++--- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/post/dto/find-post.dto.ts b/src/post/dto/find-post.dto.ts index 7c612c1..63912ab 100644 --- a/src/post/dto/find-post.dto.ts +++ b/src/post/dto/find-post.dto.ts @@ -1,4 +1,5 @@ export class FindPostDto { page: number; categoryName: string; + limit: number; } diff --git a/src/post/post.controller.ts b/src/post/post.controller.ts index 9903bb7..072c049 100644 --- a/src/post/post.controller.ts +++ b/src/post/post.controller.ts @@ -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, }); } diff --git a/src/post/post.service.ts b/src/post/post.service.ts index fd79590..4b19159 100644 --- a/src/post/post.service.ts +++ b/src/post/post.service.ts @@ -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', { @@ -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 {