Upload files to 'models'

This commit is contained in:
Muhammad Iqbal Rifai 2018-07-04 10:56:29 +02:00
parent c32c228e7e
commit a8c710960e
3 changed files with 165 additions and 0 deletions

65
models/episodes.js Normal file
View File

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

66
models/series.js Normal file
View File

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

34
models/user.js Normal file
View File

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