{"size": 3239, "ext": "js", "max_stars_repo_path": "backend/controllers/venueController.js", "max_stars_repo_name": "NasibNia/Tempo", "max_stars_repo_stars_event_min_datetime": "2018-10-05T04:44:40.000Z", "max_stars_repo_stars_event_max_datetime": "2018-10-16T06:48:13.000Z", "max_issues_repo_path": "backend/controllers/venueController.js", "max_issues_repo_name": "NasibNia/Tempo", "max_issues_repo_issues_event_min_datetime": "2018-10-08T21:37:55.000Z", "max_issues_repo_issues_event_max_datetime": "2018-12-02T23:05:27.000Z", "max_forks_repo_path": "backend/controllers/venueController.js", "max_forks_repo_name": "NasibNia/Tempo", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 2, "max_issues_count": 5, "max_forks_count": null, "avg_line_length": 26.1209677419, "max_line_length": 76, "alphanum_fraction": 0.3834516826}
const db = require("../models");

module.exports = {
    
    // tested and working
    findAllVenues : function(req,res){
        db.Venue.findAll({

        include : [{
                model : db.Show
                // ({
                //     include : [{
                //         model : db.Show
                //     }]
                // })
            }],  
            order : [['updatedAt', 'DESC']]
        })
        .then(venuesRes =>{
            res.json(venuesRes);
        });
    },

    // tested and working
    create: function(req,res){
        db.Venue.create(req.body)
            .then(venuesRes => {
                res.json(venuesRes);
            });
    },

    // tested and working
    findVenue : function (req, res){
        db.Venue.findOne({
            where : {
                id : req.params.id
            },
            include : [db.Show]
            })
            .then(venuesRes => {
                res.json(venuesRes);
            });
    },

    findReadyVenues : function (req, res){
        db.Venue.findAll({
            where : {
                readyToBook : true
            },
            include : [db.Show]
            })
            .then(venuesRes => {
                res.json(venuesRes);
            });
    },

    findVenueByName: function (req, res) {
        db.Venue.findOne({
            where: {
                name: req.params.name
            },
            include: { model: db.Show }
        })
            .then(venuesRes => {
                res.json(venuesRes);
            });
    },

    // tested and working
    deleteVenue : function(req,res){
        db.Venue.destroy({
            where : {
                id : req.params.id
            }
        })
        .then(venuesRes => {
            res.json(venuesRes);
        });
    },
    
    updateVenue : function(req,res){
        db.Venue.update(
            req.body ,
            {where : {
                id:req.params.id
            }
        })
        .then(dbVenues => res.json(dbVenues));
    },

    // Gets current rating and adds new rating to it, adds 1 to totalRated
    // addRating : function(req, res) {
    //     console.log("adding rating to venue")
    //     db.Venue.findOne({
    //         where : {
    //             id : req.params.id
    //         }})
    //         .then(venueRes => {
    //             db.Venue.update(
    //                 {
    //                     rating : venueRes + req.rating,
    //                     totalRated : venueRes.totalRated + 1
    //                 },
    //                 {where : {
    //                     id:req.params.id
    //                 }
    //             })
    //             .then(updateRes => res.json(updateRes));   
    //             res.json(updateRes);
    //         });
    // },

    // // Gets user info, calculates rating, and responds with obj of rating
    // getRating : function(req,res){
    //     db.Venue.findOne({
    //         where : {
    //             id : req.params.id
    //         }})
    //         .then(venueRes => {
    //             const rating = venueRes.ratings / venueRes.totalRated;
    //             res.json({rating : rating});
    //         });
    // }
};