Guest User

Untitled

a guest
Dec 3rd, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. 'use strict';
  2.  
  3. const bcrypt = require('bcryptjs');
  4. const mongoose = require('mongoose');
  5. mongoose.Promise = global.Promise;
  6.  
  7. const userSchema = new mongoose.Schema({
  8. username: {type: String, required: true, unique: true},
  9. password: {type: String, required: true},
  10. firstName: {type: String, default: ''},
  11. lastName: {type: String, default: ''}
  12. });
  13.  
  14. userSchema.methods.apiRepr = function () {
  15. return {
  16. id: this._id,
  17. username: this.username,
  18. firstName: this.firstName,
  19. lastName: this.lastName
  20. };
  21. };
  22.  
  23. userSchema.methods.validatePassword = function(password) {
  24. return bcrypt.compare(password, this.password);
  25. };
  26.  
  27. userSchema.statics.hashPassword = function(password) {
  28. return bcrypt.hash(password, 10);
  29. };
  30.  
  31. //-----------------------------------------//
  32.  
  33. const blogPostSchema = mongoose.Schema({
  34. author: {
  35. firstName: String,
  36. lastName: String
  37. },
  38. title: {type: String, required: true},
  39. content: {type: String},
  40. // created: {type: Date, default: Date.now}
  41. });
  42.  
  43.  
  44. blogPostSchema.virtual('authorName').get(function() {
  45. return `${this.author.firstName} ${this.author.lastName}`.trim();
  46. });
  47.  
  48. blogPostSchema.methods.apiRepr = function() {
  49. return {
  50. id: this._id,
  51. author: this.authorName,
  52. content: this.content,
  53. title: this.title,
  54. created: this.created
  55. };
  56. };
  57.  
  58. const UserModel = mongoose.model('User', userSchema);
  59. const BlogPost = mongoose.model('BlogPost', blogPostSchema, 'blog-posts');
  60.  
  61. module.exports = {BlogPost, UserModel};
Add Comment
Please, Sign In to add comment