From fb2102168f64e0e09cb6cc7196c563e456962ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 19 Aug 2022 19:20:38 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B2=AB=EB=B2=88=EC=A7=B8=20=EC=88=98?= =?UTF-8?q?=EC=A0=95..?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prisma/schema.prisma | 1 - src/todo/todo.controller.ts | 41 +++++++++++++++++++++++++++---------- src/todo/todo.service.ts | 4 ++++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ad23dd6..5f1c85a 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -3,7 +3,6 @@ generator client { provider = "prisma-client-js" - previewFeatures = ["interactiveTransactions"] } //데이터베이스 연동 diff --git a/src/todo/todo.controller.ts b/src/todo/todo.controller.ts index 4419bc7..f7e51f2 100644 --- a/src/todo/todo.controller.ts +++ b/src/todo/todo.controller.ts @@ -23,14 +23,32 @@ export class TodoController { constructor(private readonly todoService: TodoService) {} @Get() - async fetchAll(@Query() query): Promise { - const take: number = isNaN(query.per_page) + async fetchAll(@Query() query): Promise<{}> { + //쿼리값 변경 + const per_page: number = isNaN(query.per_page) ? Number(env.DEFAULT_TABLE_PER_PAGE) : Number(query.per_page) - const skip: number = isNaN(query.page) + const page: number = isNaN(query.page) ? Number(env.DEFAULT_TABLE_PAGE) - : Number(query.page) * take - const search = query.searchString + : 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용 + const searchSql = query.searchString ? { OR: [ { title: { contains: query.searchString as string } }, @@ -38,18 +56,19 @@ export class TodoController { ] } : {} + const sql = { - take: take, - skip: skip, + take: per_page, + skip: page, where: { //is_done: true, - ...search + ...searchSql }, orderBy: { id: 'desc' } } - //console.log(query) - //console.log(sql) - return this.todoService.fetchAll(sql) + datas.rows = await this.todoService.fetchAll(sql) + //console.log(datas) + return datas } @HasRoles(Role.USER) diff --git a/src/todo/todo.service.ts b/src/todo/todo.service.ts index 74f746b..2e00a10 100644 --- a/src/todo/todo.service.ts +++ b/src/todo/todo.service.ts @@ -5,10 +5,14 @@ import { TodoDTO } from './dtos/todo.dto' @Injectable() export class TodoService { + todo: any constructor(private prismaService: PrismaService) {} //참고: https://intrepidgeeks.com/tutorial/05-instagram-clone-code-5-user-summary //전체조회 + async count(): Promise { + return this.prismaService.todo.count() + } async fetchAll(sql: any): Promise { return this.prismaService.todo.findMany(sql) }