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 <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-05-03 11:59:58 +02:00
parent 3525fb12e6
commit 371d960cf3
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -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<IDbVehicle>} - A promise that resolves to the retrieved vehicle.
* @throws {Error} - If an error occurs while retrieving the vehicle.
* @returns {Promise<Array<IDbVehicle>>} - 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<IDbVehicle> {
getById(handler: MysqlHandler, vehicleId: string): Promise<Array<IDbVehicle>> {
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<IDbVehicle>);
});
} 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<Array<IDbRent>>} 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<Array<IDbRent>> {
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<IDbRent>);
});
} catch (err: unknown) {
reject(err as Error);
}
});
},
//ToTest
/**
* Retrieves all assigned vehicles from the database.