-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patharticleprovider-memory.js
72 lines (65 loc) · 1.94 KB
/
articleprovider-memory.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
var articleCounter = 1;
function ArticleProvider(){};
ArticleProvider.prototype = {
dummyData : [],
findAll : function(callback) {
callback( null, this.dummyData )
},
findById : function(id, callback) {
var result = null;
for(var i =0;i<this.dummyData.length;i++) {
if( this.dummyData[i]._id == id ) {
result = this.dummyData[i];
break;
}
}
callback(null, result);
},
delete : function(id, callback) {
var result = null;
for(var i =0;i<this.dummyData.length;i++) {
if( this.dummyData[i]._id == id ) {
this.dummyData.splice(i, 1);
break;
}
}
callback(null);
},
save : function(articles, callback) {
var article = null;
console.log(articles);
if( typeof(articles.length)=="undefined") {
if(articles.id != "NEW") {
this.findById(articles.id, function(err, oldArt) {
if(oldArt) {
oldArt.title = articles.title;
oldArt.body = articles.body;
}
callback(null, oldArt);
});
return;
}
articles._id = null;
articles = [articles];
}
for( var i =0;i< articles.length;i++ ) {
article = articles[i];
article._id = articleCounter++;
article.created_at = new Date();
if( article.comments === undefined )
article.comments = [];
for(var j =0;j< article.comments.length; j++) {
article.comments[j].created_at = new Date();
}
this.dummyData[this.dummyData.length]= article;
}
callback(null, articles);
}
}
/* Lets bootstrap with dummy data */
// ap.save([
// { title: 'Post one', body: 'Body one', comments:[{author:'Bob', comment:'I love it'}, {author:'Dave', comment:'This is rubbish!'}]},
// { title: 'Post two', body: 'Body two'},
// { title: 'Post three', body: 'Body three'}
// ], function(error, articles){});
exports.ArticleProvider = new ArticleProvider();