Skip to content

Instantly share code, notes, and snippets.

@marianocodes
Last active May 30, 2019 02:04
Show Gist options
  • Save marianocodes/4052aae8f713bbcb45b5c7b07b4d8bef to your computer and use it in GitHub Desktop.
Save marianocodes/4052aae8f713bbcb45b5c7b07b4d8bef to your computer and use it in GitHub Desktop.
Convector with NestJS - person.controller.ts
import { Body, Controller, Get, Logger, HttpException, HttpStatus, Post, Param } from '@nestjs/common';
import { Person } from 'person-cc';
import { PersonService } from './person.service';
import { PersonControllerBackEnd } from '../convector';
@Controller('person')
export class PersonController {
constructor(public personService: PersonService) { }
@Get('/')
public getAll() {
try {
return this.personService.getAll();
} catch (err) {
Logger.log(JSON.stringify(err));
throw new HttpException('Bad request', HttpStatus.BAD_REQUEST);
}
}
@Post('/')
public async create(@Body() { id, name }) {
try {
const personToCreate = new Person({ id, name });
return await PersonControllerBackEnd.create(personToCreate);
} catch (err) {
Logger.log(JSON.stringify(err));
throw new HttpException('Internal', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Get('/:id')
public async get(@Param() { id }) {
try {
const personToReturn = new Person(await PersonControllerBackEnd.get(id));
return personToReturn.toJSON();
} catch (err) {
Logger.log(JSON.stringify(err));
throw new HttpException('Internal', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Post('/:id/add-attribute')
public async getAttribute(@Param() { id }, @Body() { attributeId, content }) {
try {
return this.personService.addAttribute(id, attributeId, content);
} catch (err) {
Logger.log(JSON.stringify(err));
throw new HttpException('Internal', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment