This repository has been archived by the owner on Apr 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 168
/
project.js
60 lines (53 loc) · 1.48 KB
/
project.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
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright IBM Corp. 2015,2017. All Rights Reserved.
// Node module: loopback-example-access-control
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
module.exports = function(Project) {
// listProjects
Project.listProjects = function(cb) {
Project.find({
fields: {
balance: false
}
}, cb);
};
Project.remoteMethod('listProjects', {
returns: {arg: 'projects', type: 'array'},
http: {path:'/list-projects', verb: 'get'}
});
// donate
Project.donate = function(id, amount, cb) {
Project.findById(id, function(err, project) {
if (err) return cb(err);
project.balance += amount;
project.save();
cb(null, true);
});
};
Project.remoteMethod('donate', {
accepts: [
{arg: 'id', type: 'number'},
{arg: 'amount', type: 'number'}
],
returns: {arg: 'success', type: 'boolean'},
http: {path:'/donate', verb: 'post'}
});
// withdraw
Project.withdraw = function(id, amount, cb) {
Project.findById(id, function(err, project) {
if (err) return cb(err);
project.balance = project.balance >= amount ?
project.balance - amount : 0;
project.save();
cb(null, true);
});
};
Project.remoteMethod('withdraw', {
accepts: [
{arg: 'id', type: 'number'},
{arg: 'amount', type: 'number'}
],
returns: {arg: 'success', type: 'boolean'},
http: {path:'/withdraw', verb: 'post'}
});
};