Compare commits

...

3 Commits

Author SHA1 Message Date
3525fb12e6
feat(routes): add create vehicle function to route
This commit hooks up the `VehicleController.create` function to the route handling post requests at `/veh/new`. With this change, admin users can add new vehicles via this endpoint.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-05-03 11:23:13 +02:00
82afff9878
feat(services): add update function in mysql service
This commit adds an `update` function to the `mysql.service.ts`. This new function will update a rent record in the database. It takes a database handler object and a rent object containing the updated data, and returns a Promise that resolves to the status result of the update operation. The function will throw an error if an issue occurs during the update operation.

Issue: #23
Signed-off-by: Mathis <yidhra@tuta.io>
2024-05-03 11:23:00 +02:00
fef2bda082
refactor(services): simplify SQL query strings and reformat function parameters
This commit includes the following changes in `mysql.service.ts` file:
- Simplify SQL query strings for `UPDATE` and `INSERT` commands in various methods to make them more readable.
- Reformat parameters in `getAllModelsFromCategory` and `delete` methods for better readability.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-05-03 11:18:30 +02:00
2 changed files with 25 additions and 25 deletions

View File

@ -12,7 +12,7 @@ RentRouter.route("/affected").get(UserGuard);
RentRouter.route("/affected/all").get(AdminGuard);
// Add a new vehicle (admin only)
RentRouter.route("/veh/new").post(AdminGuard);
RentRouter.route("/veh/new").post(AdminGuard, VehicleController.create);
// Get all vehicles
RentRouter.route("/veh/all").get(VehicleController.getAll);

View File

@ -228,9 +228,7 @@ const MySqlService = {
actionName: "Update user..",
})
.then((factorizeResult) => {
const _sql = `UPDATE users SET (${factorizeResult._keysTemplate}) WERE id VALUES(${
factorizeResult._questionMarksFields
})`;
const _sql = `UPDATE users SET (${factorizeResult._keysTemplate}) WERE id VALUES(${factorizeResult._questionMarksFields})`;
try {
if (isDebugMode()) handler.Logger.trace(_sql);
@ -432,9 +430,7 @@ const MySqlService = {
actionName: "Update brand..",
})
.then((factorizeResult) => {
const _sql = `UPDATE brands SET (${factorizeResult._keysTemplate}) WERE id VALUES(${
factorizeResult._questionMarksFields
})`;
const _sql = `UPDATE brands SET (${factorizeResult._keysTemplate}) WERE id VALUES(${factorizeResult._questionMarksFields})`;
try {
if (isDebugMode()) handler.Logger.trace(_sql);
@ -672,9 +668,7 @@ const MySqlService = {
actionName: "Update model..",
})
.then((factorizeResult) => {
const _sql = `UPDATE models SET (${factorizeResult._keysTemplate}) WERE id VALUES(${
factorizeResult._questionMarksFields
})`;
const _sql = `UPDATE models SET (${factorizeResult._keysTemplate}) WERE id VALUES(${factorizeResult._questionMarksFields})`;
try {
if (isDebugMode()) handler.Logger.trace(_sql);
@ -778,9 +772,7 @@ const MySqlService = {
actionName: "Update vehicle..",
})
.then((factorizeResult) => {
const _sql = `UPDATE vehicles SET (${factorizeResult._keysTemplate}) WERE id VALUES(${
factorizeResult._questionMarksFields
})`;
const _sql = `UPDATE vehicles SET (${factorizeResult._keysTemplate}) WERE id VALUES(${factorizeResult._questionMarksFields})`;
try {
if (isDebugMode()) handler.Logger.trace(_sql);
@ -903,9 +895,9 @@ const MySqlService = {
.then((factorizedResult) => {
const valuesArray = factorizedResult._valuesArray;
const template = factorizedResult._keysTemplate;
const _sql = `INSERT INTO categories (${template + `, id`}) VALUES(${
factorizedResult._questionMarksFields
})`;
const _sql = `INSERT INTO categories (${
template + `, id`
}) VALUES(${factorizedResult._questionMarksFields})`;
try {
if (isDebugMode()) handler.Logger.trace(_sql);
@ -939,9 +931,7 @@ const MySqlService = {
actionName: "Update brand..",
})
.then((factorizeResult) => {
const _sql = `UPDATE categories SET (${factorizeResult._keysTemplate}) WERE id VALUES(${
factorizeResult._questionMarksFields
})`;
const _sql = `UPDATE categories SET (${factorizeResult._keysTemplate}) WERE id VALUES(${factorizeResult._questionMarksFields})`;
try {
if (isDebugMode()) handler.Logger.trace(_sql);
@ -1066,7 +1056,10 @@ const MySqlService = {
* @param {string} categoryId - The ID of the category.
* @return {Promise<Array<IDbModel>>} - A promise that resolves to an array of database models belonging to the specified category.
*/
getAllModelsFromCategory(handler: MysqlHandler, categoryId: string): Promise<Array<IDbModel>> {
getAllModelsFromCategory(
handler: MysqlHandler,
categoryId: string,
): Promise<Array<IDbModel>> {
return new Promise((resolve, reject) => {
const _sql =
"SELECT models.* FROM models INNER JOIN categories ON models.category_id = categories.id WHERE categories.id VALUES(?)";
@ -1079,7 +1072,6 @@ const MySqlService = {
}
});
},
},
Rent: {
//ToTest
@ -1143,6 +1135,7 @@ const MySqlService = {
});
},
//ToTest
/**
* Retrieves all assigned vehicles from the database.
*
@ -1164,6 +1157,16 @@ const MySqlService = {
}
});
},
//ToTest
/**
* Updates a rent record in the database.
*
* @param {MysqlHandler} handler - The database handler object.
* @param {IDbRent} data - The rent object containing the updated data.
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the status result of the update operation.
* @throws {Error} - If an error occurs during the update operation.
*/
update(handler: MysqlHandler, data: IDbRent): Promise<IDbStatusResult> {
return new Promise((resolve, reject) => {
if (!data.id) return reject("Id is undefined");
@ -1197,10 +1200,7 @@ const MySqlService = {
});
},
delete(
handler: MysqlHandler,
rentId: string,
): Promise<IDbStatusResult> {
delete(handler: MysqlHandler, rentId: string): Promise<IDbStatusResult> {
return new Promise((resolve, reject) => {
if (!rentId) return reject("Id is undefined");
if (rentId.length !== 36) return reject("Id invalid");