-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathEditProfileController.ts
More file actions
65 lines (60 loc) · 1.8 KB
/
Copy pathEditProfileController.ts
File metadata and controls
65 lines (60 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {
BadRequestException,
Body,
Controller,
Get,
Inject,
Post,
Render,
Res,
UseGuards
} from '@nestjs/common';
import { LoggedUser } from '../Decorator/LoggedUser';
import { User } from 'src/Domain/HumanResource/User/User.entity';
import { UserView } from 'src/Application/HumanResource/User/View/UserView';
import { IsAuthenticatedGuard } from '../Security/IsAuthenticatedGuard';
import { ICommandBus } from 'src/Application/ICommandBus';
import { ProfileDTO } from '../DTO/ProfileDTO';
import { UpdateProfileCommand } from 'src/Application/HumanResource/User/Command/UpdateProfileCommand';
import { RouteNameResolver } from 'src/Infrastructure/Common/ExtendedRouting/RouteNameResolver';
import { Response } from 'express';
import { WithName } from 'src/Infrastructure/Common/ExtendedRouting/WithName';
@Controller('app/profile/edit')
@UseGuards(IsAuthenticatedGuard)
export class EditProfileController {
constructor(
@Inject('ICommandBus')
private readonly commandBus: ICommandBus,
private readonly resolver: RouteNameResolver
) {}
@Get()
@WithName('profile_edit')
@Render('pages/profile/edit.njk')
public async get(@LoggedUser() user: User) {
const me = new UserView(
user.getId(),
user.getFirstName(),
user.getLastName(),
user.getEmail(),
user.getRole(),
false,
);
return { user: me };
}
@Post()
public async post(
@Body() dto: ProfileDTO,
@LoggedUser() user: User,
@Res() res: Response
) {
try {
const { firstName, lastName, email, password } = dto;
await this.commandBus.execute(
new UpdateProfileCommand(user, firstName, lastName, email, password)
);
res.redirect(303, this.resolver.resolve('home'));
} catch (e) {
throw new BadRequestException(e.message);
}
}
}