Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Completed Points Update API #28

Merged
merged 1 commit into from
Oct 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions carpool-backend/api/controllers/RideDetailController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* RideDetailController
*
* @description :: Server-side logic for managing Ridedetails
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/

module.exports = {

};

58 changes: 56 additions & 2 deletions carpool-backend/api/controllers/UserCarpoolController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
});
}

};
44 changes: 44 additions & 0 deletions carpool-backend/api/models/RideDetail.js
Original file line number Diff line number Diff line change
@@ -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
}
});
}
};
80 changes: 42 additions & 38 deletions carpool-backend/api/models/UserCarpool.js
Original file line number Diff line number Diff line change
@@ -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
}
});
}
};
};
4 changes: 0 additions & 4 deletions carpool-backend/api/models/UserLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions carpool-backend/config/policies.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ module.exports.policies = {
'get': ['authBearer']
},

UserCarpoolController: {
'put': ['authBearer']
}

};
3 changes: 2 additions & 1 deletion carpool-backend/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'

/***************************************************************************
* *
Expand Down