Compare commits

...

3 Commits

Author SHA1 Message Date
5d53cd28f8
refactor(services): update methods in ModelService
Removed `getByIdModel`, `getByCategoryModel`, and `getModelsByBrand` methods. These changes are to refine the methods available in ModelService for better usage and understanding.

Issue: #27
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-26 12:13:28 +02:00
8711b3530a
feat(services): add fetch functions for models
Added two new functions, `getBySlugModel` and `getAllModels`, to the `ModelService` in `model.service.ts`. `getBySlugModel` fetches a model based on its slug while `getAllModels` fetches all models from the database.

Issue: #27
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-26 12:12:03 +02:00
13d72ad529
feat(services): update logging in updateModel and add deleteModel
The `updateModel` function's logging has been updated to use `slug_name` instead of `id`. Additionally, a new function `deleteModel` has been added to remove models from the database based on their slug. This function checks the existence of a model before attempting deletion and logs the process.

Issue: #27
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-26 12:06:19 +02:00

View File

@ -45,7 +45,7 @@ async function createModel(data: IDbModel): Promise<boolean> {
* @return {Promise<boolean>} - A promise that resolves to a boolean indicating whether the update was successful or not.
*/
async function updateModel(data: IDbModel): Promise<boolean> {
logger.info(`Updating model... (${id})`);
logger.info(`Updating model... (${data.slug_name})`);
try {
await MysqlService.Model.update(DbHandler, {
display_name: data.display_name,
@ -64,15 +64,91 @@ async function updateModel(data: IDbModel): Promise<boolean> {
}
}
/**
* Deletes a model from the database.
*
* @param {string} modelSlug - The slug of the model to be deleted.
* @return {Promise<boolean>} - A promise that resolves to true if the deletion is successful, else false.
*/
async function deleteModel(modelSlug: string): Promise<boolean> {
if (!modelSlug) {
logger.error("Model slug is missing");
return false
}
logger.info(`Deleting model with ID: ${modelSlug}`);
const doesExist = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
if (!doesExist || !doesExist.id) {
logger.warn(`Model with slug ${modelSlug} not found`);
return false;
}
try {
await MysqlService.Model.delete(DbHandler, doesExist.id);
logger.info("Deletion Successful !");
return true;
} catch (error) {
logger.error(`Error deleting model: ${error}`);
return false;
}
}
/**
* Fetches a model by slug from the database.
*
* @param {string} modelSlug - The slug of the model to be fetched.
* @return {Promise<IDbModel | null>} - A promise that resolves to the model if found, else null.
*/
async function getBySlugModel(modelSlug: string): Promise<IDbModel | null> {
logger.info(`Fetching model with slug: ${modelSlug}`);
try {
const model = await MysqlService.Model.getBySlug(DbHandler, modelSlug);
if (!model) {
logger.warn(`Model with slug ${modelSlug} not found`);
return null;
}
return model;
} catch (error) {
logger.error(`Error fetching model by slug: ${error}`);
return null;
}
}
/**
* Fetches all models from the database.
*
* @return {Promise<IDbModel[] | null>} - A promise that resolves to an array of all models if found, else null.
*/
async function getAllModels(): Promise<IDbModel[] | null> {
logger.info("Fetching all models from the database");
try {
const models = await MysqlService.Model.getAll(DbHandler);
if (!models || models.length === 0) {
logger.warn("No models found on the database");
return null;
}
logger.info(`Found ${models.length} model(s)`)
return models;
} catch (error) {
logger.error(`Error fetching all models: ${error}`);
return null;
}
}
/**
* ModelService is responsible for managing models.
* @namespace
*/
const ModelService = {
create: createModel,
update: updateModel,
delete: deleteModel,
getBySlug: getBySlugModel,
getAll: getAllModels,
getById: getByIdModel,
getByCategory: getByCategoryModel,
getByBrand: getModelsByBrand,
//getByCategory: getByCategoryModel,
//getByBrand: getModelsByBrand,
}
export default ModelService;