-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileRepository.js
More file actions
68 lines (55 loc) · 1.63 KB
/
Copy pathProfileRepository.js
File metadata and controls
68 lines (55 loc) · 1.63 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
68
import { ProfileModel } from "../model";
import { ThrowableError } from "../utils";
import { getMessage } from "../utils";
export default class ProfileRepository {
/**
* List all profiles in DB
* @param {object} [filters={}]
* @param {string[]} [includes=[]]
* @memberof ProfileRepository
*/
async findAll(filters = {}, includes = null){
let profiles = await ProfileModel
.find(filters)
.populate(includes)
return profiles
}
/**
* Store a Profile in DB
* @param {ProfileModel} profile
* @memberof ProfileRepository
*/
async store(profile){
let storedProfile = await ProfileModel.create(profile);
return storedProfile;
}
/**
* Update roles in a Profile
* @param {number[]} roles
* @param {number} id
* @memberof ProfileRepository
*/
async update(roles, id){
let foundProfile = await ProfileModel
.findById(id);
if(!foundProfile) {
throw new ThrowableError(getMessage('profileNotFound')('id', id), 'MongoError', 404);
}
foundProfile.roles = roles
await foundProfile.save();
return foundProfile;
}
/**
* Delete a Profile in DB
* @param {number} id
* @memberof ProfileRepository
*/
async delete(id){
let foundProfile = await ProfileModel.findById(id);
if(!foundProfile) {
throw new ThrowableError(getMessage('profileNotFound')('id', id), 'MongoError', 404);
}
await ProfileModel.deleteOne({ _id: id });
return getMessage('profileDeleted')(id);
}
}