forked from fonoster/routr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusers_api.js
47 lines (36 loc) · 1.11 KB
/
users_api.js
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
/**
* @author Pedro Sanders
* @since v1
*/
const CoreUtils = require('@routr/core/utils')
const DSUtils = require('@routr/data_api/utils')
const {
Status
} = require('@routr/core/status')
class UsersAPI {
constructor(dataSource) {
this.ds = dataSource
}
createFromJSON(jsonObj) {
return this.userExist(jsonObj.spec.credentials.username) ? CoreUtils.buildResponse(Status.CONFLICT) : this.ds.insert(jsonObj)
}
updateFromJSON(jsonObj) {
return !this.userExist(jsonObj.spec.credentials.username) ? CoreUtils.buildResponse(Status.NOT_FOUND) : this.ds.update(jsonObj)
}
getUsers(filter) {
return this.ds.withCollection('users').find(filter)
}
getUser(ref) {
return this.ds.withCollection('users').get(ref)
}
getUserByUsername(username) {
return DSUtils.deepSearch(this.getUsers(), "spec.credentials.username", username)
}
userExist(username) {
return DSUtils.objExist(this.getUser(username))
}
deleteUser(ref) {
return this.ds.withCollection('users').remove(ref)
}
}
module.exports = UsersAPI