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>
This commit is contained in:
Mathis H (Avnyr) 2024-04-26 12:12:03 +02:00
parent 13d72ad529
commit 8711b3530a
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -91,6 +91,54 @@ async function deleteModel(modelSlug: string): Promise<boolean> {
}
}
/**
* 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,