Advertisement
Guest User

Untitled

a guest
Feb 4th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. /**
  2. * List of Articles
  3. */
  4. exports.list = function (req, res) {
  5. Article.find({ 'user.displayName': 'GIGANTOR !' }).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
  6. if (err) {
  7. return res.status(422).send({
  8. message: errorHandler.getErrorMessage(err)
  9. });
  10. } else {
  11. res.json(articles);
  12. }
  13. });
  14. };
  15.  
  16. /**
  17. * Show the current article
  18. */
  19. exports.read = function (req, res) {
  20. // convert mongoose document to JSON
  21. var article = req.article ? req.article.toJSON() : {};
  22.  
  23. // Add a custom field to the Article, for determining if the current User is the "owner".
  24. // NOTE: This field is NOT persisted to the database, since it doesn't exist in the Article model.
  25. article.isCurrentUserOwner = !!(req.user && article.user && article.user._id.toString() === req.user._id.toString());
  26.  
  27. res.json(article);
  28. };
  29.  
  30. 'use strict';
  31.  
  32. /**
  33. * Module dependencies
  34. */
  35. var mongoose = require('mongoose'),
  36. Schema = mongoose.Schema;
  37.  
  38. /**
  39. * Article Schema
  40. */
  41. var ArticleSchema = new Schema({
  42. created: {
  43. type: Date,
  44. default: Date.now
  45. },
  46. title: {
  47. type: String,
  48. default: '',
  49. trim: true,
  50. required: 'Title cannot be blank'
  51. },
  52. content: {
  53. type: String,
  54. default: '',
  55. trim: true,
  56. required: 'Content cannot be blank'
  57. },
  58. user: {
  59. type: Schema.ObjectId,
  60. ref: 'User'
  61. }
  62. });
  63.  
  64. mongoose.model('Article', ArticleSchema);
  65.  
  66. /**
  67. * User Schema
  68. */
  69. var UserSchema = new Schema({
  70. displayName: {
  71. type: String,
  72. trim: true
  73. },
  74. email: {
  75. type: String,
  76. index: {
  77. unique: true,
  78. sparse: true // For this to work on a previously indexed field, the index must be dropped & the application restarted.
  79. },
  80. lowercase: true,
  81. trim: true,
  82. default: '',
  83. validate: [validateLocalStrategyEmail, 'Please fill a valid email address']
  84. },
  85. username: {
  86. type: String,
  87. unique: 'Username already exists',
  88. required: 'Please fill in a username',
  89. validate: [validateUsername, 'Please enter a valid username: 3+ characters long, non restricted word, characters "_-.", no consecutive dots, does not begin or end with dots, letters a-z and numbers 0-9.'],
  90. lowercase: true,
  91. trim: true
  92. },
  93. password: {
  94. type: String,
  95. default: ''
  96. },
  97. salt: {
  98. type: String
  99. },
  100. provider: {
  101. type: String,
  102. required: 'Provider is required'
  103. },
  104. providerData: {},
  105. additionalProvidersData: {},
  106. roles: {
  107. type: [{
  108. type: String,
  109. enum: ['user', 'admin']
  110. }],
  111. default: ['user'],
  112. required: 'Please provide at least one role'
  113. },
  114. updated: {
  115. type: Date
  116. },
  117. created: {
  118. type: Date,
  119. default: Date.now
  120. },
  121. /* For reset password */
  122. resetPasswordToken: {
  123. type: String
  124. },
  125. resetPasswordExpires: {
  126. type: Date
  127. }
  128. });
  129.  
  130. Article.find({}).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
  131. if (err) {
  132. return res.status(422).send({
  133. message: errorHandler.getErrorMessage(err)
  134. });
  135. } else {
  136. let filteredArticles = articles
  137. .filter(article => article.user.displayName === 'GIGANTOR !');
  138.  
  139. res.json(filteredArticles);
  140. }
  141. });
  142.  
  143. Article.find({ user: 'somemongoobjectidofuser' }).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
  144. if (err) {
  145. return res.status(422).send({
  146. message: errorHandler.getErrorMessage(err)
  147. });
  148. } else {
  149. res.json(articles);
  150. }
  151. });
  152.  
  153. /**
  154. * List of Articles
  155. */
  156. exports.list = function (req, res) {
  157. Article.find({ user: req.user._id.toString() }).sort('-created').populate('user', 'displayName').exec(function (err, articles) {
  158. if (err) {
  159. return res.status(422).send({
  160. message: errorHandler.getErrorMessage(err)
  161. });
  162. } else {
  163. res.json(articles);
  164. }
  165. });
  166. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement