diff --git a/backend/src/main/database/DatabaseManager.js b/backend/src/main/database/DatabaseManager.js index fa3b4eb0..2cead150 100644 --- a/backend/src/main/database/DatabaseManager.js +++ b/backend/src/main/database/DatabaseManager.js @@ -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 }; } @@ -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); diff --git a/backend/src/main/rides/CreateRideService.js b/backend/src/main/rides/CreateRideService.js index db4ae7a1..4533c0fe 100644 --- a/backend/src/main/rides/CreateRideService.js +++ b/backend/src/main/rides/CreateRideService.js @@ -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 { @@ -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) { diff --git a/backend/src/main/rides/FindOneRideService.js b/backend/src/main/rides/FindOneRideService.js index f53981f4..5aee8b83 100644 --- a/backend/src/main/rides/FindOneRideService.js +++ b/backend/src/main/rides/FindOneRideService.js @@ -2,6 +2,7 @@ const RideRepository = require('./RideRepository'); const RidesMapper = require('./RideMapper'); +const PromiseUtils = require('../utils/PromiseUtils'); class FindOneRideService { @@ -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) { diff --git a/backend/src/main/rides/ListRidesService.js b/backend/src/main/rides/ListRidesService.js index 3192e62c..d5ee4827 100644 --- a/backend/src/main/rides/ListRidesService.js +++ b/backend/src/main/rides/ListRidesService.js @@ -2,6 +2,7 @@ const RideRepository = require('./RideRepository'); const RidesMapper = require('./RideMapper'); +const PromiseUtils = require('../utils/PromiseUtils'); class ListRidesService { @@ -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) { diff --git a/backend/src/main/rides/RideRepository.js b/backend/src/main/rides/RideRepository.js index 520edab7..fd2dce1b 100644 --- a/backend/src/main/rides/RideRepository.js +++ b/backend/src/main/rides/RideRepository.js @@ -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, @@ -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}, @@ -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 => diff --git a/backend/src/main/rides/UpdateRideService.js b/backend/src/main/rides/UpdateRideService.js index 08f331f5..86f0d267 100644 --- a/backend/src/main/rides/UpdateRideService.js +++ b/backend/src/main/rides/UpdateRideService.js @@ -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 { @@ -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) { diff --git a/backend/src/main/utils/PromiseUtils.js b/backend/src/main/utils/PromiseUtils.js new file mode 100644 index 00000000..2772cf9e --- /dev/null +++ b/backend/src/main/utils/PromiseUtils.js @@ -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; \ No newline at end of file