diff --git a/src/todo/dtos/todo.dto.ts b/src/todo/dtos/todo.dto.ts index 848f687..71a6470 100644 --- a/src/todo/dtos/todo.dto.ts +++ b/src/todo/dtos/todo.dto.ts @@ -1,5 +1,7 @@ export class TodoDTO { title: string content: string - is_done: boolean + is_done?: boolean | false + updatedAt?: Date | null + createdAt?: Date } diff --git a/src/todo/todo.controller.ts b/src/todo/todo.controller.ts index 1db4e3b..eccdda5 100644 --- a/src/todo/todo.controller.ts +++ b/src/todo/todo.controller.ts @@ -14,6 +14,7 @@ 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' import { RolesGuard } from 'src/auth/guards/roles.guard' +import { TodoDTO } from './dtos/todo.dto' import { TodoService } from './todo.service' @Controller('todo') @@ -74,29 +75,33 @@ export class TodoController { @HasRoles(Role.USER) @UseGuards(JwtAuthGuard, RolesGuard) @Get(':id') - async fetchOne(@Param('id') id: number): Promise { - return await this.todoService.fetchOne(id) - } - - @HasRoles(Role.USER) - @UseGuards(JwtAuthGuard, RolesGuard) - @Delete(':id') - async delete(@Param('id') id: number): Promise { - return await this.todoService.remove(id) + async fetchOne(@Param('id') id: string): Promise { + return await this.todoService.fetchOne({ id: Number(id) }) } // @HasRoles(Role.USER) // @UseGuards(JwtAuthGuard, RolesGuard) //@UseGuards(JwtAuthGuard) @Post() - async add(@Body() todo: Todo): Promise { - return await this.todoService.add(todo) + async add(@Body() data: TodoDTO): Promise { + return await this.todoService.add(data) } @HasRoles(Role.USER) @UseGuards(JwtAuthGuard, RolesGuard) @Put(':id') - async update(@Param('id') id: number, @Body() todo: Todo): Promise { - return await this.todoService.update(id, todo) + async update(@Param('id') id: string, @Body() data: TodoDTO): Promise { + data.updatedAt = new Date() + return await this.todoService.update({ + where: { id: Number(id) }, + data: data + }) + } + + @HasRoles(Role.USER) + @UseGuards(JwtAuthGuard, RolesGuard) + @Delete(':id') + async delete(@Param('id') id: string): Promise { + return await this.todoService.remove({ id: Number(id) }) } } diff --git a/src/todo/todo.service.ts b/src/todo/todo.service.ts index 72df864..cb9ec98 100644 --- a/src/todo/todo.service.ts +++ b/src/todo/todo.service.ts @@ -41,27 +41,29 @@ export class TodoService { } //단일 조회 - async fetchOne(id: number): Promise { - return this.prisma.todo.findUnique({ - where: { id: Number(id) } + async fetchOne(where: Prisma.TodoWhereUniqueInput): Promise { + return this.prisma.todo.findUnique({ where }) + } + + //단일 추가 + async add(data: Prisma.TodoCreateInput): Promise { + return this.prisma.todo.create({ data }) + } + + //단일 수정 + async update(params: { + where: Prisma.TodoWhereUniqueInput + data: Prisma.TodoUpdateInput + }): Promise { + const { where, data } = params + return this.prisma.todo.update({ + data, + where }) } //단일 삭제 - async remove(id: number): Promise { - return this.prisma.todo.delete({ where: { id: Number(id) } }) - } - - //단일 추가 - async add(todo: Prisma.TodoCreateInput): Promise { - return this.prisma.todo.create({ data: todo }) - } - - //단일 수정 - async update(id: number, todo: Todo): Promise { - return this.prisma.todo.update({ - where: { id: Number(id) }, - data: todo - }) + async remove(where: Prisma.TodoWhereUniqueInput): Promise { + return this.prisma.todo.delete({ where }) } }