UNPKG

1.56 kBJavaScriptView Raw
1var d = require("debug")("raptorjs:User")
2const Base = require("../Base")
3const Pager = require("../pager")
4const UserModel = require("../model/User")
5
6class User extends Base {
7
8 create(usr) {
9 return this.getClient().post(this.route("USER_CREATE"), usr)
10 .then((u) => new UserModel(u))
11 }
12 update(user) {
13 if(!user.id) throw new Error("User id is missing")
14 return this.getClient().put(this.route("USER_UPDATE", user.id), user)
15 .then((u) => new UserModel(u))
16 }
17 save(user) {
18 if(user.id) {
19 return this.update(user)
20 }
21 return this.create(user)
22 }
23 delete(user, query={}) {
24 if (typeof user === "string") {
25 user = { id: user }
26 }
27 if(!user.id) throw new Error("User id is missing")
28 const url = this.route("USER_DELETE", user.id) + Pager.buildQuery(null, query)
29 return this.getClient().delete(url)
30 }
31 read(id) {
32 id = id ? id.id || id : null
33 const path = id ? this.route("USER_GET", id) : this.route("USER_GET_ME")
34 return this.getClient().get(path)
35 }
36 me() {
37 return this.read()
38 }
39 list(query={}, pager={}) {
40 d(pager)
41 const url = this.route("USER_LIST") + Pager.buildQuery(pager, query)
42 return this.getClient().get(url)
43 .then((list) => Promise.resolve(Pager.create(list, (u) => new UserModel(u))))
44 }
45
46 can(req) {
47 return this.getClient().post(this.route("PERMISSION_CHECK"), req)
48 }
49}
50
51
52module.exports = User