Ajout initial des fichiers du projet
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
var mongoose = require('mongoose');
|
||||
var slug = require('slug');
|
||||
|
||||
var CanopySchema = new mongoose.Schema({
|
||||
slug: { type: String, lowercase: true, unique: true },
|
||||
voile: String,
|
||||
taille: Number,
|
||||
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
|
||||
}, {timestamps: true, toJSON: {virtuals: true}});
|
||||
|
||||
CanopySchema.pre('validate', function (next) {
|
||||
if (!this.slug) {
|
||||
this.slugify();
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
CanopySchema.methods.slugify = function () {
|
||||
this.slug = slug(`${this.voile} ${this.taille}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
|
||||
};
|
||||
|
||||
CanopySchema.methods.toJSONFor = function(user){
|
||||
return {
|
||||
slug: this.slug,
|
||||
voile: this.voile,
|
||||
taille: this.taille,
|
||||
createdAt: this.createdAt,
|
||||
updatedAt: this.updatedAt,
|
||||
author: this.author.toProfileJSONFor(user)
|
||||
};
|
||||
};
|
||||
|
||||
mongoose.model('Canopy', CanopySchema);
|
||||
Reference in New Issue
Block a user