-
Notifications
You must be signed in to change notification settings - Fork 0
/
Article.js
87 lines (77 loc) · 1.66 KB
/
Article.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const mongoose = require('mongoose');
// Article Schema
const articleSchema = mongoose.Schema({
title: {
type: String
},
subtitle: {
type: String
},
category: {
type: String
},
body: {
type: String
},
author: {
type: String
},
created_at: {
type: Date,
default: Date.now
},
comments: [{
comment_subject:{
type: String
},
comment_body:{
type: String
},
comment_author:{
type: String
},
comment_email:{
type: String
},
comment_date:{
type: String
},
}]
});
const Article = module.exports = mongoose.model('Article', articleSchema);
// Get Articles
module.exports.getArticles = function(callback, limit){
Article.find(callback).limit(limit).sort([['title', 'ascending']]);
}
// Get Article By Category
module.exports.getCategoryArticles = function(categoryId, callback){
let query = {category: categoryId}
Article.find(query, callback).sort([['title', 'ascending']]);
}
// Add Article
module.exports.addArticle = function(article, callback){
Article.create(article, callback);
}
// Get Single Article By Id
module.exports.getArticleById = function(id, callback){
Article.findById(id, callback);
}
// Update Article
module.exports.updateArticle = function(query, update, options, callback){
Article.findOneAndUpdate(query, update, options, callback);
}
// Remove Article
module.exports.removeArticle = function(query, callback){
Article.remove(query, callback);
}
// Add comment
module.exports.addComment = function(query, comment, callback){
Article.update(query,
{
$push: {
comments: comment
}
},
callback
);
}