feat: Update MySQL service for category operations

This update introduces changes in the 'mysql.service.ts' to allow create and update operations for categories. The 'INSERT INTO' SQL query has been adjusted from targeting `users` to `categories`. An `update` function has also been added to allow updates in category data. The import order has been rearranged for better readability.

Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-25 12:01:02 +02:00
parent ef83ad18a4
commit 47adae9137
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -1,8 +1,8 @@
import mysql, {type Connection, type ConnectionOptions} from 'mysql2';
import {Logger} from "tslog";
import type IDbCategory from "@interfaces/database/IDbCategory";
import type {IDbModel} from "@interfaces/database/IDbModel";
import type {IDbUser} from "@interfaces/database/IDbUser";
import type IDbCategory from "@interfaces/database/IDbCategory";
import mysql, {type Connection, type ConnectionOptions} from 'mysql2';
import {Logger} from "tslog";
const access: ConnectionOptions = {
@ -351,7 +351,7 @@ const MySqlService = {
if (!data.id) return reject('Id is undefined');
if (data.id.length !== 36) return reject('Id invalid');
const _sql = "INSERT INTO `users`(`id`,`slug_name`, `display_name`) VALUES (?, ?, ?)"
const _sql = "INSERT INTO `categorys`(`id`,`slug_name`, `display_name`) VALUES (?, ?, ?)"
const _values = [
data.id,
data.slug_name,
@ -363,6 +363,31 @@ const MySqlService = {
reject(err as Error);
}
})
},
update(handler: MysqlHandler, data: IDbCategory) {
return new Promise((resolve, reject) => {
if (!data.id) return reject('Id is undefined');
if (data.id.length !== 36) return reject('Id invalid');
try {
const _template = `
${data.slug_name ? "`slug_name` = ?," : null}
${data.display_name ? "`display_name` = ?," : null}`
const _values = [
data.slug_name,
data.display_name,
data.id
]
const _sql = `UPDATE "categories" SET ${_template} WHERE 'id' = ?`;
return resolve(handler.execute(_sql, _values));
} catch (err: unknown) {
reject(err as Error);
}
})
}
}
}