Guest User

Untitled

a guest
Jan 23rd, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. const mongoose = require("mongoose");
  2. const Schema = mongoose.Schema;
  3.  
  4. const reviewSchema = new Schema ({
  5.  
  6. companyName: String,
  7. companyId: { type: Schema.Types.ObjectId, ref: 'Company'},
  8. starRating: Number,
  9. subject: String,
  10. commentBody: String,
  11. createdBy: { type: Schema.Types.ObjectId, ref: 'User'},
  12.  
  13. });
  14.  
  15.  
  16. const Review = mongoose.model("Review", reviewSchema);
  17. module.exports = Review;
  18.  
  19. const mongoose = require("mongoose");
  20. const Schema = mongoose.Schema;
  21.  
  22. const companySchema = new Schema ({
  23.  
  24. companyName: String,
  25. about: String,
  26. basedIn: String,
  27. materialOrigins: [String],
  28. productRange: [String],
  29. category: String,
  30. reviews: [ {type: Schema.Types.ObjectId, ref: 'Review'} ],
  31. socialRating: Number,
  32. environmentalRating: Number,
  33. priceRange: Number
  34.  
  35. });
  36.  
  37.  
  38. const Company = mongoose.model("Company", companySchema);
  39. module.exports = Company;
  40.  
  41. const mongoose = require("mongoose");
  42. const Schema = mongoose.Schema;
  43.  
  44. const userSchema = new Schema ({
  45. email: String,
  46. firstName: String,
  47. lastName: String,
  48. password: String,
  49. image: Object,
  50. aboutText: String,
  51. reviews: [ { type: Schema.Types.ObjectId, ref: "Review" } ]
  52. // comments: { type: Schema.Types.ObjectId, ref: 'Comment' }
  53.  
  54. });
  55.  
  56. const User = mongoose.model("User", userSchema);
  57. module.exports = User;
  58.  
  59. router.post('/:category/:company', (req, res) => {
  60.  
  61. var subject = req.body.subject;
  62. var commentBody = req.body.commentBody;
  63. var starRating = req.body.starRating;
  64. var userId = req.body.userId;
  65.  
  66. if(!subject || !commentBody || !starRating) {
  67. res.status(400).json({ message: "Subject, comment body, and star rating are required." });
  68. return;
  69. }
  70.  
  71. var newReview = Review({
  72. starRating,
  73. subject,
  74. commentBody,
  75. userId
  76. });
  77.  
  78.  
  79. User.findById(userId, {
  80. }, (err, user) => {
  81. if (err) {
  82. return res.send(err);
  83. } else {
  84.  
  85. console.log("checking out user in route", user);
  86. user.reviews.push(newReview);
  87. user.save();
  88.  
  89. newReview.save((err, review) => {
  90. if (err) {
  91. return res.status(400).json({ message: err });
  92. } else {
  93. res.status(200).json({ message: 'Review saved', review });
  94. }
  95. });
  96.  
  97. }
  98. });
Add Comment
Please, Sign In to add comment