/
account.controller.ts
166 lines (153 loc) · 4.02 KB
/
account.controller.ts
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import {
Body,
Controller,
Delete,
Get,
HttpStatus,
Param,
Post,
Put,
Query,
Response,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiResponse, ApiUseTags } from '@nestjs/swagger';
import { InjectRepository } from '@nestjs/typeorm';
import { Response as ExpressResponse } from 'express';
import { Repository } from 'typeorm';
import { ValidationPipe } from '../../common';
import { FindManyQuery, FindOneQuery, FindQueryPipe } from '../../database';
import { Roles } from '../enums/Roles';
import { IsAccountOwnerGuard } from '../guards/isAccountOwner.guard';
import { RestrictRoles } from '../guards/restrictRoles.decorator';
import { RolesGuard } from '../guards/roles.guard';
import { Account } from './account.entity';
@Controller('api/account')
@UseGuards(RolesGuard)
@ApiBearerAuth()
@ApiUseTags('account')
export class AccountController {
constructor(
@InjectRepository(Account) private accountRepository: Repository<Account>,
) {}
@Post()
@RestrictRoles(Roles.ADMIN)
@ApiResponse({
description: 'The account has been created',
status: 201,
type: Account,
})
@ApiResponse({
description:
"One of the property of the body didn't pass the validation, error details on the message",
status: 400,
})
public async create(
@Body(new ValidationPipe())
body: Account,
) {
return this.accountRepository.save(body);
}
@Get()
@RestrictRoles(Roles.ADMIN)
@ApiResponse({
description: 'The accounts have been retrieved',
isArray: true,
status: 200,
type: Account,
})
public async find(
@Query(new FindQueryPipe(), new ValidationPipe())
query: FindManyQuery<Account>,
@Response() res: ExpressResponse,
) {
const { order, orderBy, ...otherOptions } = query;
const options: any = otherOptions;
if (order && orderBy) {
options.order = {
[orderBy]: order,
};
}
options.relations = ['role'];
const count = await this.accountRepository.count();
const accounts = Array.isArray(query.ids)
? await this.accountRepository.findByIds(query.ids)
: await this.accountRepository.find(options);
res.set('Content-Range', `account ${accounts.length}/${count}`);
res.json(accounts);
}
@Get(':id')
@UseGuards(IsAccountOwnerGuard)
@ApiResponse({
description: 'The account has been retrieved',
status: 200,
type: Account,
})
@ApiResponse({
description: 'The account has not been retrieved',
status: 204,
})
public async findOne(
@Param('id') id: string,
@Query(new FindQueryPipe(), new ValidationPipe())
query: FindOneQuery<Account>,
@Response() res: ExpressResponse,
) {
const user = await this.accountRepository.findOneById(id, query);
if (!user) {
res.status(HttpStatus.NO_CONTENT).json();
} else {
res.json(user);
}
}
@Delete(':id')
@UseGuards(IsAccountOwnerGuard)
@ApiResponse({
description: 'The account has been deleted',
status: 200,
})
@ApiResponse({
description: "The account doesn't exist",
status: 204,
})
public async remove(
@Param('id') id: string,
@Response() res: ExpressResponse,
) {
const value = await this.accountRepository.deleteById(id);
if (value) {
res.json();
} else {
res.status(HttpStatus.NO_CONTENT).json({});
}
}
@Put(':id')
@UseGuards(IsAccountOwnerGuard)
@ApiResponse({
description: 'The account has been updated',
status: 200,
type: Account,
})
@ApiResponse({
description: "The account doesn't exist",
status: 204,
})
@ApiResponse({
description:
"One of the property of the body didn't pass the validation, error details on the message",
status: 400,
})
public async update(
@Param('id') id: string,
@Body(new ValidationPipe())
account: Account,
@Response() res: ExpressResponse,
) {
const value = await this.accountRepository.update({ id }, account);
if (value) {
res.json();
} else {
res.status(HttpStatus.NO_CONTENT).json({});
}
}
}