Guest User

Untitled

a guest
Nov 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. const mongoose = require('mongoose');
  2. const Schema = mongoose.Schema;
  3. const logoSchema = require('./site-sections/logo');
  4.  
  5. var sectionSchema = new Schema(
  6. {
  7. show: { type: Boolean, default: true },
  8. order: Number
  9. },
  10. { discriminatorKey: 'type' }
  11. );
  12.  
  13. const siteSchema = new Schema({
  14. _user: { type: Schema.Types.ObjectId, ref: 'User' },
  15. type: { type: String, required: true },
  16. title: { type: String, default: '' },
  17. name: { type: String, required: true },
  18. password: { type: String, default: '' },
  19. caching: { type: Number, default: 1 },
  20. unique_id: { type: String, required: true },
  21. sections: [sectionSchema]
  22. });
  23.  
  24. const sectionArray = siteSchema.path('sections');
  25.  
  26. const headerSchema = new Schema({
  27. image: { type: String, default: '' },
  28. title: { type: String, default: '' },
  29. sub_title: { type: String, default: '' },
  30. show: { type: Boolean, default: true },
  31. logo: logoSchema
  32. });
  33.  
  34. sectionArray.discriminator('header', headerSchema);
  35.  
  36. const textSchema = new Schema({
  37. text: String
  38. });
  39.  
  40. sectionArray.discriminator('text', textSchema);
  41.  
  42. module.exports = mongoose.model('site', siteSchema);
  43.  
  44. const Site = require('../../models/site');
  45.  
  46. exports.update = async function(req, res, next) {
  47. console.log(req.body);
  48. if (req.body.unique_site_id) {
  49. Site.update(
  50. {
  51. unique_id: req.body.unique_site_id,
  52. _user: req.user.id,
  53. 'sections.type': 'header'
  54. },
  55. {
  56. $set: {
  57. ['sections.$.' + req.body.key]: req.body.value
  58. }
  59. },
  60. function(err, status) {
  61. if (err) {
  62. console.log(err);
  63. return res.status(500).send();
  64. }
  65. console.log(status);
  66. return res.status(200).send();
  67. }
  68. );
  69. }
  70. };
Add Comment
Please, Sign In to add comment