diff --git a/carpool-backend/api/controllers/RideDetailController.js b/carpool-backend/api/controllers/RideDetailController.js new file mode 100644 index 0000000..e9ebc20 --- /dev/null +++ b/carpool-backend/api/controllers/RideDetailController.js @@ -0,0 +1,11 @@ +/** + * RideDetailController + * + * @description :: Server-side logic for managing Ridedetails + * @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers + */ + +module.exports = { + +}; + diff --git a/carpool-backend/api/controllers/UserCarpoolController.js b/carpool-backend/api/controllers/UserCarpoolController.js index 8d1f4fe..fd68d42 100644 --- a/carpool-backend/api/controllers/UserCarpoolController.js +++ b/carpool-backend/api/controllers/UserCarpoolController.js @@ -6,6 +6,60 @@ */ module.exports = { - -}; + put: function(req, res) { + var riderId = req.body.riderId; + var rideeId = req.body.rideeId; + var point = req.body.point; + var self = {}; + function createRideDetails(callback) { + return sequelize.transaction(function(t) { + return RideDetail.create({ + riderId: riderId, + rideeId: rideeId, + isComplete: false + }, { transaction: t }).then(function(riderDetails) { + self.riderDetails = riderDetails; + return UserPoint.create({ + point: point, + userId: riderId + }, { transaction: t }).then(function(riderUpdateDetails) { + self.riderUpdateDetails = riderUpdateDetails; + UserPoint.create({ + userId: rideeId, + point: -10 + }).then(function(rideeUpdateDetails) { + self.rideeUpdateDetails = rideeUpdateDetails; + }); + }); + }); + }).then(function(result) { + // Transaction has been committed + // result is whatever the result of the promise chain returned to the transaction callback + return callback(null); + }).catch(function(err) { + // Transaction has been rolled back + // err is whatever rejected the promise chain returned to the transaction callback + return callback("Could Not create the ride for the user"); + }); + } + + function responseCreation(callback) { + var response = {}; + response.riderDetails = self.riderDetails; + response.riderUpdateDetails = self.riderUpdateDetails; + response.rideeUpdateDetails = self.rideeUpdateDetails; + return res.ok(response); + } + + async.waterfall([ + createRideDetails, + responseCreation + ], function(err) { + if (err) { + return res.badRequest({ exception: err }); + } + }); + } + +}; \ No newline at end of file diff --git a/carpool-backend/api/models/RideDetail.js b/carpool-backend/api/models/RideDetail.js new file mode 100644 index 0000000..085f55c --- /dev/null +++ b/carpool-backend/api/models/RideDetail.js @@ -0,0 +1,44 @@ +/** + * RideDetail.js + * + * @description :: TODO: You might write a short summary of how this model works and what it represents here. + * @docs :: http://sailsjs.org/#!documentation/models + */ + +module.exports = { + + attributes: { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + createdAt: { + type: Sequelize.DATE, + defaultValue: Sequelize.NOW + }, + updatedAt: { + type: Sequelize.DATE, + defaultValue: Sequelize.NOW + }, + isComplete: { + type: Sequelize.BOOLEAN, + defaultValue: false, + }, + }, + associations: function() { + RideDetail.belongsTo(User, { + foreignKey: { + name: 'riderId', + allowNull: false + } + }); + + RideDetail.belongsTo(User, { + foreignKey: { + name: 'rideeId', + allowNull: false + } + }); + } +}; \ No newline at end of file diff --git a/carpool-backend/api/models/UserCarpool.js b/carpool-backend/api/models/UserCarpool.js index 4ab87e4..4d01bf2 100644 --- a/carpool-backend/api/models/UserCarpool.js +++ b/carpool-backend/api/models/UserCarpool.js @@ -1,44 +1,48 @@ /** -* UserCarpool.js -* -* @description :: TODO: You might write a short summary of how this model works and what it represents here. -* @docs :: http://sailsjs.org/#!documentation/models -*/ + * UserCarpool.js + * + * @description :: TODO: You might write a short summary of how this model works and what it represents here. + * @docs :: http://sailsjs.org/#!documentation/models + */ module.exports = { - attributes: { - id: { - type: Sequelize.INTEGER, - primaryKey: true, - autoIncrement: true + attributes: { + id: { + type: Sequelize.INTEGER, + primaryKey: true, + autoIncrement: true + }, + createdAt: { + type: Sequelize.DATE, + defaultValue: Sequelize.NOW + }, + updatedAt: { + type: Sequelize.DATE, + defaultValue: Sequelize.NOW + }, + startTime: { + type: Sequelize.DATE, + defaultValue: Sequelize.NOW + }, + endTime: { + type: Sequelize.DATE, + defaultValue: Sequelize.NOW + }, + distance: { + type: Sequelize.INTEGER, + }, + isComplete: { + type: Sequelize.BOOLEAN, + defaultValue: false, + }, }, - createdAt: { - type: Sequelize.DATE, - defaultValue: Sequelize.NOW - }, - updatedAt: { - type: Sequelize.DATE, - defaultValue: Sequelize.NOW - }, - startTime: { - type: Sequelize.DATE, - defaultValue: Sequelize.NOW - }, - endTime: { - type: Sequelize.DATE, - defaultValue: Sequelize.NOW - }, - distance:{ - type: Sequelize.INTEGER, + associations: function() { + UserCarpool.belongsTo(User, { + foreignKey: { + name: 'userId', + allowNull: false + } + }); } - }, - associations: function() { - UserCarpool.belongsTo(User, { - foreignKey: { - name: 'userId', - allowNull: false - } - }); - } -}; +}; \ No newline at end of file diff --git a/carpool-backend/api/models/UserLocation.js b/carpool-backend/api/models/UserLocation.js index a7a525e..d4b95f2 100644 --- a/carpool-backend/api/models/UserLocation.js +++ b/carpool-backend/api/models/UserLocation.js @@ -21,10 +21,6 @@ module.exports = { type: Sequelize.DECIMAL(11, 8), require: true }, - isComplete:{ - type:Sequelize.BOOLEAN, - defaultValue:false, - }, createdAt: { type: Sequelize.DATE, defaultValue: Sequelize.NOW diff --git a/carpool-backend/config/policies.js b/carpool-backend/config/policies.js index ec99028..1d61779 100644 --- a/carpool-backend/config/policies.js +++ b/carpool-backend/config/policies.js @@ -54,4 +54,8 @@ module.exports.policies = { 'get': ['authBearer'] }, + UserCarpoolController: { + 'put': ['authBearer'] + } + }; \ No newline at end of file diff --git a/carpool-backend/config/routes.js b/carpool-backend/config/routes.js index 44b8e08..e49e81c 100644 --- a/carpool-backend/config/routes.js +++ b/carpool-backend/config/routes.js @@ -39,7 +39,8 @@ module.exports.routes = { 'get /schools': 'SchoolController.find', 'post /login': 'UserController.login', 'post /signup': 'UserController.signup', - 'get /allcarpools?': 'UserLocationController.get' + 'get /allcarpools?': 'UserLocationController.get', + 'put /acceptride': 'UserCarpoolController.put' /*************************************************************************** * *