Skip to content

Commit

Permalink
Added user controller
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleigh committed Oct 2, 2018
1 parent 6e7a466 commit ef2e9c0
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {Controller, Get, Post, Put, Body, ValidationPipe, UnprocessableEntityException, Param, NotFoundException} from '@nestjs/common';
import { UserEntity } from 'entities';
import { Pagination } from 'paginate';
import { UserService } from './user.service';
import { UserModel } from 'models';
import { UpdateResult } from 'typeorm';

@Controller('users')
export class UserController {
constructor(private readonly userService: UserService) {}

@Get()
async index(): Promise<Pagination<UserEntity>> {
//TODO make PaginationOptionsInterface an object so it can be defaulted
return await this.userService.paginate();
}

@Post()
async store(@Body(new ValidationPipe()) user: UserModel): Promise<UserEntity> {
//TODO check duplication
const emailExists = this.userService.findByEmail(user.email);

if (emailExists) {
throw new UnprocessableEntityException;
}

return await this.userService.create(user);
}

@Put('/{id}')
async update(@Param('id') id: number, @Body(new ValidationPipe()) user: UserModel): Promise<UpdateResult> {
const userEntity = await this.userService.findById(id);

if (!userEntity) {
throw new NotFoundException;
}

return await this.userService.update({
...userEntity,
...user,
});
}

@Get('/{id}')
async show(@Param('id') id :number): Promise<UserEntity> {
const user = this.userService.findById(id);

if (!user) {
throw new NotFoundException;
}

return user;
}


}

0 comments on commit ef2e9c0

Please sign in to comment.