Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 71 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { |
||
2 | Controller, |
||
3 | Inject, |
||
4 | Post, |
||
5 | Body, |
||
6 | BadRequestException, |
||
7 | UseGuards |
||
8 | } from '@nestjs/common'; |
||
9 | import { AuthGuard } from '@nestjs/passport'; |
||
10 | import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger'; |
||
11 | import { ICommandBus } from 'src/Application/ICommandBus'; |
||
12 | import { CreateSchoolCommand } from 'src/Application/School/Command/CreateSchoolCommand'; |
||
13 | import { UserRole } from 'src/Domain/User/User.entity'; |
||
14 | import { Roles } from 'src/Infrastructure/User/Decorator/Roles'; |
||
15 | import { RolesGuard } from 'src/Infrastructure/User/Security/RolesGuard'; |
||
16 | import { SchoolDTO } from '../DTO/SchoolDTO'; |
||
17 | |||
18 | @Controller('schools') |
||
19 | @ApiTags('School') |
||
20 | @ApiBearerAuth() |
||
21 | @UseGuards(AuthGuard('bearer'), RolesGuard) |
||
22 | export class CreateSchoolAction { |
||
23 | constructor( |
||
24 | @Inject('ICommandBus') |
||
25 | private readonly commandBus: ICommandBus |
||
26 | ) {} |
||
27 | |||
28 | @Post() |
||
29 | @Roles(UserRole.PHOTOGRAPHER) |
||
30 | @ApiOperation({ summary: 'Create new school' }) |
||
31 | public async index(@Body() dto: SchoolDTO) { |
||
32 | const { |
||
33 | reference, |
||
34 | name, |
||
35 | address, |
||
36 | zipCode, |
||
37 | city, |
||
38 | email, |
||
39 | phoneNumber, |
||
40 | numberOfClasses, |
||
41 | numberOfStudents, |
||
42 | observation, |
||
43 | status, |
||
44 | type |
||
45 | } = dto; |
||
46 | |||
47 | try { |
||
48 | const id = await this.commandBus.execute( |
||
49 | new CreateSchoolCommand( |
||
50 | reference, |
||
51 | name, |
||
52 | address, |
||
53 | zipCode, |
||
54 | city, |
||
55 | status, |
||
56 | type, |
||
57 | email, |
||
58 | phoneNumber, |
||
59 | numberOfStudents, |
||
60 | numberOfClasses, |
||
61 | observation |
||
62 | ) |
||
63 | ); |
||
64 | |||
65 | return { id }; |
||
66 | } catch (e) { |
||
67 | throw new BadRequestException(e.message); |
||
68 | } |
||
69 | } |
||
70 | } |
||
71 |