-
Notifications
You must be signed in to change notification settings - Fork 4
/
survey-storage.js
50 lines (46 loc) · 1.56 KB
/
survey-storage.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
let currentId = 1;
function SurveyStorage (dbQueryAdapter) {
function addSurvey (name, callback) {
const newObj = {
name: name || ("New Survey" + " " + currentId++),
json: "{}"
};
dbQueryAdapter.create("surveys", newObj, id => {
newObj.id = id;
callback(newObj);
});
}
function postResults (postId, json, callback) {
const newObj = {
postid: postId,
json: json
};
dbQueryAdapter.create("results", newObj, id => {
newObj.id = id;
callback(newObj);
});
}
return {
addSurvey: addSurvey,
getSurvey: (surveyId, callback) => {
dbQueryAdapter.retrieve("surveys", [{ name: "id", op: "=", value: surveyId }], (results) => { callback(results[0]); });
},
storeSurvey: (id, _, json, callback) => {
dbQueryAdapter.update("surveys", { id: id, json: json }, (results) => { callback(results); });
},
getSurveys: (callback) => {
dbQueryAdapter.retrieve("surveys", [], (results) => { callback(results); });
},
deleteSurvey: (surveyId, callback) => {
dbQueryAdapter.delete("surveys", surveyId, (results) => { callback(results); });
},
postResults: postResults,
getResults: (postId, callback) => {
dbQueryAdapter.retrieve("results", [{ name: "postid", op: "=", value: postId }], (results) => { callback({ id: postId, data: results.map(r => r.json)}); });
},
changeName: (id, name, callback) => {
dbQueryAdapter.update("surveys", { id: id, name: name }, (results) => { callback(results); });
}
};
}
module.exports = SurveyStorage;