diff --git a/models/asset.js b/models/asset.js index 3e620e553..161c62154 100644 --- a/models/asset.js +++ b/models/asset.js @@ -17,6 +17,25 @@ const AssetSchema = new Schema({ _id: false }); +/** + * Finds an asset by the id. + * @param {String} id identifier of the asset (uuid) +*/ +AssetSchema.methods.findById = function(id, done) { + Asset.findOne({ + id : id + }, (err, asset) => { + if (err) { + return done(err); + } + + if (!asset) { + return done(null, false); + } + return done(null, asset); + }); +}; + const Asset = mongoose.model('Asset', AssetSchema); module.exports = Asset; diff --git a/models/comments.js b/models/comments.js index 73538d378..bf54fcf82 100644 --- a/models/comments.js +++ b/models/comments.js @@ -49,8 +49,8 @@ const CommentSchema = new Schema({ }); /** - * Finds a user by the id. - * @param {String} id identifier of the user (uuid) + * Finds a comment by the id. + * @param {String} id identifier of the comment (uuid) */ CommentSchema.methods.findById = function(id, done) { Comment.findOne({ diff --git a/models/user.js b/models/user.js index 31213101c..56cc476d0 100644 --- a/models/user.js +++ b/models/user.js @@ -10,13 +10,35 @@ const UserSchema = new Schema({ default: uuid.v4, unique: true }, - name: String, + name: { + type: String, + unique: true + }, created_at: { type: Date, default: Date.now }, updated_at: { type: Date, default: Date.now } },{ _id: false }); +/** + * Finds a user by the id. + * @param {String} id identifier of the user (uuid) +*/ +UserSchema.methods.findById = function(id, done) { + User.findOne({ + id : id + }, (err, user) => { + if (err) { + return done(err); + } + + if (!user) { + return done(null, false); + } + return done(null, user); + }); +}; + const User = mongoose.model('User', UserSchema); module.exports = User;