feat(services): enhance insert and update methods in mysql.service.ts

- Enhanced `insert` and `update` methods in `mysql.service.ts`
- Added detailed documentation and type checks for `insert` function.
- Implemented body of `update` method, included parameter validation and SQL query building.

Issue: #20
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-29 12:05:34 +02:00
parent cd4b8479d2
commit edf2f6880c
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -581,8 +581,16 @@ const MySqlService = {
})
}
},
//ToTest
Vehicle: {
insert(handler: MysqlHandler, data: IDbVehicle) {
/**
* Inserts a new record into the `vehicles` table.
* @param {MysqlHandler} handler - The MySQL handler object.
* @param {IDbVehicle} data - The vehicle data to be inserted.
* @throws Throws an error if the provided `data` object does not contain the `id` property or if the `id` is not a valid UUID.
* @returns {Promise<IDbStatusResult>} A promise that resolves to the result of the insert operation.
*/
insert(handler: MysqlHandler, data: IDbVehicle): Promise<IDbStatusResult> {
return new Promise((resolve, reject) => {
if (!data.id) return reject('Id is undefined');
if (data.id.length !== 36) return reject('Id invalid');
@ -603,10 +611,38 @@ const MySqlService = {
})
},
//TODO update by id
update(handler: MysqlHandler, data: IDbVehicle) {
/**
* Update a vehicle record in the database.
*
* @param {MysqlHandler} handler - The MySQL handler object for executing the SQL query.
* @param {IDbVehicle} data - The updated vehicle data.
* @throws {string} Throws an error if the id is undefined or invalid.
* @returns {Promise<IDbStatusResult>} Returns a Promise that resolves to the status result of the update operation.
*/
update(handler: MysqlHandler, data: IDbVehicle): Promise<IDbStatusResult> {
return new Promise((resolve, reject) => {
if (!data.id) return reject('Id is undefined');
if (data.id.length !== 36) return reject('Id invalid');
try {
const _template = `
${data.model_id ? "`model_id` = ?," : null}
${data.plate_number ? "`plate_number` = ?," : null}
${data.odometer ? "`odometer` = ?," : null}
${data.health_state ? "`health_state` = ?," : null}`
const _values = [
data.model_id,
data.plate_number,
data.odometer,
data.health_state,
data.id
]
const _sql = `UPDATE "vehicles" SET ${_template} WHERE 'id' = ?`;
return resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
} catch (err: unknown) {
reject(err as Error);
}
})
},