-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrepresentatives-resource.js
More file actions
94 lines (79 loc) · 2.42 KB
/
representatives-resource.js
File metadata and controls
94 lines (79 loc) · 2.42 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
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
var Speaker = require("./models/speaker");
var mongojs = require("mongojs");
var connection_string = process.env.MONGOLAB_URI || '127.0.0.1:27017/roisalen';
var db = mongojs(connection_string, []);
function getSpeakerFromDB(organisation, speakerId, callBack) {
var representatives = db.collection(organisation+"-representatives");
representatives.findOne({number: speakerId}, function(err, success) {
if (success){
return callBack(success);
}
return callBack(false);
});
}
module.exports.getSpeakerFromDB = getSpeakerFromDB;
module.exports.getAll = function(req, res, next) {
var representatives = db.collection(req.header('X-organisation')+"-representatives");
representatives.find().sort({number: 1}, function(err, success){
if (success) {
res.status(200).send(success);
} else {
res.status(500).send();
return next(err);
}
});
};
module.exports.get = function(req, res, next) {
getSpeakerFromDB(req.header('X-organisation'), parseInt(req.params.speakerId), function(speaker) {
if (speaker) {
res.status(200).send(speaker);
return next();
} else {
res.status(500).send();
return next();
}
});
};
module.exports.delete = function(req, res, next) {
var representatives = db.collection(req.header('X-organisation')+"-representatives");
representatives.remove({number: parseInt(req.params.speakerId)}, function(err, success){
if (success) {
res.status(200).send();
return next();
} else {
res.status(500).send();
}
return next(err);
});
}
module.exports.deleteAll = function(req, res, next) {
var representatives = db.collection(req.header('X-organisation')+"-representatives");
representatives.remove({},function(err, success) {
if (success) {
console.log("deleted all");
res.status(200).send();
} else {
res.status(500).send();
}
return next(err);
});
}
module.exports.add = function(req, res, next) {
var representatives = db.collection(req.header('X-organisation')+"-representatives");
var speakerJson = req.body;
var speakerNumber = parseInt(speakerJson.number);
if (isNaN(speakerNumber)) {
res.status(400).send("Speaker number must be a number");
return next();
}
var speaker = new Speaker(speakerJson.name, speakerNumber, speakerJson.sex, speakerJson.group);
representatives.save(speaker, function(err, success) {
if (success) {
res.status(201).send(speaker);
return next();
} else {
res.status(500).send();
return next(err);
}
});
}