/**
 * Created by rcordoba on 11/05/2017.
 */

var mongoose = require('mongoose');
var season = mongoose.model('Season');

//GET - Devuelve todas las temporadas
exports.findAll = function(req, res) {
  season.find(function(err, _seasons) {
     if(err) res.send(500, err.message);
     console.log('GET /seasons');
     res.status(200).jsonp(_seasons);
  });
};

//GET - Devuelve una temporada por ID
exports.findById = function(req, res) {
  season.find({seasonId: req.params.id}, function(err, _season) {
     if(err) res.send(500, err.message);
     console.log('GET /seasons/' + req.params.id);
     res.status(200).jsonp(_season);
  });
};

//POST - Inserta una nueva temporada
exports.addSeason = function(req, res) {
  console.log('POST');
  console.log(req.body);

  var season = new Season({
      seasonId: req.body.seasonId,
      seasonName: req.body.seasonName,
      startDate: req.body.startDate,
      finishDate: req.body.finishDate
  });

  season.save(function(err, _season) {
     if(err) return res.status(500).send(err.message);
     res.status(200).jsonp(_season);
  });
};

//PUT - Actualiza una temporada
exports.updateSeason = function(req, res) {
  season.findById(req.params.id, function(err, _season) {
      _season.seasonId = req.body.seasonId;
      _season.seasonName = req.body.seasonName;
      _season.startDate = req.body.startDate;
      _season.finishDate = req.body.finishDate;

      _season.save(function(err) {
         if(err) return res.status(500).send(err.message);
         res.status(200).jsonp(_season);
      });
  });
};

//DELETE - Borra una temporada
exports.deleteSeason = function(req, res) {
  season.find({seasonId: req.params.id}, function(err, _season) {
     _season.remove(function(err) {
         if(err) return res.status(500).send(err.message);
         res.status(200).send();
     })
  });
};