nestjs_backend/src/todo/todo.controller.ts
2022-08-18 11:45:49 +09:00

52 lines
1.2 KiB
TypeScript

import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
Query
} from '@nestjs/common'
import { Todo } from '@prisma/client'
import { TodoDTO } from './dtos/todo.dto'
import { TodoService } from './todo.service'
@Controller('todo')
export class TodoController {
constructor(private readonly todoService: TodoService) {}
@Get()
async fetchAll(@Query() query): Promise<Todo[]> {
const sql = {
take: isNaN(query.per_page) ? 20 : Number(query.per_page),
skip: isNaN(query.page) ? 0 : Number(query.page) * query.per_page,
//where: { is_done: true },
orderBy: { id: 'desc' }
}
//console.log(query)
//console.log(sql)
return this.todoService.fetchAll(sql)
}
@Get(':id')
async fetchOne(@Param('id') id: number): Promise<Todo | undefined> {
return this.todoService.fetchOne(id)
}
@Delete(':id')
async delete(@Param('id') id: number): Promise<Todo | undefined> {
return this.todoService.remove(id)
}
@Post()
async add(@Body() data: Todo): Promise<Todo> {
return this.todoService.add(data)
}
@Put(':id')
async update(@Param('id') id: number, @Body() data: TodoDTO): Promise<Todo> {
return this.todoService.update(id, data)
}
}