-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmethods.js
129 lines (117 loc) · 4.94 KB
/
methods.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// This code runs on the client and server to enable latency compensation (the security of server code and no round-trip delay). See https://www.meteor.com/tutorials/blaze/security-with-methods for details.
Meteor.methods({
addDomain: function(name, code) {
if (!Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
else {
// Wrap the async db method so we can return the result value in the callback.
var func = Meteor.wrapAsync(function(callback) {
Domains.insert({ created: new Date(), user: Meteor.userId(), name: name, code: code }, function(err, id) {
callback(err, id);
});
});
return func();
}
},
addProblem: function(domain, name, code) {
if (!Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
else {
// Wrap the async db method so we can return the result value in the callback.
var func = Meteor.wrapAsync(function(callback) {
Problems.insert({ created: new Date(), user: Meteor.userId(), domain: domain, name: name, code: code }, function(err, id) {
callback(err, id);
});
});
return func();
}
},
updateDomain: function(domain, name, code) {
if (!Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
else {
// Wrap the async db method so we can return the result value in the callback.
var func = Meteor.wrapAsync(function(callback) {
Domains.update({ _id: domain, user: Meteor.userId() }, { $set: { lastModified: new Date(), name: name, code: code } }, function(err, count) {
if (!count) {
// Domain not found, must exist for 'public' user. Copy and insert under this user.
Domains.insert({ created: new Date(), user: Meteor.userId(), name: 'Copy of ' + name, code: code }, function(err, id) {
callback(err, id);
});
}
else {
callback(err, domain);
}
});
});
return func();
}
},
updateProblem: function(domain, problem, name, code) {
if (!Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
else {
var func = Meteor.wrapAsync(function(callback) {
Problems.update({ _id: problem, user: Meteor.userId() }, { $set: { lastModified: new Date(), name: name, code: code } }, function(err, count) {
if (!count) {
// Problem not found, must exist for 'public' user. Copy and insert under this user.
Problems.insert({ created: new Date(), user: Meteor.userId(), domain: domain, name: name, code: code }, function(err, id) {
callback(err, id);
});
}
else {
callback(err, problem);
}
});
});
return func();
}
},
removeDomain: function(domain) {
if (!Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
else {
var func = Meteor.wrapAsync(function(callback) {
// Remove all problems in this domain.
var problems = Problems.find({ domain: domain });
problems.forEach(function(problem) {
Problems.remove({ _id: problem._id, user: Meteor.userId() });
});
Domains.remove({ _id: domain, user: Meteor.userId() }, function(err, count) {
if (!count) {
// Domain not found, must exist for 'public' user.
callback(err, domain);
}
else {
callback(err, domain);
}
});
});
return func();
}
},
removeProblem: function(problem) {
if (!Meteor.userId()) {
throw new Meteor.Error("not-authorized");
}
else {
var func = Meteor.wrapAsync(function(callback) {
Problems.remove({ _id: problem, user: Meteor.userId() }, function(err, count) {
if (!count) {
// Problem not found, must exist for 'public' user.
callback(err, problem);
}
else {
callback(err, problem);
}
});
});
return func();
}
}
});