From 371d960cf3dc09e31dbe439c3e8a38f517318d2f Mon Sep 17 00:00:00 2001 From: Mathis Date: Fri, 3 May 2024 11:59:58 +0200 Subject: [PATCH] feat(services): update getById method in mysql.service - Update the getById method in mysql.service to return an array of vehicles instead of a single vehicle - Add a new getById method to retrieve array of rental information by rental ID - Improve the documentation to match the modifications in both methods' functionalities Issue: #23 Signed-off-by: Mathis --- src/services/mysql.service.ts | 37 +++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/src/services/mysql.service.ts b/src/services/mysql.service.ts index afae857..c83c8b2 100644 --- a/src/services/mysql.service.ts +++ b/src/services/mysql.service.ts @@ -795,14 +795,14 @@ const MySqlService = { }, /** - * Retrieves a vehicle from the database by its ID. + * Retrieves an array of vehicles from the database by a given vehicle ID. * - * @param {MysqlHandler} handler - The instance of the MySQL handler. + * @param {MysqlHandler} handler - The MySQL handler object for executing database queries. * @param {string} vehicleId - The ID of the vehicle to retrieve. - * @returns {Promise} - A promise that resolves to the retrieved vehicle. - * @throws {Error} - If an error occurs while retrieving the vehicle. + * @returns {Promise>} - A promise that resolves with an array of vehicles matching the ID. + * @throws {Error} - If there is an error executing the query or the ID is invalid. */ - getById(handler: MysqlHandler, vehicleId: string): Promise { + getById(handler: MysqlHandler, vehicleId: string): Promise> { return new Promise((resolve, reject) => { if (!vehicleId) return reject("Id is undefined"); if (vehicleId.length !== 36) return reject("Id invalid"); @@ -810,7 +810,7 @@ const MySqlService = { const _values = [vehicleId]; try { handler.execute(_sql, _values).then((result) => { - return resolve(result as unknown as IDbVehicle); + return resolve(result as unknown as Array); }); } catch (err: unknown) { reject(err as Error); @@ -1135,6 +1135,31 @@ const MySqlService = { }); }, + /** + * Retrieves rental information from the database by rental ID. + * + * @param {MysqlHandler} handler - The instance of the MySQL handler used to execute the query. + * @param {string} rentId - The ID of the rental to retrieve. + * + * @returns {Promise>} A promise that resolves with an array of rental information + * or rejects with an error message if the ID is undefined or invalid. + */ + getById(handler: MysqlHandler, rentId: string): Promise> { + return new Promise((resolve, reject) => { + if (!rentId) return reject("Id is undefined"); + if (rentId.length !== 36) return reject("Id invalid"); + const _sql = "SELECT * FROM rents WHERE id VALUES(?)"; + const _values = [rentId]; + try { + handler.execute(_sql, _values).then((result) => { + return resolve(result as unknown as Array); + }); + } catch (err: unknown) { + reject(err as Error); + } + }); + }, + //ToTest /** * Retrieves all assigned vehicles from the database.