feat(services): update MySQL services with method adjustments

- Modified 'getById' reject message for accuracy.
- Adjusted 'getBySlug' method parameters for clarity and renamed the field used in the SQL statement.
- Added a new 'getById' method for retrieving models by ID.
- Updated the 'insert' method to correctly insert data into the `models` table.
- Refactored 'getBySlug' method for categories, renaming the parameter and making error messages more precise.

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

View File

@ -361,7 +361,7 @@ const MySqlService = {
*/
getById(handler: MysqlHandler, brandId: string): Promise<IDbBrand> {
return new Promise((resolve, reject) => {
if (!brandId) return reject('slug is undefined')
if (!brandId) return reject('Id is undefined')
if (brandId.length !== 36) return reject('Id invalid');
const _sql = "SELECT * FROM `brands` WHERE `id` = ?";
const _values = [brandId];
@ -447,14 +447,14 @@ const MySqlService = {
* Retrieves a database model by slug from a given MySQL handler.
*
* @param {MysqlHandler} handler - The MySQL handler instance.
* @param {string} slug - The slug of the model to retrieve.
* @param {string} modelSlug - The slug of the model to retrieve.
* @return {Promise<IDbModel>} A promise that resolves with the retrieved model.
* @throws {Error} If there was an error executing the query.
*/
getBySlug(handler: MysqlHandler, slug: string): Promise<IDbModel> {
getBySlug(handler: MysqlHandler, modelSlug: string): Promise<IDbModel> {
return new Promise((resolve, reject) => {
const _sql = "SELECT * FROM `models` WHERE `slug` = ?";
const _values = [slug];
const _sql = "SELECT * FROM `models` WHERE `slug_name` = ?";
const _values = [modelSlug];
try {
resolve(handler.execute(_sql, _values) as unknown as IDbModel);
} catch (err: unknown) {
@ -462,6 +462,13 @@ const MySqlService = {
}
});
},
/**
* Retrieves a model by its ID from the database.
*
* @param {MysqlHandler} handler - The MySQL handler object used to execute the SQL query.
* @param {string} modelId - The ID of the model to retrieve.
* @return {Promise<IDbModel>} - A promise that resolves with the retrieved model, or rejects with an error.
*/
getById(handler: MysqlHandler, modelId: string): Promise<IDbModel> {
return new Promise((resolve, reject) => {
const _sql = "SELECT * FROM `models` WHERE `id` = ?";
@ -474,12 +481,19 @@ const MySqlService = {
});
},
insert(handler: MysqlHandler, data: IDbModel) {
/**
* Inserts a new record into the `models` table.
*
* @param {MysqlHandler} handler - The MySQL handler instance.
* @param {IDbModel} data - The data to be inserted.
* @returns {Promise<unknown>} - A Promise that resolves with the execution result or rejects with an error.
*/
insert(handler: MysqlHandler, data: IDbModel): Promise<unknown> {
return new Promise((resolve, reject) => {
if (!data.id) return reject('Id is undefined');
if (data.id.length !== 36) return reject('Id invalid');
const _sql = "INSERT INTO `users`(`id`,`username`, `firstname`, `lastname`, `dob`, `email`, `is_mail_verified`, `is_admin`, `gdpr`, `hash`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
const _sql = "INSERT INTO `models`(`slug_name`,`display_name`, `brand_id`, `category_id`, `image_blob`, `is_trending`, `base_price`, `id`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"
const _values = [
data.slug_name,
data.display_name,
@ -487,7 +501,8 @@ const MySqlService = {
data.category_id,
data.image_blob,
data.is_trending,
data.base_price
data.base_price,
data.id
]
try {
resolve(handler.execute(_sql, _values))
@ -578,15 +593,15 @@ const MySqlService = {
* Retrieves a category from the database by its slug.
*
* @param {MysqlHandler} handler - The MySQL handler object to execute the query.
* @param {string} slug - The slug of the category to retrieve.
* @param {string} categorySlug - The slug of the category to retrieve.
* @returns {Promise<IDbCategory>} - A promise that resolves with the retrieved category object.
* @throws {Error} - If an error occurs while executing the query.
*/
getBySlug(handler: MysqlHandler, slug: string): Promise<IDbCategory> {
getBySlug(handler: MysqlHandler, categorySlug: string): Promise<IDbCategory> {
return new Promise((resolve, reject) => {
if (!slug) return reject('slug is undefined')
if (!categorySlug) return reject('slug is undefined')
const _sql = "SELECT * FROM `categories` WHERE `slug_name` = ?";
const _values = [slug];
const _values = [categorySlug];
try {
resolve(handler.execute(_sql, _values) as unknown as IDbCategory);
} catch (err: unknown) {