35 lines
949 B
JavaScript
35 lines
949 B
JavaScript
var mongoose = require('mongoose');
|
|
var User = mongoose.model('User');
|
|
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);
|