-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqaItemController.js
executable file
·71 lines (57 loc) · 1.53 KB
/
qaItemController.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
"use strict";
var qaItems = [];
function findItemIndex(id) {
var idx = qaItems.findIndex(
function(qa) {
return (qa.id === Number(id));
}
);
if (idx >= 0 ) {
console.log('found element at index = ' + idx);
}
return idx;
}
var qaItemController = {
// create
qaItemCreate: function (qaItem) {
// todo: validate if qa isn't empty
// generate unique id;
var maxId = 0;
qaItems.forEach(function(item){
if (item.id > maxId) {
maxId = item.id;
}
});
qaItem.id = maxId + 1;
qaItems.push(qaItem);
return qaItem;
},
// read
getAll: function() {
return qaItems;
},
getCategory: function(category) {
return qaItems.filter(function(item) {
return (item.category.toLowerCase() === category.toLowerCase());
});
},
// update
updateQA(item) {
// todo: validate if qa isn't empty
var idx = findItemIndex(item.id);
if (idx >= 0) {
qaItems[idx].question = item.question;
qaItems[idx].answer = item.answer;
// todo: finish the rest of the properties
}
return qaItems[idx];
} ,
// delete
deleteQA(id) {
var idx = findItemIndex(id);
if (idx >= 0) {
return qaItems.splice(idx, 1);
}
}
}
module.exports = qaItemController;