0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nest.js その7 todos.controlle.ts

Last updated at Posted at 2025-04-23

todos.controlle.ts
NestJS でビジネスロジック(データの取得・保存など)を担当するファイル

import {
  Body,
  Controller,
  Delete,
  Get,
  HttpException,
  HttpStatus,
  NotFoundException,
  Param,
  Post,
  Put,
} from '@nestjs/common';
import { TodosService } from './todos.service';
import { CreateTodoDTO, DeleteTodoDTO, UpdateTodoDTO } from 'src/dto/todos.dto';
import { TodosEntity } from 'src/entities/todos.entity';
import { UsersService } from 'src/users/users.service';
import { title } from 'process';

@Controller('todos')
export class TodosController {
  constructor(private readonly todosService: TodosService) {}

  @Post()
  async createOne(@Body() dto: CreateTodoDTO): Promise<TodosEntity> {
    try {
      return await this.todosService.createOne(dto);
    } catch (err) {
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Get()
  async readAll(): Promise<TodosEntity[]> {
    const selected = await this.todosService.readAll();
    if (!selected) {
      throw new NotFoundException({ message: 'データがありません' });
    }
    return selected;
  }

  @Get(':id')
  async readOne(@Param('id') id: number): Promise<TodosEntity> {
    const selected = await this.todosService.readOne(id);
    if (!selected) {
      throw new NotFoundException({ message: 'データがありません' });
    }
    return selected;
  }

  @Get('B/:title/:C')
  async readTitleOne(
    @Param('title') A: string,
    @Param('C') D: string,
  ): Promise<TodosEntity> {
    const selected = await this.todosService.readTitleOne(D);
    if (!selected) {
      throw new NotFoundException({ message: 'データがありません' });
    }
    return selected;
  }

  @Put()
  async updateOne(@Body() dto: UpdateTodoDTO): Promise<TodosEntity> {
    const selected = await this.todosService.readOne(dto.id);
    if (!selected) {
      throw new NotFoundException({ message: 'データがありません' });
    }
    try {
      return await this.todosService.updateOne(selected, dto);
    } catch (err) {
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  
  @Delete('delete')
  async deleteOneByBody(@Body() dto: DeleteTodoDTO): Promise<TodosEntity> {
    const selected = await this.todosService.readOne(dto.id);
    if (!selected) {
      throw new NotFoundException({ message: 'データがありません' });
    }
    try {
      return await this.todosService.deleteOne(selected);
    } catch (err) {
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Delete(':id')
  async deleteOne(@Param('id') id: number): Promise<TodosEntity> {
    const selected = await this.todosService.readOne(id);
    if (!selected) {
      throw new NotFoundException({ message: 'データがありません' });
    }
    try {
      return await this.todosService.deleteOne(selected);
    } catch (err) {
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

}



@Post()
  async createOne(@Body() dto: CreateTodoDTO): Promise<TodosEntity> {
    try {
      return await this.todosService.createOne(dto);
    } catch (err) {
      throw new HttpException(
        'Internal server error',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }
  
  //asnycとawaitはセット  promiseが型      await=処理を待つ  
  //try-catchの流れ エラーをキャッチし,プログラムの停止を防いでエラーメッセージをユーザーに表示したり適切な処理を行うことができる.






@Get(':id')
  async readOne(@Param('id') id: number): Promise<TodosEntity> {
    const selected = await this.todosService.readOne(id);
    if (!selected) {
      throw new NotFoundException({ message: 'データがありません' });
    }
    return selected;
  }




赤枠の中身もコードと同じようにpram (id)と書く。指定したidのデータをポストマンで表示することができる。

スクリーンショット 2025-04-23 23.05.09.png



@Put()
  async updateOne(@Body() dto: UpdateTodoDTO): Promise<TodosEntity> {
    const selected = await this.todosService.readOne(dto.id);
    if (!selected) {
      throw new NotFoundException({ message: 'データがありません' });
    }




赤枠が書いてある上画面で編集したい内容を書いてsendボタンをクリックすると下画面に編集された内容が表示される
スクリーンショット 2025-04-23 23.40.57.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?