{"size": 1481, "ext": "js", "max_stars_repo_path": "controllers/rentController.js", "max_stars_repo_name": "jessicaRmaria/alugaaquigit_backend", "max_stars_repo_stars_event_min_datetime": "2021-08-24T11:23:07.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-24T11:23:07.000Z", "max_issues_repo_path": "controllers/rentController.js", "max_issues_repo_name": "carolinambf/AlugaAqui-Backend", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/rentController.js", "max_forks_repo_name": "carolinambf/AlugaAqui-Backend", "max_forks_repo_forks_event_min_datetime": "2020-09-18T11:17:14.000Z", "max_forks_repo_forks_event_max_datetime": "2020-09-18T11:17:14.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 35.2619047619, "max_line_length": 141, "alphanum_fraction": 0.5266711681}
const Rent = require('../models/rent');
const prices = require('../prices');
const { verifyToken } = require('../auth/user');

exports.all = function(req, res) {                                   
    verifyToken(req, res, function(user) {
        Rent.find({user_id: user._id}, function(err, rents) {      // cada utilizador tem acesso aos seus proprios alugueres e não dos outros
            res.json(rents);
        });
    });
};

exports.get = function(req, res) {                               
    verifyToken(req, res, function(user) {
        Rent.findById(req.params.id, function(err, rent) {
            res.json(rent);
        });
    });
};

exports.store = function (req, res) {                                      //dados do filme alugado e do utilizador 
    verifyToken(req, res, function(user) {
        let rent = new Rent();
        rent.movie_id = req.body.movie_id;
        rent.movie_name = req.body.movie_name;
        rent.price = prices[rent.movie_id] ? prices[rent.movie_id] : 5.99; // preço por defeito
        rent.user_id = user._id;
        rent.save();
        res.json(rent);
    });
};

exports.return = function (req, res) {
    verifyToken(req, res, function(user) {                               
        Rent.findById(req.params.id, function(err, rent) {                // devolução do filme, guarda a data de devolução
            rent.date_returned = new Date();
            rent.save();
            res.json(rent); 
        });
    });
};
