-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileController.js
More file actions
67 lines (52 loc) · 1.93 KB
/
Copy pathProfileController.js
File metadata and controls
67 lines (52 loc) · 1.93 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
66
67
import { ProfileRepository } from "../repository";
import { GlobalHandler, ThrowableError, getMessage } from "../utils";
export default class ProfileController {
constructor(){
this.repository = new ProfileRepository();
}
async listProfiles(req, res) {
try {
const filters = req.filters;
const { includes } = req.query;
let profiles = await this.repository.findAll(filters, includes)
res.send(profiles)
} catch(error){
const sanitizedError = GlobalHandler.handle(error);
res.status(sanitizedError.code).send(sanitizedError)
}
}
async saveProfile(req, res) {
try {
const profile = req.body;
let savedProfile = await this.repository.store(profile)
res.send(savedProfile)
} catch (error){
const sanitizedError = GlobalHandler.handle(error);
res.status(sanitizedError.code).send(sanitizedError)
}
}
async updateProfileRoles(req, res) {
try {
const { id } = req.params;
const roles = req.body;
if(!Array.isArray(roles)) throw new ThrowableError(getMessage('bodyMustBeArray')('roles'), 'ValidationError', 400);
let updatedProfile = await this.repository.update(roles, id)
res.send(updatedProfile)
} catch (error){
const sanitizedError = GlobalHandler.handle(error);
res.status(sanitizedError.code).send(sanitizedError)
}
}
async deleteProfile(req, res) {
try {
const { id } = req.params;
await this.repository.delete(id)
res.json({
message: `Profile with id ${id} successful deleted!`
})
} catch (error){
const sanitizedError = GlobalHandler.handle(error);
res.status(sanitizedError.code).send(sanitizedError)
}
}
}