Advertisement
Guest User

Untitled

a guest
Jan 25th, 2017
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. deletePost: (req, res) => {
  2.         "use strict";
  3.         let id = req.params.id;
  4.  
  5.         User.findOneAndRemove({_id: id}).then(user => {
  6.             user.prepareDelete();
  7.             res.redirect('/admin/user/all');
  8.         })
  9.     }
  10. ----------------
  11. //user.prepareDelete
  12. prepareDelete: function () {
  13.         let Comment = mongoose.model('Comment')
  14.         for (let comment of this.comments) {
  15.             Comment.findById(comment).then(comment => {
  16.                 comment.prepareDelete()
  17.                 comment.remove()
  18.             })
  19.         }
  20.  
  21.         let Problems = mongoose.model('Problems')
  22.         for (let problem of this.problems) {
  23.             Problems.findById(problem).then(problem => {
  24.                 "use strict";
  25.                 problem.prepareDelete()
  26.                 problem.remove()
  27.             })
  28.         }
  29.  
  30.     }
  31. ---------------
  32. //comment.prepareDelete
  33. prepareDelete: function () {
  34.         let User = mongoose.model('User')
  35.         User.findById(this.author).then(user => {
  36.             "use strict";
  37.             if (user) {
  38.                 user.comments.remove(this.id)
  39.                 user.save()
  40.             }
  41.         })
  42.  
  43.         let Problems = mongoose.model('Problems')
  44.         Problems.findById(this.target).then(problem => {
  45.             "use strict";
  46.             if (problem) {
  47.                 problem.comments.remove(this.id)
  48.                 problem.save()
  49.             }
  50.         })
  51.     }
  52. -------------------
  53. //problem.prepareDelete
  54. prepareDelete: function () {
  55.         let User = mongoose.model('User')
  56.         User.findById(this.author).then(user => {
  57.             "use strict";
  58.             if (user) {
  59.                 user.problems.remove(this.id)
  60.                 user.save()
  61.             }
  62.         })
  63.  
  64.         let Comment = mongoose.model('Comment')
  65.         for (let commentId of this.comments) {
  66.             Comment.findOneAndRemove(commentId).populate('author').then(comment => {
  67.                 "use strict";
  68.                 if (comment) {
  69.                     let author = comment.author
  70.                     let index = author.comments.indexOf(commentId)
  71.  
  72.                     let count = 1
  73.                     author.comments.splice(index, count);
  74.                     author.save()
  75.                     comment.save()
  76.                 }
  77.             })
  78.         }
  79.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement