Skip to content

Commit

Permalink
#13 - Added endpoint to update ride
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascvas committed Sep 2, 2018
1 parent 7156fb4 commit 9756daf
Show file tree
Hide file tree
Showing 19 changed files with 502 additions and 348 deletions.
14 changes: 7 additions & 7 deletions backend/src/main/database/DatabaseManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ class DatabaseManager {
return new Promise((resolve, reject) => {
connection.query(queryString, (error, results, fields) => {
if (closeConnection) {
return this.closeConnection(connection)
.finally(() => {
if (error) {
return reject(error);
}
resolve(results);
});
let closePromise = this.closeConnection(connection);
return closePromise.finally(() => {
if (error) {
return reject(error);
}
resolve(results);
});
}
if (error) {
console.log("Error executing", queryString, error);
Expand Down
32 changes: 6 additions & 26 deletions backend/src/main/rides/CreateRideService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const jsonValidator = require('jsonschema');
const rideSchema = require('../schema/ride.json');
const RideStatus = require('./RideStatus');
const RideRepository = require('./RideRepository');
const Coordinates = require('./Coordinates');
const RideMapper = require('./RideMapper');

class CreateRideService {

Expand All @@ -25,34 +25,14 @@ class CreateRideService {

let validationError = this._validate(rideObject);
if (validationError) {
return reject(validationError);
return Promise.reject(validationError);
}

let facilitatorEmail = loginData && loginData.email;
let payload = this._parseBody(rideObject, facilitatorEmail);
return this._rideRepository.create(payload, connection);
}
const payload = RideMapper.dtoToEntity(rideObject, loginData && loginData.email);
payload.status = RideStatus.OPEN;
payload.deleted = '0';

_parseBody(data, facilitatorEmail) {
return {
client: `${data.client}`,
facilitatorEmail: facilitatorEmail,
pickupTimeAndDateInUTC: new Date(data.pickupTimeAndDateInUTC),
locationFrom: new Coordinates(data.locationFrom.latitude, data.locationFrom.longitude),
locationTo: new Coordinates(data.locationTo.latitude, data.locationTo.longitude),
fbLink: data.fbLink,
driverGender: data.driverGender,
carType: data.carType,
status: RideStatus.OPEN,
deleted: `0`,
suburbFrom: data.locationFrom.suburb,
placeNameFrom: data.locationFrom.placeName,
postCodeFrom: data.locationFrom.postcode,
suburbTo: data.locationTo.suburb,
placeNameTo: data.locationTo.placeName,
postCodeTo: data.locationTo.postcode,
description: data.description
};
return this._rideRepository.create(payload, connection);
}

_validate(data) {
Expand Down
9 changes: 5 additions & 4 deletions backend/src/main/rides/FindOneRideService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const RideRepository = require('./RideRepository');
const RidesMapper = require('./RideMapper');

class FindOneRideService {

Expand All @@ -10,7 +11,7 @@ class FindOneRideService {
}

findOne(id, loginData) {
if(loginData.role === 'driver'){
if (loginData.role === 'driver') {
return Promise.resolve(null);
}

Expand All @@ -21,14 +22,14 @@ class FindOneRideService {

_findOne(id, loginData, connection) {
let jsonQuery = this._createQuery(id, loginData);
return this._rideRepository.list(jsonQuery, connection)
.then(items => items[0] || null);
return this._rideRepository.findOne(jsonQuery, connection)
.then(item => RidesMapper.entityToDto(item));
}

_createQuery(id, loginData) {
return {
id: id,
facilitatorEmail: loginData.role === 'facilitator' ? loginData.email : undefined,
facilitatorId: loginData.role === 'facilitator' ? loginData.email : undefined,
includePickupTimeInPast: true
};
}
Expand Down
4 changes: 3 additions & 1 deletion backend/src/main/rides/ListRidesService.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const RideRepository = require('./RideRepository');
const RidesMapper = require('./RideMapper');

class ListRidesService {

Expand All @@ -13,6 +14,7 @@ class ListRidesService {
const connection = this._databaseManager.createConnection();

return this._listRides(queryParams, loginData, connection)
.then(rides => rides.map(RidesMapper.entityToDto))
.finally(() => this._databaseManager.closeConnection(connection));
}

Expand All @@ -29,7 +31,7 @@ class ListRidesService {
fromLatitude: queryParams.fromLatitude,
driverGenders: loginData.role === 'driver' ? ['any', loginData.driverGender] : undefined,
includePickupTimeInPast: loginData.role !== 'driver',
facilitatorEmail: loginData.role === 'facilitator' ? loginData.email : undefined,
facilitatorId: loginData.role === 'facilitator' ? loginData.email : undefined,
};
}
}
Expand Down
66 changes: 66 additions & 0 deletions backend/src/main/rides/RideMapper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
class RideMapper {
static entityToDto(ride) {
if(!ride){
return null;
}
return {
"client": ride.client,
"pickupTimeAndDateInUTC": new Date(ride.pickupTimeAndDateInUTC),
"locationFrom": {
"latitude": ride.locationFrom.x,
"longitude": ride.locationFrom.y,
"suburb": ride.suburbFrom,
"postcode": ride.postCodeFrom,
"placeName": ride.placeNameFrom
},
"locationTo": {
"latitude": ride.locationTo.x,
"longitude": ride.locationTo.y,
"suburb": ride.suburbTo,
"postcode": ride.postCodeTo,
"placeName": ride.placeNameTo
},
"fbLink": ride.fbLink,
"driverGender": ride.driverGender,
"carType": ride.carType,
"status": ride.status,
"deleted": parseInt(ride.deleted + ''),
"facilitatorId": ride.facilitatorId || ride.facilitatorEmail,
"description": ride.description,
"id": ride.id
}
}

static dtoToEntity(ride, facilitatorId) {
if(!ride){
return null;
}
return {
client: `${ride.client}`,
pickupTimeAndDateInUTC: new Date(ride.pickupTimeAndDateInUTC),
locationFrom: {
latitude: ride.locationFrom.latitude,
longitude: ride.locationFrom.longitude,
suburb: ride.locationFrom.suburb,
placeName: ride.locationFrom.placeName,
postcode: ride.locationFrom.postcode,
},
locationTo: {
latitude: ride.locationTo.latitude,
longitude: ride.locationTo.longitude,
suburb: ride.locationTo.suburb,
placeName: ride.locationTo.placeName,
postcode: ride.locationTo.postcode,
},
fbLink: ride.fbLink,
driverGender: ride.driverGender,
carType: ride.carType,
status: ride.status,
deleted: 0,
facilitatorId: facilitatorId,
description: ride.description
}
}
}

module.exports = RideMapper;
101 changes: 61 additions & 40 deletions backend/src/main/rides/RideRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,6 @@ class RideRepository {
this._databaseManager = databaseManager;
}

/**
* @param ride:
* client {string}
* facilitatorEmail {string}
* pickupTimeAndDateInUTC {Date}
* locationFrom {Coordinates}
* locationTo {Coordinates}
* fbLink {string}
* driverGender {string}
* carType {string}
* status {RideStatus}
* deleted {number - 0/1}
* suburbFrom {string}
* placeNameFrom {string}
* postCodeFrom {string}
* suburbTo {string}
* placeNameTo {string}
* postCodeTo {string}
* description
* @param connection
* @returns {*}
*/
create(ride, connection) {
const escape = (data) => connection.escape(data);
const locationFrom = `POINT(${ride.locationFrom.latitude}, ${ride.locationFrom.longitude})`;
Expand All @@ -52,65 +30,108 @@ class RideRepository {
description)
VALUES
(${
[ escape(ride.client),
escape(ride.facilitatorEmail),
[escape(ride.client),
escape(ride.facilitatorId),
escape(moment(ride.pickupTimeAndDateInUTC).format('YYYY-MM-DD HH:mm:ss')),
locationFrom,
locationTo,
escape(ride.fbLink),
escape(ride.driverGender),
escape(ride.carType),
escape(ride.status.statusName),
escape(ride.status),
escape(ride.deleted),
escape(ride.suburbFrom),
escape(ride.placeNameFrom),
escape(ride.postCodeFrom),
escape(ride.suburbTo),
escape(ride.placeNameTo),
escape(ride.postCodeTo),
escape(ride.locationFrom.suburb),
escape(ride.locationFrom.placeName),
escape(ride.locationFrom.postcode),
escape(ride.locationTo.suburb),
escape(ride.locationTo.placeName),
escape(ride.locationTo.postcode),
escape(ride.description)
].join(',')})`;
console.log(query);

return this._databaseManager.query(query, connection);
}

update(id, ride, connection) {
if (!id) {
throw new Error('No id specified when updating ride.');
}
const escape = (data) => connection.escape(data);
const locationFrom = `POINT(${ride.locationFrom.latitude}, ${ride.locationFrom.longitude})`;
const locationTo = `POINT(${ride.locationTo.latitude}, ${ride.locationTo.longitude})`;
let query = `UPDATE rides SET client = ${escape(ride.client)},
facilitatorEmail = ${escape(ride.facilitatorId)},
pickupTimeAndDateInUTC = ${escape(moment(ride.pickupTimeAndDateInUTC).format('YYYY-MM-DD HH:mm:ss'))},
locationFrom = ${locationFrom},
locationTo = ${locationTo},
fbLink = ${escape(ride.fbLink)},
driverGender = ${escape(ride.driverGender)},
carType = ${escape(ride.carType)},
status = ${escape(ride.status)},
deleted = ${ride.deleted},
suburbFrom = ${escape(ride.locationFrom.suburb)},
placeNameFrom = ${escape(ride.locationFrom.placeName)},
postCodeFrom = ${escape(ride.locationFrom.postcode)},
suburbTo = ${escape(ride.locationTo.suburb)},
placeNameTo = ${escape(ride.locationTo.placeName)},
postCodeTo = ${escape(ride.locationTo.postcode)},
description = ${escape(ride.description)}
WHERE
id = ${id}`;
console.log(query);

return this._databaseManager.query(query, connection);
}

findOne(jsonQuery, connection) {
return this.list(jsonQuery, connection)
.then(results => results[0] || null);
}

/**
* @param jsonQuery:
* toLongitude {number}
* toLatitude {number}
* fromLongitude {number}
* fromLatitude {number}
* driverGenders {string[]}
* facilitatorEmail {string}
* facilitatorId {string}
* includePickupTimeInPast {boolean}
* @param connection
*/
list(jsonQuery, connection) {
const escape = (data) => connection.escape(data);
let where = [];
if(jsonQuery.toLongitude && jsonQuery.toLatitude && jsonQuery.fromLongitude && jsonQuery.fromLatitude) {
if (jsonQuery.toLongitude && jsonQuery.toLatitude && jsonQuery.fromLongitude && jsonQuery.fromLatitude) {
where.push(`ST_Contains(ST_Envelope(ST_GeomFromText('LINESTRING(${jsonQuery.toLongitude} ${jsonQuery.toLatitude}, ${jsonQuery.fromLongitude} ${jsonQuery.fromLatitude})')), locationFrom)`);
}
if(!jsonQuery.includePickupTimeInPast){
if (!jsonQuery.includePickupTimeInPast) {
where.push('pickupTimeAndDateInUTC >= NOW()')
}
if(jsonQuery.id){
if (jsonQuery.id) {
where.push(`id = ${jsonQuery.id}`)
}
if(jsonQuery.driverGenders && jsonQuery.driverGenders.length){
if (jsonQuery.driverGenders && jsonQuery.driverGenders.length) {
let genders = jsonQuery.driverGenders.map(g => ` driverGender = '${g}'`).join(' or ');
where.push(genders.length === 1 ? genders : `(${genders})`)
}
if(jsonQuery.facilitatorEmail){
where.push(`facilitatorEmail = ${escape(jsonQuery.facilitatorEmail)}`)
if (jsonQuery.facilitatorId) {
where.push(`facilitatorEmail = ${escape(jsonQuery.facilitatorId)}`)
}

let query = `SELECT * FROM rides ${where.length ? ' WHERE ' + where.join(' AND ') : ''} ORDER BY pickupTimeAndDateInUTC ASC;`;
console.log(query);
return this._databaseManager.query(query, connection);
return this._databaseManager.query(query, connection)
.then(rides =>
rides.map(ride => {
// Workaround to map facilitatorEmail from database to the facilitatorId in the entity
ride.facilitatorId = ride.facilitatorId || ride.facilitatorEmail;
delete ride.facilitatorEmail;
return ride;
})
)
}

}

module.exports = RideRepository;
16 changes: 6 additions & 10 deletions backend/src/main/rides/RideStatus.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
class RideStatus {
constructor(status) {
this.statusName = status;
}

static get OPEN(){ return new RideStatus('OPEN'); }
static get CONFIRMED(){ return new RideStatus('CONFIRMED'); }
static get ENDED(){ return new RideStatus('ENDED'); }
static get CANCELLED(){ return new RideStatus('CANCELLED'); }
}
const RideStatus = {
OPEN: 'OPEN',
CONFIRMED: 'CONFIRMED',
ENDED: 'ENDED',
CANCELLED: 'CANCELLED',
};

module.exports = RideStatus;
Loading

0 comments on commit 9756daf

Please sign in to comment.