0

enter image description here

I have added the below code in main.ts

import { NestFactory } from '@nestjs/core';

import{SwaggerModule,DocumentBuilder} from '@nestjs/swagger'

import { AppModule } from './app.module';

async function bootstrap() {

  const app = await NestFactory.create(AppModule);
 
  const options = new DocumentBuilder()

    .setTitle('My API')

    .setDescription('API description')

    .setVersion('1.0')

    .build();

  const document = SwaggerModule.createDocument(app, options);

  SwaggerModule.setup('api', app, document);


  await app.listen(3000);

}
bootstrap();

//controller

@Post()

addProduct(

    @Body('title') title:string,
    @Body('price') price:number,


):any{

    const idFromService=this.productserive.addNewProduct(title,price);

    return idFromService;

}

//productservice

export class ProductService {

  products:Product[]=[]; 
 
    addNewProduct(title:string,price:number){

        const id=uId();

        const newproduct=new Product(id,title,price);

        this.products.push(newproduct);

        return {title,price};

    }

}
Ouroborus
  • 16,237
  • 4
  • 39
  • 62
Hitler
  • 1

1 Answers1

0

// create a separate dto 
import { ApiProperty } from '@nestjs/swagger';

export class TestDto {
    @ApiProperty()
    readonly title: string

    @ApiProperty()
    readonly price: number
}

// use it in your controller
@Post()
    addProduct(@Body() TestDto:TestDto): any {
        return;
    }
Subham kuswa
  • 356
  • 4
  • 12