NestJS 수정1..

This commit is contained in:
최준흠 2022-08-30 17:47:14 +09:00
parent e10dc01eaa
commit 399e0c21aa
3 changed files with 41 additions and 32 deletions

View File

@ -1,5 +1,7 @@
export class TodoDTO { export class TodoDTO {
title: string title: string
content: string content: string
is_done: boolean is_done?: boolean | false
updatedAt?: Date | null
createdAt?: Date
} }

View File

@ -14,6 +14,7 @@ import { HasRoles } from 'src/auth/decorators/has-roles.decorator'
import { JwtAuthGuard } from 'src/auth/guards/jwt.authguard' import { JwtAuthGuard } from 'src/auth/guards/jwt.authguard'
import { Role } from 'src/auth/guards/role.enum' import { Role } from 'src/auth/guards/role.enum'
import { RolesGuard } from 'src/auth/guards/roles.guard' import { RolesGuard } from 'src/auth/guards/roles.guard'
import { TodoDTO } from './dtos/todo.dto'
import { TodoService } from './todo.service' import { TodoService } from './todo.service'
@Controller('todo') @Controller('todo')
@ -74,29 +75,33 @@ export class TodoController {
@HasRoles(Role.USER) @HasRoles(Role.USER)
@UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
@Get(':id') @Get(':id')
async fetchOne(@Param('id') id: number): Promise<Todo | undefined> { async fetchOne(@Param('id') id: string): Promise<Todo | undefined> {
return await this.todoService.fetchOne(id) return await this.todoService.fetchOne({ id: Number(id) })
}
@HasRoles(Role.USER)
@UseGuards(JwtAuthGuard, RolesGuard)
@Delete(':id')
async delete(@Param('id') id: number): Promise<Todo | undefined> {
return await this.todoService.remove(id)
} }
// @HasRoles(Role.USER) // @HasRoles(Role.USER)
// @UseGuards(JwtAuthGuard, RolesGuard) // @UseGuards(JwtAuthGuard, RolesGuard)
//@UseGuards(JwtAuthGuard) //@UseGuards(JwtAuthGuard)
@Post() @Post()
async add(@Body() todo: Todo): Promise<Todo> { async add(@Body() data: TodoDTO): Promise<Todo> {
return await this.todoService.add(todo) return await this.todoService.add(data)
} }
@HasRoles(Role.USER) @HasRoles(Role.USER)
@UseGuards(JwtAuthGuard, RolesGuard) @UseGuards(JwtAuthGuard, RolesGuard)
@Put(':id') @Put(':id')
async update(@Param('id') id: number, @Body() todo: Todo): Promise<Todo> { async update(@Param('id') id: string, @Body() data: TodoDTO): Promise<Todo> {
return await this.todoService.update(id, todo) 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<Todo | undefined> {
return await this.todoService.remove({ id: Number(id) })
} }
} }

View File

@ -41,27 +41,29 @@ export class TodoService {
} }
//단일 조회 //단일 조회
async fetchOne(id: number): Promise<Todo | undefined> { async fetchOne(where: Prisma.TodoWhereUniqueInput): Promise<Todo | null> {
return this.prisma.todo.findUnique({ return this.prisma.todo.findUnique({ where })
where: { id: Number(id) } }
//단일 추가
async add(data: Prisma.TodoCreateInput): Promise<Todo> {
return this.prisma.todo.create({ data })
}
//단일 수정
async update(params: {
where: Prisma.TodoWhereUniqueInput
data: Prisma.TodoUpdateInput
}): Promise<Todo | null> {
const { where, data } = params
return this.prisma.todo.update({
data,
where
}) })
} }
//단일 삭제 //단일 삭제
async remove(id: number): Promise<Todo | undefined> { async remove(where: Prisma.TodoWhereUniqueInput): Promise<Todo | null> {
return this.prisma.todo.delete({ where: { id: Number(id) } }) return this.prisma.todo.delete({ where })
}
//단일 추가
async add(todo: Prisma.TodoCreateInput): Promise<Todo> {
return this.prisma.todo.create({ data: todo })
}
//단일 수정
async update(id: number, todo: Todo): Promise<Todo | undefined> {
return this.prisma.todo.update({
where: { id: Number(id) },
data: todo
})
} }
} }