---|main| Initial commit

This commit is contained in:
Julien Gautier
2023-09-12 21:43:57 +02:00
commit 7e9599118e
42 changed files with 8015 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
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);