feat(services): add updateModel function and refine createModel function in model.service.ts

This commit mainly includes adding `updateModel` function, which updates a model in the database and refines `createModel` function in `model.service.ts`. It provides more clear explanations before the function and also specifies the return data type.

Issue: #27
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-26 11:58:04 +02:00
parent c70bcef352
commit f23aabccd4
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -7,7 +7,14 @@ const DbHandler = new MysqlService.Handler('ModelService')
const logger = new Logger({name: 'ModelService'})
//SEC TODO validate blob
async function createModel(data: IDbModel) {
/**
* Creates a new model with the provided data.
*
* @param {IDbModel} data - The data for the new model.
*
* @return {Promise<boolean>} - Indicates whether the model was created successfully.
*/
async function createModel(data: IDbModel): Promise<boolean> {
logger.info(`Creating a new model... (${data.display_name})`)
//TODO Validate IDbModel data
try {
@ -30,6 +37,33 @@ async function createModel(data: IDbModel) {
}
}
/**
* Updates a model in the database.
*
* @param {IDbModel} data - The model data to update.
*
* @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})`);
try {
await MysqlService.Model.update(DbHandler, {
display_name: data.display_name,
slug_name: data.slug_name,
image_blob: data.image_blob,
brand_id: data.brand_id,
category_id: data.category_id,
base_price: data.base_price,
is_trending: data.is_trending,
});
logger.info("Update Successful !");
return true;
} catch (error) {
logger.error(`Error updating model: ${error}`);
return false;
}
}
const ModelService = {
create: createModel,
update: updateModel,