Skip to content

Commit

Permalink
#30 - Changed circleci config.
Browse files Browse the repository at this point in the history
  • Loading branch information
douglascvas committed Sep 4, 2018
1 parent 49e3db7 commit 7dae064
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 19 deletions.
15 changes: 8 additions & 7 deletions backend/src/main/database/DatabaseManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class DatabaseManager {
port: process.env.MYSQL_PORT || 3306,
user: process.env.MYSQL_USER || 'root',
password: process.env.MYSQL_PW || 'admin',
database: process.env.MYSQL_DB || 'carpal',
database: process.env.MYSQL_DB,
multipleStatements: true
};
}
Expand All @@ -28,12 +28,13 @@ class DatabaseManager {
connection.query(queryString, (error, results, fields) => {
if (closeConnection) {
let closePromise = this.closeConnection(connection);
return closePromise.finally(() => {
if (error) {
return reject(error);
}
resolve(results);
});
return closePromise
.then(() => {
resolve(results);
})
.catch(error1 => {
reject(error1 || error);
});
}
if (error) {
console.log("Error executing", queryString, error);
Expand Down
6 changes: 4 additions & 2 deletions backend/src/main/rides/CreateRideService.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const rideSchema = require('../schema/ride.json');
const RideStatus = require('./RideStatus');
const RideRepository = require('./RideRepository');
const RideMapper = require('./RideMapper');
const PromiseUtils = require('../utils/PromiseUtils');

class CreateRideService {

Expand All @@ -16,8 +17,9 @@ class CreateRideService {
createRide(body, loginData) {
const connection = this._databaseManager.createConnection();

return this._createRide(body, loginData, connection)
.finally(() => this._databaseManager.closeConnection(connection));
const createRidePromise = this._createRide(body, loginData, connection);
const closeConnection = () => this._databaseManager.closeConnection(connection);
return PromiseUtils.promiseFinally(createRidePromise, closeConnection);
}

_createRide(rideObject, loginData, connection) {
Expand Down
6 changes: 4 additions & 2 deletions backend/src/main/rides/FindOneRideService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

class FindOneRideService {

Expand All @@ -16,8 +17,9 @@ class FindOneRideService {
}

const connection = this._databaseManager.createConnection();
return this._findOne(id, loginData, connection)
.finally(() => this._databaseManager.closeConnection(connection));
const findOnePromise = this._findOne(id, loginData, connection);
const closeConnection = () => this._databaseManager.closeConnection(connection);
return PromiseUtils.promiseFinally(findOnePromise, closeConnection);
}

_findOne(id, loginData, connection) {
Expand Down
8 changes: 5 additions & 3 deletions backend/src/main/rides/ListRidesService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

class ListRidesService {

Expand All @@ -13,9 +14,10 @@ class ListRidesService {
listRides(query, loginData) {
const connection = this._databaseManager.createConnection();

return this._listRides(query, loginData, connection)
.then(rides => rides.map(RidesMapper.entityToDto))
.finally(() => this._databaseManager.closeConnection(connection));
const listRidesPromise = this._listRides(query, loginData, connection)
.then(rides => rides.map(RidesMapper.entityToDto));
const closeConnection = () => this._databaseManager.closeConnection(connection);
return PromiseUtils.promiseFinally(listRidesPromise, closeConnection);
}

_listRides(query, loginData, connection) {
Expand Down
7 changes: 4 additions & 3 deletions backend/src/main/rides/RideRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@ const moment = require('moment');
class RideRepository {
constructor(databaseManager) {
this._databaseManager = databaseManager;
this._dbName = this._databaseManager.databaseConfig.database;
}

create(ride, connection) {
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 = `INSERT INTO rides(client,
let query = `INSERT INTO ${this._dbName}.rides(client,
facilitatorEmail,
pickupTimeAndDateInUTC,
locationFrom,
Expand Down Expand Up @@ -60,7 +61,7 @@ class RideRepository {
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)},
let query = `UPDATE ${this._dbName}.rides SET client = ${escape(ride.client)},
facilitatorEmail = ${escape(ride.facilitatorId)},
pickupTimeAndDateInUTC = ${escape(moment(ride.pickupTimeAndDateInUTC).format('YYYY-MM-DD HH:mm:ss'))},
locationFrom = ${locationFrom},
Expand Down Expand Up @@ -120,7 +121,7 @@ class RideRepository {
where.push(`facilitatorEmail = ${escape(jsonQuery.facilitatorId)}`)
}

let query = `SELECT * FROM rides ${where.length ? ' WHERE ' + where.join(' AND ') : ''} ORDER BY pickupTimeAndDateInUTC ASC;`;
let query = `SELECT * FROM ${this._dbName}.rides ${where.length ? ' WHERE ' + where.join(' AND ') : ''} ORDER BY pickupTimeAndDateInUTC ASC;`;
console.log(query);
return this._databaseManager.query(query, connection)
.then(rides =>
Expand Down
5 changes: 3 additions & 2 deletions backend/src/main/rides/UpdateRideService.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const jsonValidator = require('jsonschema');
const rideSchema = require('../schema/ride.json');
const RideRepository = require('./RideRepository');
const RideMapper = require('./RideMapper');
const PromiseUtils = require('../utils/PromiseUtils');

class UpdateRideService {

Expand All @@ -14,9 +15,9 @@ class UpdateRideService {

updateRide(id, ride, loginData) {
const connection = this._databaseManager.createConnection();

let updatePromise = this._updateRide(id, ride, loginData, connection);
return updatePromise.finally(() => this._databaseManager.closeConnection(connection));
const closeConnection = () => this._databaseManager.closeConnection(connection);
return PromiseUtils.promiseFinally(updatePromise, closeConnection);
}

_updateRide(id, rideObject, loginData, connection) {
Expand Down
17 changes: 17 additions & 0 deletions backend/src/main/utils/PromiseUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class PromiseUtils {
static promiseFinally(promise, finallyFn) {
return new Promise((resolve, reject) => {
promise
.then(result => {
return Promise.resolve(finallyFn())
.then(() => resolve(result))
})
.catch(error => {
return Promise.resolve(finallyFn())
.then(() => resolve(reject(error)));
});
});
}
}

module.exports = PromiseUtils;

0 comments on commit 7dae064

Please sign in to comment.