-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharticle.model.js
57 lines (52 loc) · 1.43 KB
/
article.model.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
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
var sendgrid = require('../Sendgrid/SendgridFunctions')
//const PrettyUrl = require('./Utils/PrettyUrl');
let ArticleSchema = new Schema({
title: {
type: String,
required: true,
},
author: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
body: {
type: Object,
required: true,
},
createdAt: {
type: Date, default: Date.now ,
},
comments: [
{ body: {type: String, required: true}, date: { type: Date, default: Date.now }, author: String }
],
meta: {
votes: Number,
},
slug: {
type: String
},
isDraft: {
type: Boolean, default: false
},
});
ArticleSchema.statics.newArticle = function (title, url, userList) {
let slug = encodeURIComponent(title)
console.log("requesting " + slug + " from article schema")
Article.findOne({ slug: slug })
.populate('author', 'email username')
.exec(function (err, article) {
//console.log(slug)
if (err || !article) {
console.log(err + 'Could not find article');
} else {
// console.log("heres your article")
//console.log(article.title + " by: " + article.author.username)
sendgrid.sendNewArticle(article, url, userList)
}
})
}
var Article = mongoose.model('Article', ArticleSchema);
module.exports = Article