Skip to content

Commit d84d35d

Browse files
author
William Wang
committed
model and view
1 parent 195c75e commit d84d35d

27 files changed

+20395
-19
lines changed

.DS_Store

6 KB
Binary file not shown.

models/Article.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
var db = require('../libs/db');
2+
var GeneralErrors = require('../errors/GeneralErrors');
3+
4+
var Article = function(options) {
5+
this.id = options.id;
6+
this.title = options.title;
7+
this.content = options.content;
8+
this.createdAt = options.createdAt;
9+
};
10+
11+
Article.getAll = function(cb) {
12+
db.select()
13+
.from('article')
14+
.map(function(row) {
15+
return new Article(row);
16+
})
17+
.then(function(articleList) {
18+
cb(null, articleList);
19+
})
20+
.catch(function(err) {
21+
cb(new GeneralErrors.Database());
22+
});
23+
}
24+
25+
Article.get = function(articleId, cb) {
26+
db.select()
27+
.from('article')
28+
.where({
29+
id : articleId
30+
})
31+
.map(function(row) {
32+
return new Article(row);
33+
})
34+
.then(function(articleList) {
35+
if(articleList.length) {
36+
cb(null, articleList[0]);
37+
} else {
38+
cb(null, new GeneralErrors.NotFound());
39+
}
40+
41+
})
42+
.catch(function(err) {
43+
console.log(err);
44+
cb(new GeneralErrors.Database());
45+
});
46+
}
47+
48+
//instance fnuction
49+
Article.prototype.save = function (cb) {
50+
if(this.id) {
51+
db('article')
52+
.update({
53+
title : this.title,
54+
content : this.content
55+
})
56+
.where({
57+
id : this.id
58+
})
59+
.then(function() {
60+
cb(null);
61+
})
62+
.catch(function(err) {
63+
console.log(err);
64+
cb(null, new GeneralErrors.Database());
65+
})
66+
} else {
67+
db('article')
68+
.insert({
69+
title : this.title,
70+
content : this.content
71+
})
72+
.then(function(result) {
73+
this.id = result[0];
74+
cb(null, this);
75+
}.bind(this))
76+
.catch(function(err) {
77+
console.log(err);
78+
cb(null, new GeneralErrors.Database());
79+
});
80+
}
81+
};
82+
83+
84+
module.exports = Article;

public/.DS_Store

6 KB
Binary file not shown.
19.7 KB
Binary file not shown.

public/fonts/glyphicons-halflings-regular.svg

+288
Loading
44.3 KB
Binary file not shown.
22.9 KB
Binary file not shown.
17.6 KB
Binary file not shown.

public/images/about-bg.jpg

32.3 KB
Loading

public/images/contact-bg.jpg

283 KB
Loading

public/images/home-bg.jpg

169 KB
Loading

public/images/post-bg.jpg

138 KB
Loading

public/images/post-sample-image.jpg

112 KB
Loading

0 commit comments

Comments
 (0)