Advertisement
Guest User

Untitled

a guest
Sep 30th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. //blog.js
  2.  
  3. var mongoose = require('mongoose');
  4. var Schema = mongoose.Schema;
  5. var User = require('./user.js');
  6. var Entry = require('./entry.js');
  7. var Follower = require('./follower.js');
  8.  
  9. var Blog = new Schema({
  10. author: {
  11. type: Schema.Types.ObjectId,
  12. ref: 'User',
  13. required: true
  14. },
  15. name: {
  16. type: String,
  17. required: true
  18. },
  19. headerImageUrl: String,
  20. description: {
  21. type: String,
  22. required: true
  23. }
  24. }, {
  25. timestamps: true
  26. });
  27.  
  28. Blog.pre('remove', function(next) {
  29. Entry.find({"blogId": this._id})
  30. .exec(function(err, entries) {
  31. console.log("found entries " + entries);
  32. for(var i = entries.length -1; i >= 0; i--) {
  33. entries[i].remove();
  34. }
  35. });
  36. Follower.remove({"blogId": this._id}).exec();
  37. next();
  38. });
  39.  
  40. module.exports = mongoose.model('Blog', Blog);
  41.  
  42.  
  43. //entry.js
  44.  
  45. var mongoose = require('mongoose');
  46. var Schema = mongoose.Schema;
  47. var Blog = require('./blog.js');
  48. var Comment = require('./comment.js');
  49.  
  50. var Entry = new Schema({
  51. blogId: {
  52. type: Schema.Types.ObjectId,
  53. ref: 'Blog'
  54. },
  55. title: String,
  56. thumbnailUrl: String,
  57. content: String
  58. }, {
  59. timestamps: true
  60. });
  61.  
  62. Entry.pre('remove', function(next) {
  63. Comment.remove({"entryId": this._id}).exec();
  64. next();
  65. });
  66.  
  67. module.exports = mongoose.model('Entry', Entry);
  68.  
  69.  
  70.  
  71. //comment.js
  72.  
  73. var mongoose = require('mongoose');
  74. var Schema = mongoose.Schema;
  75. var User = require('./user.js');
  76. var Entry = require('./entry.js');
  77. var Post = require('./post.js');
  78.  
  79. var Comment = new Schema({
  80. text: {
  81. type: String,
  82. required: true
  83. },
  84. postedBy: {
  85. type: Schema.Types.ObjectId,
  86. ref: 'User',
  87. required: true
  88. },
  89. parentComment: this,
  90. entryId: {
  91. type: Schema.Types.ObjectId,
  92. ref: 'Entry'
  93. },
  94. postId: {
  95. type: Schema.Types.ObjectId,
  96. ref: 'Post'
  97. },
  98. type: {
  99. type: String,
  100. enum: ['BLOG', 'FORUM', 'COMMENT'],
  101. required: true
  102. }
  103. }, {
  104. timestamps: true
  105. });
  106.  
  107. module.exports = mongoose.model('Comment', Comment);
  108.  
  109. blogRouter.route('/:blogId')
  110.  
  111. //delete a specific blog by blog id: [OWNER OR ADMIN USER]
  112. .delete(Verify.verifyOrdinaryUser, Verify.verifyBlogOwnerOrAdmin, function(req, res, next) {
  113. Blog.findById(req.params.blogId, function(err, blog) {
  114. if(err) return next(err);
  115. blog.remove();
  116. res.json(blog);
  117. });
  118. });
  119.  
  120. //forum.js
  121.  
  122. var mongoose = require('mongoose');
  123. var Schema = mongoose.Schema;
  124. var User = require('./user.js');
  125. var Post = require('./post.js');
  126. var Subscription = require('./subscription.js');
  127.  
  128. var Forum = new Schema({
  129. title: {
  130. type: String,
  131. required: true
  132. },
  133. description: String,
  134. moderators: [
  135. {
  136. type: Schema.Types.ObjectId,
  137. ref: 'User'
  138. }
  139. ]
  140. }, {
  141. timestamps: true
  142. });
  143.  
  144. Forum.pre('remove', function(next) {
  145. Post.find({"forumId": this._id})
  146. .exec(function(err, posts) {
  147. console.log("found posts " + posts);
  148. for(var i = posts.length -1; i >= 0; i--) {
  149. posts[i].remove();
  150. }
  151. });
  152. Subscription.remove({"forumId": this._id}).exec();
  153. next();
  154. });
  155.  
  156. module.exports = mongoose.model('Forum', Forum);
  157.  
  158.  
  159. //post.js
  160.  
  161. var mongoose = require('mongoose');
  162. var Schema = mongoose.Schema;
  163. var User = require('./user.js');
  164. var Forum = require('./forum.js');
  165. var Comment = require('./comment.js');
  166.  
  167. var Post = new Schema({
  168. createdBy: {
  169. type: Schema.Types.ObjectId,
  170. ref: 'User',
  171. required: true
  172. },
  173. postText: {
  174. type: String,
  175. required: true
  176. },
  177. forumId: {
  178. type: Schema.Types.ObjectId,
  179. ref: 'Forum'
  180. },
  181. }, {
  182. timestamps: true
  183. });
  184.  
  185. Post.pre('remove', function(next) {
  186. Comment.remove({"postId": this._id}).exec();
  187. next();
  188. });
  189.  
  190. module.exports = mongoose.model('Post', Post);
  191.  
  192.  
  193. //comment.js [SAME AS INCLUDED ABOVE]
  194.  
  195. forumRouter.route('/:forumId')
  196.  
  197. //delete forum by id: [ADMIN]
  198. .delete(Verify.verifyOrdinaryUser, Verify.verifyAdmin, function(req, res, next) {
  199. Forum.findById(req.params.forumId, function(err, forum) {
  200. if(err) return next(err);
  201. forum.remove();
  202. res.json(forum);
  203. });
  204. });
  205.  
  206. postRouter.route('/')
  207.  
  208. //retirieve all forum posts: [ALL USERS]
  209. .get(function(req, res, next) {
  210. Post.find({})
  211. .populate({
  212. path: 'forumId',
  213. model: 'Forum',
  214. populate: {
  215. path: 'moderators',
  216. model: 'User'
  217. }
  218. })
  219. .populate('createdBy')
  220. .exec(function(err, posts){
  221. if(err) return next(err);
  222. res.json(posts);
  223. });
  224. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement