var utils = require('../config/utils');

module.exports = {
  getNearbyEvents: function(){
    /* Select all events from events table where distance
       between user and events < some number. */
  },

  createEvent: function(req, res){
    var models = req.app.get('models');
    var Event = models.Event;

    /* The newEvent object contains a number of properties -- some obtained
       from the body of the request and some defaults. This object is used
       to build the Sequelize model that will be saved in the database. */
    var newEvent = {};
    newEvent.HostId = req.body.HostId;
    newEvent.locationId = req.body.locationId;
    newEvent.plannedTime = req.body.plannedTime;
    newEvent.capacity = req.body.capacity;
    newEvent.currentSize = req.body.currentSize;
    newEvent.currentActivity = req.body.currentActivity;
    newEvent.completedStatus = false;

    /* Builds a Sequelize model based on newEvent object and saves to the database. 
       Sends a response if this is successful. Otherwise, logs an error. */
    Event.sync().then(function () {
      return Event.create(newEvent);
    }).then (function (newEvent) {
      utils.sendResponse(res, 201, newEvent);
    }).catch(function (err) {
      console.log('Error: ', err);
    });
  },

  getEvent: function(req, res){
    var models = req.app.get('models');
    var Event = models.Event;
    var eventId = req.params.eventId;

    Event.sync().then(function () {
      return Event.findById(eventId);
    }).then(function (event) {
      utils.sendResponse(res, 200, event);
    }).catch(function (err) {
      console.log('Error: ', err);
    });
  },

  updateEvent: function (req, res) {
    var models = req.app.get('models');
    var Event = models.Event;
    var eventId = req.params.eventId;

    var updatedEvent = {};
    updatedEvent.HostId = req.body.HostId;
    updatedEvent.locationId = req.body.locationId;
    updatedEvent.plannedTime = req.body.plannedTime;
    updatedEvent.capacity = req.body.capacity;
    updatedEvent.currentSize = req.body.currentSize;
    updatedEvent.currentActivity = req.body.currentActivity;
    updatedEvent.completedStatus = req.body.completedStatus;

    var options = {};
    options.where = { id: eventId };
    options.returning = true;

    Event.sync().then(function () {
      return Event.update(updatedEvent, options);
    }).then(function (update) {
      /* Sequelize update method returns an array. Second element is an 
         array contained updated records. We want the first updated record 
         since there should be only one. See documentation of .update() at: 
         http://docs.sequelizejs.com/en/latest/api/model/#updatevalues-options-promisearrayaffectedcount-affectedrows */
      updatedEvent = update[1][0];
      utils.sendResponse(res, 200, updatedEvent);
    }).catch(function (err) {
      console.log('Error: ', err);
    });
  },

  getHistory: function(req, res){
    var models = req.app.get('models');
    var Event = models.Event;
    var userId = req.params.userId;

    Event.sync().then(function () {
      return Event.findAll({ where: { HostId : userId }});
    }).then(function (events){
      utils.sendResponse(res, 200, events);
    }).catch(function (err){
      console.log('Error: ', err);
    });
  },

};
@@ -1,559 +1,29 @@
-var utils = require('../config/utils');%0A%0Amodule.exports = %7B%0A  getNearbyEvents: function()%7B%0A    /* Select all events from events table where distance%0A       between user and events %3C some number. */%0A  %7D,%0A%0A  createEvent: function(req, res)%7B%0A    var models = req.app.get('models');%0A    var Event = models.Event;%0A%0A    /* The newEvent object contains a number of properties -- some obtained%0A       from the body of the request and some defaults. This object is used%0A       to build the Sequelize model that will be saved in the database. */%0A    
+// events.js%0A
 var 
-new
 Event = 
 %7B%7D;%0A
@@ -22,1760 +22,141 @@
 t = 
-%7B%7D;%0A    newEvent.HostId = req.body.HostId;%0A    newEvent.locationId = req.body.locationId;%0A    newEvent.plannedTime = req.body.plannedTime;%0A    newEvent.capacity = req.body.capacity;%0A    newEvent.currentSize = req.body.currentSize;%0A    newEvent.currentActivity = req.body.currentActivity;%0A    newEvent.completedStatus = false;%0A%0A    /* Builds a Sequelize model based on newEvent object and saves to the database. %0A       Sends a response if this is successful. Otherwise, logs an error. */%0A    Event.sync().then(function () %7B%0A      return Event.create(newEvent);%0A    %7D).then (function (newEvent) %7B%0A      utils.sendResponse(res, 201, newEvent);%0A    %7D).catch(function (err) %7B%0A      console.log('Error: ', err);%0A    %7D);%0A  %7D,%0A%0A  getEvent: function(req, res)%7B%0A    var models = req.app.get('models');%0A    var Event = models.Event;%0A    var eventId = req.params.eventId;%0A%0A    Event.sync().then(function () %7B%0A      return Event.findById(eventId);%0A    %7D).then(function (event) %7B%0A      utils.sendResponse(res, 200, event);%0A    %7D).catch(function (err) %7B%0A      console.log('Error: ', err);%0A    %7D);%0A  %7D,%0A%0A  updateEvent: function (req, res) %7B%0A    var models = req.app.get('models');%0A    var Event = models.Event;%0A    var eventId = req.params.eventId;%0A%0A    var updatedEvent = %7B%7D;%0A    updatedEvent.HostId = req.body.HostId;%0A    updatedEvent.locationId = req.body.locationId;%0A    upd
+require('../models/events');%0A%0Amodule.exports = %7B%0A  getNearbyEvents: function()%7B%0A    console.log(Event);%0A  %7D,%0A%0A  cre
 ate
-d
 Event
-.plannedTime = req.body.plannedTime;%0A    updatedEvent.capacity = req.body.capacity;%0A    updatedEvent.currentSize = req.body.currentSize;%0A    updatedEvent.currentActivity = req.body.currentActivity;%0A    updatedEvent.completedStatus = req.body.completedStatus;%0A%0A    var options = %7B%7D;%0A    options.where = %7B id: eventId %7D;%0A    options.returning = true;%0A%0A    Event.sync().then(
+: 
 function
  () 
@@ -155,994 +155,81 @@
 tion
- 
 ()
- 
 %7B%0A
-      return Event.update(updatedEvent, options);%0A    %7D).then(function (update) %7B%0A      /* Sequelize update method returns an array. Second element is an %0A         array contained updated records. We want the first updated record %0A         since there should be only one. See documentation of .update() at: %0A         http://docs.sequelizejs.com/en/latest/api/model/#updatevalues-options-promisearrayaffectedcount-affectedrows */%0A      updatedEvent = update%5B1%5D%5B0%5D;%0A      utils.sendResponse(res, 200, updatedEvent);%0A    %7D).catch(function (err) %7B%0A      console.log('Error: ', err);%0A    %7D);%0A  %7D,%0A%0A  getHistory: function(req, res)%7B%0A    var models = req.app.get('models');%0A    var Event = models.Event;%0A    var userId = req.params.userId;%0A%0A    Event.sync().then(function () %7B%0A      return Event.findAll(%7B where: %7B HostId : userId %7D%7D);%0A    %7D).then(function (events)%7B%0A      utils.sendResponse(res, 200, events);%0A    %7D).catch(function (err)%7B%0A      console.log('Error: ', err);%0A    %7D);%0A  %7D,%0A%0A%7D;
+%0A  %7D,%0A%0A  getEvent: function()%7B%0A%0A  %7D,%0A%0A  updateEvent: function()%7B%0A%0A  %7D%0A%7D;%0A

Update events.js