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 {
title: 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 { 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<Todo | undefined> {
return await this.todoService.fetchOne(id)
}
@HasRoles(Role.USER)
@UseGuards(JwtAuthGuard, RolesGuard)
@Delete(':id')
async delete(@Param('id') id: number): Promise<Todo | undefined> {
return await this.todoService.remove(id)
async fetchOne(@Param('id') id: string): Promise<Todo | undefined> {
return await this.todoService.fetchOne({ id: Number(id) })
}
// @HasRoles(Role.USER)
// @UseGuards(JwtAuthGuard, RolesGuard)
//@UseGuards(JwtAuthGuard)
@Post()
async add(@Body() todo: Todo): Promise<Todo> {
return await this.todoService.add(todo)
async add(@Body() data: TodoDTO): Promise<Todo> {
return await this.todoService.add(data)
}
@HasRoles(Role.USER)
@UseGuards(JwtAuthGuard, RolesGuard)
@Put(':id')
async update(@Param('id') id: number, @Body() todo: Todo): Promise<Todo> {
return await this.todoService.update(id, todo)
async update(@Param('id') id: string, @Body() data: TodoDTO): Promise<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> {
return this.prisma.todo.findUnique({
where: { id: Number(id) }
async fetchOne(where: Prisma.TodoWhereUniqueInput): Promise<Todo | null> {
return this.prisma.todo.findUnique({ where })
}
//단일 추가
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> {
return this.prisma.todo.delete({ where: { id: Number(id) } })
}
//단일 추가
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
})
async remove(where: Prisma.TodoWhereUniqueInput): Promise<Todo | null> {
return this.prisma.todo.delete({ where })
}
}