2020-11-20 10:02:30 +00:00
|
|
|
const mongoose = require('mongoose');
|
2020-04-13 00:02:40 +00:00
|
|
|
|
2020-11-20 10:02:30 +00:00
|
|
|
const CommentSchema = new mongoose.Schema(
|
|
|
|
{
|
|
|
|
body: String,
|
|
|
|
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
|
|
|
|
article: { type: mongoose.Schema.Types.ObjectId, ref: 'Article' },
|
|
|
|
},
|
|
|
|
{ timestamps: true },
|
|
|
|
);
|
2020-04-13 00:02:40 +00:00
|
|
|
|
|
|
|
// Requires population of author
|
2020-11-20 10:02:30 +00:00
|
|
|
CommentSchema.methods.toJSONFor = function (user) {
|
2020-04-13 00:02:40 +00:00
|
|
|
return {
|
|
|
|
id: this._id,
|
|
|
|
body: this.body,
|
|
|
|
createdAt: this.createdAt,
|
2020-11-20 10:02:30 +00:00
|
|
|
author: this.author.toProfileJSONFor(user),
|
2020-04-13 00:02:40 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
mongoose.model('Comment', CommentSchema);
|