From 31a2557f6566d20cfc23f9a8edbeb03d7509642a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Mon, 22 Aug 2022 19:59:04 +0900 Subject: [PATCH] =?UTF-8?q?=EC=88=98=EC=A0=95=203=EB=B2=88=EC=A7=B8...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 4 +-- src/todo/todo.controller.ts | 62 +++++++++++++++++++------------------ src/todo/todo.service.ts | 3 +- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/.env b/.env index f12d2d9..03e60fe 100644 --- a/.env +++ b/.env @@ -11,5 +11,5 @@ JWT_SECURITY_KEY = "security_key" JWT_EXPIRE_MAX = "600s" AUTH_USERNAME_FIELD="email" -DEFAULT_TABLE_PER_PAGE = 20 -DEFAULT_TABLE_PAGE = 0 +DEFAULT_TABLE_PER_PAGE = 5 +DEFAULT_TABLE_PAGE = 1 diff --git a/src/todo/todo.controller.ts b/src/todo/todo.controller.ts index f7e51f2..4010527 100644 --- a/src/todo/todo.controller.ts +++ b/src/todo/todo.controller.ts @@ -10,7 +10,6 @@ import { UseGuards } from '@nestjs/common' import { Todo } from '@prisma/client' -import { env } from 'process' import { HasRoles } from 'src/auth/decorators/has-roles.decorator' import { JwtAuthGuard } from 'src/auth/guards/jwt.authguard' import { Role } from 'src/auth/guards/role.enum' @@ -24,30 +23,14 @@ export class TodoController { @Get() async fetchAll(@Query() query): Promise<{}> { - //쿼리값 변경 - const per_page: number = isNaN(query.per_page) - ? Number(env.DEFAULT_TABLE_PER_PAGE) - : Number(query.per_page) - const page: number = isNaN(query.page) - ? Number(env.DEFAULT_TABLE_PAGE) - : Number(query.page) - const searchString: string = isNaN(query.searchString) - ? '' - : String(query.searchString) - - //리턴 할 데이터 - const datas = { - query: { - page: page, - per_page: per_page, - searchString: searchString - }, - total: 0, - rows: [] - } - datas.total = await this.todoService.count() - //Sql용 + console.log(query) + const page = query.page + ? parseInt(query.page) + : parseInt(process.env.DEFAULT_TABLE_PAGE) + const per_page = query.per_page + ? parseInt(query.per_page) + : parseInt(process.env.DEFAULT_TABLE_PER_PAGE) const searchSql = query.searchString ? { OR: [ @@ -57,18 +40,37 @@ export class TodoController { } : {} - const sql = { - take: per_page, - skip: page, + const countSQL = { where: { //is_done: true, ...searchSql }, orderBy: { id: 'desc' } } - datas.rows = await this.todoService.fetchAll(sql) - //console.log(datas) - return datas + const total = await this.todoService.count(countSQL) + console.log(countSQL) + + const fetchSQL = { + skip: (page - 1) * per_page, + take: per_page, + where: { + //is_done: true, + ...searchSql + }, + orderBy: { id: 'desc' } + } + const rows = await this.todoService.fetchAll(fetchSQL) + console.log(fetchSQL) + + const data = { + page: page, + per_page: per_page, + searchString: query.searchString, + total: total, + rows: rows + } + //console.log(data) + return data } @HasRoles(Role.USER) diff --git a/src/todo/todo.service.ts b/src/todo/todo.service.ts index 2e00a10..2e65197 100644 --- a/src/todo/todo.service.ts +++ b/src/todo/todo.service.ts @@ -10,9 +10,10 @@ export class TodoService { //참고: https://intrepidgeeks.com/tutorial/05-instagram-clone-code-5-user-summary //전체조회 - async count(): Promise { + async count(sql: any): Promise { return this.prismaService.todo.count() } + async fetchAll(sql: any): Promise { return this.prismaService.todo.findMany(sql) }