chore: remove Article model, it is unused, fix references from comments and favorites

This commit is contained in:
Paul Bienkowski 2020-11-25 15:49:11 +01:00
parent 11c806dbe3
commit c5b5dda0b4
3 changed files with 3 additions and 57 deletions

View file

@ -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);

View file

@ -4,7 +4,7 @@ const CommentSchema = new mongoose.Schema(
{
body: String,
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 },
);

View file

@ -24,7 +24,7 @@ const UserSchema = new mongoose.Schema(
},
bio: 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' }],
areTracksVisibleForAll: Boolean,
hash: String,
@ -131,4 +131,4 @@ UserSchema.methods.isFollowing = function (id) {
mongoose.model('User', UserSchema);
module.exports = mongoose.model('User')
module.exports = mongoose.model('User');