Bingo for meetings-nestjs–create meeting api -part 15
Bingo
Bingo is a small project, written in TypeScript , and developed with Alexandru Badita in launch break (one hour - more or less). You can find sources at https://github.com/alexandru360/PresentationBingoCards/ . Those are my blog posts for Bingo : ( scroll below for the post)So now it is time to expose our objects as HTTP API. We have decided to go with nest.js because has support for TypeScript. The documentation to install is pretty obvious at https://docs.nestjs.com/ .
Now we must code the endpoint to create a meeting. As per the nest.js documentation , we should create a module ,a controller and a service to redirect calls . The problem is that , to create a meeting, we should transmit 2 parameters: userName and meetingName. To do this via HTTP, we must create a new class with this 2 parameters :
export class CreateMeeting { readonly userName: string; readonly meetingName: string; }
Also we want to add Swagger as show at https://docs.nestjs.com/recipes/swagger . And for this, according to the documentation , we should decorate the class:
import { ApiModelProperty } from '@nestjs/swagger'; export class CreateMeeting { @ApiModelProperty() readonly userName: string; @ApiModelProperty() readonly meetingName: string; }
And now we have a controller with 2 functions – get – to show all meetings – and post- to create the meeting:
@Controller('meetings') export class MeetingsController { constructor(private meetingsService: MeetingService){} @Get() index(): Meeting[] { return this.meetingsService.meetings; } @Post() async create(@Body() cm: CreateMeeting): Promise<Meeting> { console.log(`userName : ${JSON.stringify(cm.userName)} meetingName: ${JSON.stringify(cm.meetingName)}`); return this.meetingsService.create(cm.userName, cm.meetingName); } }
And now we have a fully qualified web api that can create a meeting. And we have swagger to test functions.
That’s good for a start…
Leave a Reply