Compare commits

...

2 Commits

Author SHA1 Message Date
f23aabccd4
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>
2024-04-26 11:58:04 +02:00
c70bcef352
feat(services): add model service
This commit adds a new service `ModelService` to handle model operations such as create, update, and delete. Some features like getBySlug, getAll, getById, getByCategory, and getByBrand are also introduced.

Issue: #27
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-26 11:53:42 +02:00

View File

@ -0,0 +1,78 @@
import type IDbModel from "@interfaces/database/IDbModel";
import MysqlService from "@services/mysql.service";
import {Logger} from "tslog";
import { v4 as uuidv4 } from 'uuid';
const DbHandler = new MysqlService.Handler('ModelService')
const logger = new Logger({name: 'ModelService'})
//SEC TODO validate blob
/**
* 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 {
await MysqlService.Model.insert(DbHandler, {
id: uuidv4(),
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
})
//TODO Return the new id
logger.info('Success !')
return true;
} catch (error) {
logger.error(`Error creating category: ${error}`);
return false;
}
}
/**
* 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,
delete: deleteModel,
getBySlug: getBySlugModel,
getAll: getAllModels,
getById: getByIdModel,
getByCategory: getByCategoryModel,
getByBrand: getModelsByBrand,
}
export default ModelService;