35 lines
955 B
JavaScript
35 lines
955 B
JavaScript
var mongoose = require('mongoose');
|
|
var User = mongoose.model('User');
|
|
var slug = require('slug');
|
|
|
|
var AeronefSchema = new mongoose.Schema({
|
|
slug: { type: String, lowercase: true, unique: true },
|
|
aeronef: String,
|
|
imat: String,
|
|
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
|
|
}, {timestamps: true, toJSON: {virtuals: true}});
|
|
|
|
AeronefSchema.pre('validate', function (next) {
|
|
if (!this.slug) {
|
|
this.slugify();
|
|
}
|
|
next();
|
|
});
|
|
|
|
AeronefSchema.methods.slugify = function () {
|
|
this.slug = slug(`${this.aeronef} ${this.imat}`) + '-' + (Math.random() * Math.pow(36, 6) | 0).toString(36);
|
|
};
|
|
|
|
AeronefSchema.methods.toJSONFor = function(user){
|
|
return {
|
|
slug: this.slug,
|
|
aeronef: this.aeronef,
|
|
imat: this.imat,
|
|
createdAt: this.createdAt,
|
|
updatedAt: this.updatedAt,
|
|
author: this.author.toProfileJSONFor(user)
|
|
};
|
|
};
|
|
|
|
mongoose.model('Aeronef', AeronefSchema);
|