scope: services, interfaces subject: Apply code formatting - Correct indentation and formatting to match code style standards in multiple 'interfaces' and 'services' files. - Also ensure lines at the end of the files. Signed-off-by: Mathis <yidhra@tuta.io>
153 lines
4.0 KiB
TypeScript
153 lines
4.0 KiB
TypeScript
import type { IDbCategory } from "@interfaces/database/IDbCategory";
|
|
import MysqlService from "@services/mysql.service";
|
|
import { Logger } from "tslog";
|
|
import { v4 as uuidv4 } from "uuid";
|
|
|
|
const DbHandler = new MysqlService.Handler("CategoryService");
|
|
const logger = new Logger({
|
|
name: "CategoryService",
|
|
});
|
|
|
|
/**
|
|
* Creates a new category with the given data.
|
|
*
|
|
* @param {IDbCategory} data - The category data.
|
|
* @returns {Promise<boolean>} A promise that resolves with the created category.
|
|
* If an error occurs, the promise will reject with the error.
|
|
*/
|
|
async function createCategory(
|
|
data: IDbCategory,
|
|
): Promise<boolean> {
|
|
logger.info(
|
|
`Creating a new category... (${data.display_name})`,
|
|
);
|
|
try {
|
|
await MysqlService.Category.insert(DbHandler, {
|
|
id: uuidv4(),
|
|
display_name: data.display_name,
|
|
slug_name: data.slug_name,
|
|
});
|
|
//TODO Return the new id
|
|
return true;
|
|
} catch (error) {
|
|
logger.error(`Error creating category: ${error}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update a category in the database.
|
|
*
|
|
* @param {IDbCategory} data - The data of the category to update.
|
|
* @property {number} data.id - The id of the category.
|
|
* @property {string} [data.slug_name] - The slug name of the category.
|
|
* @property {string} [data.display_name] - The display name of the category.
|
|
*
|
|
* @returns {boolean} - Returns true if the category is updated successfully, false otherwise.
|
|
*/
|
|
async function updateCategory(data: IDbCategory) {
|
|
if (!data.id) {
|
|
logger.error("Category id is missing.");
|
|
return false;
|
|
}
|
|
try {
|
|
await MysqlService.Category.update(DbHandler, {
|
|
id: data.id,
|
|
slug_name: data.slug_name,
|
|
display_name: data.display_name,
|
|
});
|
|
//TODO Return id
|
|
return true;
|
|
} catch (err) {
|
|
logger.error(err);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves all categories from the database.
|
|
*
|
|
* @returns {Promise<Array<IDbCategory>> | null} Promise that resolves to an array of IDbCategory objects or null if an error occurred.
|
|
*/
|
|
async function getAll(): Promise<Promise<
|
|
Array<IDbCategory>
|
|
> | null> {
|
|
try {
|
|
logger.info("Getting all categories...");
|
|
return await MysqlService.Category.getAll(DbHandler);
|
|
} catch (error) {
|
|
logger.error(`Error getting all categories: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets a category by its slug
|
|
*
|
|
* @param {string} slug - The slug of the category
|
|
* @return {Promise<IDbCategory|null>} - A promise that resolves to the category object or null if not found
|
|
*/
|
|
async function getBySlug(
|
|
slug: string,
|
|
): Promise<IDbCategory | null> {
|
|
try {
|
|
logger.info(`Getting category by slug... (${slug})`);
|
|
return await MysqlService.Category.getBySlug(
|
|
DbHandler,
|
|
slug,
|
|
);
|
|
} catch (error) {
|
|
logger.error(`Error getting category by slug: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Retrieves a category from the database by its id.
|
|
*
|
|
* @param {string} id - The id of the category to retrieve.
|
|
* @returns {Promise<IDbCategory | null>} - A Promise that resolves with the retrieved category object or null if not found.
|
|
*/
|
|
async function getById(
|
|
id: string,
|
|
): Promise<IDbCategory | null> {
|
|
try {
|
|
logger.info(`Getting category by id... (${id})`);
|
|
return await MysqlService.Category.getById(DbHandler, id);
|
|
} catch (error) {
|
|
logger.error(`Error getting category by id: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
//FEAT Get all models in category (slug)
|
|
|
|
/**
|
|
* Deletes a category with the given ID from the database.
|
|
*
|
|
* @param {string} id - The ID of the category to delete.
|
|
* @return {Promise} - A Promise that resolves to the deleted category if successful, or null if an error occurs.
|
|
*/
|
|
async function deleteCategory(id: string): Promise<unknown> {
|
|
//TODO Verify if exist
|
|
//TODO Verify if element linked to category
|
|
try {
|
|
logger.info(`Deleting category... (${id})`);
|
|
return await MysqlService.Category.delete(DbHandler, id);
|
|
} catch (error) {
|
|
logger.error(`Error deleting category: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
const CategoryService = {
|
|
create: createCategory,
|
|
delete: deleteCategory,
|
|
update: updateCategory,
|
|
getAll,
|
|
getBySlug,
|
|
getById,
|
|
};
|
|
|
|
export default CategoryService;
|