diff --git a/models/Article.js b/models/Article.js deleted file mode 100644 index f08a008..0000000 --- a/models/Article.js +++ /dev/null @@ -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); diff --git a/models/Comment.js b/models/Comment.js index 86a2fd6..6fff204 100644 --- a/models/Comment.js +++ b/models/Comment.js @@ -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 }, ); diff --git a/models/User.js b/models/User.js index ff832ad..2b77e6d 100644 --- a/models/User.js +++ b/models/User.js @@ -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');