chore: remove Article model, it is unused, fix references from comments and favorites
This commit is contained in:
parent
11c806dbe3
commit
c5b5dda0b4
|
@ -1,54 +0,0 @@
|
||||||
const mongoose = require('mongoose');
|
|
||||||
const uniqueValidator = require('mongoose-unique-validator');
|
|
||||||
const slug = require('slug');
|
|
||||||
const User = mongoose.model('User');
|
|
||||||
|
|
||||||
const ArticleSchema = new mongoose.Schema(
|
|
||||||
{
|
|
||||||
slug: { type: String, lowercase: true, unique: true },
|
|
||||||
title: String,
|
|
||||||
description: String,
|
|
||||||
body: String,
|
|
||||||
favoritesCount: { type: Number, default: 0 },
|
|
||||||
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
|
|
||||||
tagList: [{ type: String }],
|
|
||||||
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
|
|
||||||
},
|
|
||||||
{ timestamps: true },
|
|
||||||
);
|
|
||||||
|
|
||||||
ArticleSchema.plugin(uniqueValidator, { message: 'is already taken' });
|
|
||||||
|
|
||||||
ArticleSchema.pre('validate', function (next) {
|
|
||||||
if (!this.slug) {
|
|
||||||
this.slugify();
|
|
||||||
}
|
|
||||||
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
|
|
||||||
ArticleSchema.methods.slugify = function () {
|
|
||||||
this.slug = slug(this.title) + '-' + ((Math.random() * Math.pow(36, 6)) | 0).toString(36);
|
|
||||||
};
|
|
||||||
|
|
||||||
ArticleSchema.methods.updateFavoriteCount = async function () {
|
|
||||||
this.favoritesCount = await User.count({ favorites: { $in: [this._id] } });
|
|
||||||
return await this.save();
|
|
||||||
};
|
|
||||||
|
|
||||||
ArticleSchema.methods.toJSONFor = function (user) {
|
|
||||||
return {
|
|
||||||
slug: this.slug,
|
|
||||||
title: this.title,
|
|
||||||
description: this.description,
|
|
||||||
body: this.body,
|
|
||||||
createdAt: this.createdAt,
|
|
||||||
updatedAt: this.updatedAt,
|
|
||||||
tagList: this.tagList,
|
|
||||||
favorited: user ? user.isFavorite(this._id) : false,
|
|
||||||
favoritesCount: this.favoritesCount,
|
|
||||||
author: this.author.toProfileJSONFor(user),
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
mongoose.model('Article', ArticleSchema);
|
|
|
@ -4,7 +4,7 @@ const CommentSchema = new mongoose.Schema(
|
||||||
{
|
{
|
||||||
body: String,
|
body: String,
|
||||||
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
|
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
|
||||||
article: { type: mongoose.Schema.Types.ObjectId, ref: 'Article' },
|
track: { type: mongoose.Schema.Types.ObjectId, ref: 'Track' },
|
||||||
},
|
},
|
||||||
{ timestamps: true },
|
{ timestamps: true },
|
||||||
);
|
);
|
||||||
|
|
|
@ -24,7 +24,7 @@ const UserSchema = new mongoose.Schema(
|
||||||
},
|
},
|
||||||
bio: String,
|
bio: String,
|
||||||
image: String,
|
image: String,
|
||||||
favorites: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Article' }],
|
favorites: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Track' }],
|
||||||
following: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
|
following: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
|
||||||
areTracksVisibleForAll: Boolean,
|
areTracksVisibleForAll: Boolean,
|
||||||
hash: String,
|
hash: String,
|
||||||
|
@ -131,4 +131,4 @@ UserSchema.methods.isFollowing = function (id) {
|
||||||
|
|
||||||
mongoose.model('User', UserSchema);
|
mongoose.model('User', UserSchema);
|
||||||
|
|
||||||
module.exports = mongoose.model('User')
|
module.exports = mongoose.model('User');
|
||||||
|
|
Loading…
Reference in a new issue