Skip to content

Commit

Permalink
feat: Add swagger for cat api
Browse files Browse the repository at this point in the history
  • Loading branch information
Jhutatip authored and PanuphongK committed Nov 9, 2023
1 parent 19ef35f commit 77cf957
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 50 deletions.
89 changes: 86 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
"@nestjs/core": "^10.0.0",
"@nestjs/mongoose": "^10.0.1",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.1.14",
"class-validator": "^0.14.0",
"dotenv": "^16.3.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
Expand Down
3 changes: 1 addition & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { MongooseModule } from '@nestjs/mongoose';
import { CatModule } from './cat/cat.module';
import { AppService } from './app.service';
import { AppController } from './app.controller';
import { ProductService } from './product/product.service';
import { ProductController } from './product/product.controller';

import { ProductModule } from './product/product.module';

@Module({
Expand Down
52 changes: 29 additions & 23 deletions src/cat/cat.controller.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,51 @@
/* eslint-disable prettier/prettier */
/* eslint-disable @typescript-eslint/no-unused-vars */
import { Controller, Get, Post, Patch, Delete, Param, Body } from '@nestjs/common';
import {
Controller,
Get,
Post,
Patch,
Delete,
Param,
Body,
} from '@nestjs/common';
import { CatsService } from './cat.service';
import { Cat } from './cat.schema';
import { CreateCatDto } from './dto/create-cat.dto';
import { UpdatecatDto } from './dto/update-cat.dto';
import { ApiBody, ApiCreatedResponse, ApiOkResponse } from '@nestjs/swagger';
import { ApiTags} from '@nestjs/swagger';

@ApiTags('Cat')
@Controller('Cat')
export class CatController {
constructor(private readonly catsService: CatsService) {}

@Post()
async create(@Body() createCatDto: CreateCatDto){
@Post('cat')
@ApiCreatedResponse({ description: 'Cats add' })
@ApiBody({ type: CreateCatDto })
async create(@Body() createCatDto: CreateCatDto) {
return this.catsService.create(createCatDto);
}

@Get()
@Get('cat')
async findAll(): Promise<Cat[]> {
return this.catsService.findAll();
}

@Patch(':id')
async update(
@Param('id') id: string,
@Body() UpdatecatDto: UpdatecatDto,
) {
@Get('cat/:id')
async fineOne(@Param ('id') id: string): Promise<Cat> {
return this.catsService.findOne(id);
}

@Patch('cat/:id')
@ApiOkResponse({ description: 'Add new cats' })
async update(@Param('id') id: string, @Body() UpdatecatDto: UpdatecatDto) {
return this.catsService.update(id, UpdatecatDto);
}
@Delete(':id')

@Delete('cat/:id')
async remove(@Param('id') id: string) {
return this.catsService.delete(id);

return this.catsService.delete(id);
}


};








}
10 changes: 9 additions & 1 deletion src/cat/cat.schema.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/* eslint-disable prettier/prettier */
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { ApiProperty } from '@nestjs/swagger';
import { HydratedDocument } from 'mongoose';
import { CreateCatDto } from './dto/create-cat.dto';

export type CatDocument = HydratedDocument<Cat>
export type CatDocument = HydratedDocument<Cat>;

@Schema()
export class Cat {
@ApiProperty({
description: 'The name of the cat',
example: 'na mi',
})
article: CreateCatDto

@Prop()
name: string;

Expand Down
25 changes: 11 additions & 14 deletions src/cat/cat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,28 @@ import { UpdatecatDto } from './dto/update-cat.dto';
export class CatsService {
constructor(
@InjectModel(Cat.name)
private catModel: Model<Cat>
private catModel: Model<Cat>,
) {}


async create(createCatDto: CreateCatDto): Promise<Cat> {
const createdCat = new this.catModel(createCatDto);
return createdCat.save();
return createdCat.save();
}

async findAll(): Promise<Cat[]> {
return this.catModel.find().exec();
}

async findOne(id: string): Promise<Cat> {
return this.catModel.findById(id).exec();
}

async delete(id: string): Promise<void> {
await this.catModel.findByIdAndRemove(id);

}async update(
id: string,
updatecatmentDto: UpdatecatDto,
): Promise<Cat>{
}
async update(id: string, updatecatmentDto: UpdatecatDto): Promise<Cat> {
return this.catModel.findByIdAndUpdate(id, updatecatmentDto, {
new: true,
})
};


};
});
}
}
31 changes: 31 additions & 0 deletions src/cat/dto/create-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
/* eslint-disable prettier/prettier */
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator'

export class CreateCatDto {
@ApiProperty({ type: String, description: 'name' })
@IsString()
name: string;

@ApiProperty({ type: String, description: 'age' })
@IsString()
age: string;

@ApiProperty({ type: String, description: 'breed' })
@IsString()
breed: string;

@ApiProperty({ type : String, description : 'Gender'})
@IsString()
Gender: string;

@ApiProperty({ type : String, description : 'Vaccine'})
@IsString()
vaccine: string;

@ApiProperty({ type : String, description : 'history'})
@IsString()
history: string;

@ApiProperty({ type : String, description : 'Country'})
@IsString()
country: string;

@ApiProperty({ type : String, description : 'Farm'})
@IsString()
farm: string;
}
16 changes: 11 additions & 5 deletions src/cat/dto/update-cat.dto.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-disable prettier/prettier */
import { ApiProperty } from '@nestjs/swagger';
import { IsString } from 'class-validator';

export class UpdatecatDto {
readonly name: string;
readonly age: string;

}

@ApiProperty({ type: String, description: 'Vaccine' })
@IsString()
vaccine: string;

@ApiProperty({ type: String, description: 'history' })
@IsString()
history: string;
}
10 changes: 9 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
/* eslint-disable prettier/prettier */
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3006);
const config = new DocumentBuilder()
.setTitle('Backend API')
.setVersion('1.0')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);

await app.listen(3007);
}
bootstrap();
Loading

0 comments on commit 77cf957

Please sign in to comment.