forked from bashleigh/nestjs-blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ashleigh
committed
Oct 2, 2018
1 parent
6e7a466
commit ef2e9c0
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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; | ||
} | ||
|
||
|
||
} |