const  router = require('express').Router();
let tutorRatingsRoute = require('../models/TutorRatingAndReviews.model');

const mongoose = require("mongoose");

router.route('/add').post(async(req,res) =>{


    const tutor_id = mongoose.Types.ObjectId(req.body.tutor_id);
    const student_id = mongoose.Types.ObjectId(req.body.student_id);
    const rating = req.body.rating;
    const review = req.body.review;
    const version = req.body.version;

        const newTutorRating = new tutorRatingsRoute({
            tutor_id,
            student_id,
            rating,
            review,
            version,

        });
    newTutorRating.save()
            .then( () => {
                res.json('New Review Added');
            })
            .catch(err =>res.status(400).json('Error: '+err));


});


router.route('/:id').get(function (req, res) {
    let id = req.params.id;
    //console.log("TutorId : " + id)
    tutorRatingsRoute.find({"tutor_id": id}).populate('student_id').then(tutorReview => {
        ///console.log(tutor);
        res.json(tutorReview);
    }).catch(err => res.status(400).json('Eroor: '+ err));
});

module.exports = router;
