Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const mongoose = require("mongoose");
- const Schema = mongoose.Schema;
- const reviewSchema = new Schema ({
- companyName: String,
- companyId: { type: Schema.Types.ObjectId, ref: 'Company'},
- starRating: Number,
- subject: String,
- commentBody: String,
- createdBy: { type: Schema.Types.ObjectId, ref: 'User'},
- });
- const Review = mongoose.model("Review", reviewSchema);
- module.exports = Review;
- const mongoose = require("mongoose");
- const Schema = mongoose.Schema;
- const companySchema = new Schema ({
- companyName: String,
- about: String,
- basedIn: String,
- materialOrigins: [String],
- productRange: [String],
- category: String,
- reviews: [ {type: Schema.Types.ObjectId, ref: 'Review'} ],
- socialRating: Number,
- environmentalRating: Number,
- priceRange: Number
- });
- const Company = mongoose.model("Company", companySchema);
- module.exports = Company;
- const mongoose = require("mongoose");
- const Schema = mongoose.Schema;
- const userSchema = new Schema ({
- email: String,
- firstName: String,
- lastName: String,
- password: String,
- image: Object,
- aboutText: String,
- reviews: [ { type: Schema.Types.ObjectId, ref: "Review" } ]
- // comments: { type: Schema.Types.ObjectId, ref: 'Comment' }
- });
- const User = mongoose.model("User", userSchema);
- module.exports = User;
- router.post('/:category/:company', (req, res) => {
- var subject = req.body.subject;
- var commentBody = req.body.commentBody;
- var starRating = req.body.starRating;
- var userId = req.body.userId;
- if(!subject || !commentBody || !starRating) {
- res.status(400).json({ message: "Subject, comment body, and star rating are required." });
- return;
- }
- var newReview = Review({
- starRating,
- subject,
- commentBody,
- userId
- });
- User.findById(userId, {
- }, (err, user) => {
- if (err) {
- return res.send(err);
- } else {
- console.log("checking out user in route", user);
- user.reviews.push(newReview);
- user.save();
- newReview.save((err, review) => {
- if (err) {
- return res.status(400).json({ message: err });
- } else {
- res.status(200).json({ message: 'Review saved', review });
- }
- });
- }
- });
Add Comment
Please, Sign In to add comment