|
| 1 | +import { Type } from 'class-transformer'; |
| 2 | +import { IsEmail, IsNotEmpty, IsUUID, ValidateNested } from 'class-validator'; |
1 | 3 | import {
|
2 | 4 | Authorized, Body, Delete, Get, JsonController, OnUndefined, Param, Post, Put, Req
|
3 | 5 | } from 'routing-controllers';
|
| 6 | +import { OpenAPI, ResponseSchema } from 'routing-controllers-openapi'; |
4 | 7 |
|
5 | 8 | import { UserNotFoundError } from '../errors/UserNotFoundError';
|
6 | 9 | import { User } from '../models/User';
|
7 | 10 | import { UserService } from '../services/UserService';
|
| 11 | +import { PetResponse } from './PetController'; |
| 12 | + |
| 13 | +class BaseUser { |
| 14 | + @IsNotEmpty() |
| 15 | + public firstName: string; |
| 16 | + |
| 17 | + @IsNotEmpty() |
| 18 | + public lastName: string; |
| 19 | + |
| 20 | + @IsEmail() |
| 21 | + @IsNotEmpty() |
| 22 | + public email: string; |
| 23 | + |
| 24 | + @IsNotEmpty() |
| 25 | + public username: string; |
| 26 | +} |
| 27 | + |
| 28 | +export class UserResponse extends BaseUser { |
| 29 | + @IsUUID() |
| 30 | + public id: string; |
| 31 | + |
| 32 | + @ValidateNested({ each: true }) |
| 33 | + @Type(() => PetResponse) |
| 34 | + public pets: PetResponse[]; |
| 35 | +} |
| 36 | + |
| 37 | +class CreateUserBody extends BaseUser { |
| 38 | + @IsNotEmpty() |
| 39 | + public password: string; |
| 40 | +} |
8 | 41 |
|
9 | 42 | @Authorized()
|
10 | 43 | @JsonController('/users')
|
| 44 | +@OpenAPI({ security: [{ basicAuth: [] }] }) |
11 | 45 | export class UserController {
|
12 | 46 |
|
13 | 47 | constructor(
|
14 | 48 | private userService: UserService
|
15 | 49 | ) { }
|
16 | 50 |
|
17 | 51 | @Get()
|
| 52 | + @ResponseSchema(UserResponse, { isArray: true }) |
18 | 53 | public find(): Promise<User[]> {
|
19 | 54 | return this.userService.find();
|
20 | 55 | }
|
21 | 56 |
|
22 | 57 | @Get('/me')
|
| 58 | + @ResponseSchema(UserResponse, { isArray: true }) |
23 | 59 | public findMe(@Req() req: any): Promise<User[]> {
|
24 | 60 | return req.user;
|
25 | 61 | }
|
26 | 62 |
|
27 | 63 | @Get('/:id')
|
28 | 64 | @OnUndefined(UserNotFoundError)
|
| 65 | + @ResponseSchema(UserResponse) |
29 | 66 | public one(@Param('id') id: string): Promise<User | undefined> {
|
30 | 67 | return this.userService.findOne(id);
|
31 | 68 | }
|
32 | 69 |
|
33 | 70 | @Post()
|
34 |
| - public create(@Body() user: User): Promise<User> { |
| 71 | + @ResponseSchema(UserResponse) |
| 72 | + public create(@Body() body: CreateUserBody): Promise<User> { |
| 73 | + const user = new User(); |
| 74 | + user.email = body.email; |
| 75 | + user.firstName = body.firstName; |
| 76 | + user.lastName = body.lastName; |
| 77 | + user.password = body.password; |
| 78 | + user.username = body.username; |
| 79 | + |
35 | 80 | return this.userService.create(user);
|
36 | 81 | }
|
37 | 82 |
|
38 | 83 | @Put('/:id')
|
39 |
| - public update(@Param('id') id: string, @Body() user: User): Promise<User> { |
| 84 | + @ResponseSchema(UserResponse) |
| 85 | + public update(@Param('id') id: string, @Body() body: BaseUser): Promise<User> { |
| 86 | + const user = new User(); |
| 87 | + user.email = body.email; |
| 88 | + user.firstName = body.firstName; |
| 89 | + user.lastName = body.lastName; |
| 90 | + user.username = body.username; |
| 91 | + |
40 | 92 | return this.userService.update(id, user);
|
41 | 93 | }
|
42 | 94 |
|
|
0 commit comments