Compare commits
2 Commits
30bd5a0dbe
...
f23aabccd4
Author | SHA1 | Date | |
---|---|---|---|
f23aabccd4 | |||
c70bcef352 |
78
src/services/model.service.ts
Normal file
78
src/services/model.service.ts
Normal 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;
|
Loading…
x
Reference in New Issue
Block a user