diff --git a/models/episodes.js b/models/episodes.js new file mode 100644 index 0000000..03b5cfa --- /dev/null +++ b/models/episodes.js @@ -0,0 +1,65 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; +const slug = require('slugs'); + + +const episodeSchema= new Schema({ + + title: { + type: String,index: true + }, + slug: String, + serieTitle: String, + chapterTitle: String, + chapter: Number, + server: String, + serverTwo: String, + imageCap: String, + download: String, + anime: { + type: Schema.Types.ObjectId, + ref: 'serie' + } +}, { timestamps: {} } +//{ + //toJSON: { virtuals: true }, + //toObject: { virtuals: true }, +//}, +//{ runSettersOnQuery: true } + +); + + + + +//Da error +/* // Define our indexes +episodeSchema.index({ + title: 'text', +}); + +episodeSchema.index({ + location: 'indexloco' +}); + */ + +episodeSchema.pre('save', async function (next) { + if (!this.isModified('title')) { + next(); // skip it + return; // stop this function from running + } + this.slug = slug(this.title); + // find other stores that have a slug of wes, wes-1, wes-2 + const slugRegEx = new RegExp(`^(${this.slug})((-[0-9]*$)?)$`, 'i'); + const storesWithSlug = await this.constructor.find({ + slug: slugRegEx + }); + if (storesWithSlug.length) { + this.slug = `${this.slug}-${storesWithSlug.length + 1}`; //`` blackstring generacion de cadenas + } + next(); + // TODO make more resiliant so slugs are unique +}); + +const Episode = mongoose.model('episode', episodeSchema); +module.exports = Episode; diff --git a/models/series.js b/models/series.js new file mode 100644 index 0000000..47a39da --- /dev/null +++ b/models/series.js @@ -0,0 +1,66 @@ +const mongoose = require('mongoose'); +const Schema = mongoose.Schema; +const slug = require('slugs'); + + + +const serieSchema = new Schema({ + + title: { + type: String, index: true + }, + slug: String, + cover: String, + backgroundimage: String, + frontimage: String, + synopsis: String, + estate: String, + type: String, + tags: [String], + episodes: [{ + type: Schema.Types.ObjectId, + ref: 'episode' + }] +}, { + timestamps: {} +}); + + +serieSchema.pre('save', async function (next) { + if (!this.isModified('title')) { + next(); // skip it + return; // stop this function from running + } + this.slug = slug(this.title); + // find other stores that have a slug of wes, wes-1, wes-2 + const slugRegEx = new RegExp(`^(${this.slug})((-[0-9]*$)?)$`, 'i'); + const storesWithSlug = await this.constructor.find({ + slug: slugRegEx + }); + if (storesWithSlug.length) { + this.slug = `${this.slug}-${storesWithSlug.length + 1}`; //`` blackstring generacion de cadenas + } + next(); + // TODO make more resiliant so slugs are unique +}); + +serieSchema.statics.getTagsList = function() { + return this.aggregate([ + { $unwind: '$tags' }, + { $group: { _id: '$tags', count: { $sum: 1 } } }, + { $sort: { count: -1 } } + ]); + }; + +/* function autopopulate(next) { + this.populate('episodes', 'slug'); + next(); +} + +serieSchema.pre('find', autopopulate); +serieSchema.pre('findOne', autopopulate); + */ + + +const Serie = mongoose.model('serie', serieSchema); +module.exports = Serie; \ No newline at end of file diff --git a/models/user.js b/models/user.js new file mode 100644 index 0000000..d0ceeb9 --- /dev/null +++ b/models/user.js @@ -0,0 +1,34 @@ +const mongoose = require("mongoose"); +const Schema = mongoose.Schema; +const bcrypt = require("bcryptjs"); + +const userSchema = new Schema( + { + email: { + type: String + }, + username: String, + password: String + }, + { + timestamps: {} + } +); + +const User = mongoose.model("user", userSchema); +module.exports = User; +module.exports.hashPassword = async password => { + try { + const salt = await bcrypt.genSalt(10); + return await bcrypt.hash(password, salt); + } catch (error) { + throw new error("Hashing failed", error); + } +}; +module.exports.comparePasswords = async (inputPassword, hashedPassword) => { + try { + return await bcrypt.compare(inputPassword, hashedPassword); + } catch (error) { + throw new error("Comparing failed", error); + } +};