Last active
May 30, 2019 02:04
-
-
Save marianocodes/4052aae8f713bbcb45b5c7b07b4d8bef to your computer and use it in GitHub Desktop.
Convector with NestJS - person.controller.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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