- Transform `getAvailable` method into a promise that retrieves available vehicles from the database. - This method now takes the MySQL handler object to execute the query and returns a promise that resolves to an array of available vehicles. - It also handles error while executing the query. Issue: #20 Signed-off-by: Mathis <yidhra@tuta.io>
843 lines
30 KiB
TypeScript
843 lines
30 KiB
TypeScript
import type {IDbCategory} from "@interfaces/database/IDbCategory";
|
|
import type {IDbModel} from "@interfaces/database/IDbModel";
|
|
import type {IDbUser} from "@interfaces/database/IDbUser";
|
|
import type {IDbBrand} from "@interfaces/database/IDbBrand";
|
|
import type {IDbStatusResult} from "@interfaces/database/IDbStatusResult";
|
|
import mysql, {type Connection, type ConnectionOptions} from 'mysql2';
|
|
import {Logger} from "tslog";
|
|
import process from "node:process";
|
|
import {IDbVehicle} from "@interfaces/database/IDbVehicle";
|
|
|
|
|
|
const access: ConnectionOptions = {
|
|
host: `${process.env["MYSQL_HOST"]}`,
|
|
port: Number.parseInt(`${process.env["MYSQL_PORT"]}`),
|
|
user: `${process.env["MYSQL_USER"]}`,
|
|
database: `${process.env["MYSQL_DATABASE"]}`,
|
|
password: `${process.env["MYSQL_PASS"]}`
|
|
};
|
|
|
|
|
|
class MysqlHandler {
|
|
private readonly handlerName: string;
|
|
private Logger: Logger<unknown>
|
|
private Connection: Connection;
|
|
|
|
constructor(handlerName?: string) {
|
|
this.handlerName = handlerName || 'Unknown';
|
|
this.Logger = new Logger({ name: `DB>> ${this.handlerName}` });
|
|
this.Connection = mysql.createConnection(access);
|
|
this.Connection.connect((err) => {
|
|
if (err) {
|
|
this.Logger.error(`Error connecting to MySQL: ${err}`);
|
|
process.exit(1);
|
|
}
|
|
this.Logger.info(`Connected to MySQL database (${access.database})`);
|
|
});
|
|
}
|
|
closeConnection() {
|
|
this.Connection.end();
|
|
};
|
|
|
|
query(queryString: string) {
|
|
return new Promise((resolve, reject) => {
|
|
this.Connection.query(queryString, (err, results) => {
|
|
if (err) {
|
|
this.Logger.error(`Error executing query: ${err}`);
|
|
reject(err);
|
|
} else {
|
|
resolve(results);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
execute(queryString: string, values: Array<string | boolean | Date | number>): Promise<unknown> {
|
|
return new Promise((resolve, reject) => {
|
|
this.Connection.execute(queryString, values, (err: mysql.QueryError | null, results: mysql.QueryResult) => {
|
|
if (err) {
|
|
this.Logger.error(`Error executing query: ${err}`);
|
|
reject(err);
|
|
} else {
|
|
resolve(results);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Unprepare a previously prepared SQL query.
|
|
*
|
|
* @param {string} queryString
|
|
* - The SQL query string to unprepare.
|
|
* @return {Promise}
|
|
* - A promise that resolves if the unprepare operation is successful,
|
|
* or rejects with an error if there was an error unpreparing the query.
|
|
*/
|
|
unprepare(queryString: string): Promise<unknown> {
|
|
return new Promise((resolve, reject) => {
|
|
try {
|
|
resolve(this.Connection.unprepare(queryString));
|
|
} catch (err) {
|
|
reject(err)
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
const MySqlService = {
|
|
Handler : MysqlHandler,
|
|
User: {
|
|
|
|
/**
|
|
* Insert a user into the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL database handler.
|
|
* @param {IDbUser} data - The user data to insert.
|
|
* @returns {Promise<unknown>} A promise that resolves if the user was inserted successfully, or rejects with an error.
|
|
* @throws {Error} If an error occurs while executing the query.
|
|
*/
|
|
insert(handler: MysqlHandler, data: IDbUser): 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 _values = [
|
|
data.id,
|
|
data.username,
|
|
data.firstname,
|
|
data.lastname,
|
|
data.dob,
|
|
data.email,
|
|
data.is_mail_verified,
|
|
data.is_admin,
|
|
data.gdpr,
|
|
data.hash
|
|
]
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown)
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Updates user data in the database.
|
|
* @param {MysqlHandler} handler - The MySQL handler object.
|
|
* @param {IDbUser} data - The user data to be updated.
|
|
* @returns {Promise<unknown>} - A promise that resolves when the update is successful.
|
|
* @throws {Error} - If an error occurs during the update process.
|
|
*/
|
|
update(handler: MysqlHandler, data: IDbUser): Promise<unknown> {
|
|
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.username ? "`username` = ?," : null}
|
|
${data.firstname ? "`firstname` = ?," : null}
|
|
${data.lastname ? "`lastname` = ?," : null}
|
|
${data.dob ? "`dob` = ?," : null}
|
|
${data.email ? "`email` = ?," : null}
|
|
${data.is_mail_verified ? "`is_mail_verified` = ?," : null}
|
|
${data.is_admin ? "`is_admin` = ?," : null}
|
|
${data.gdpr ? "`gdpr` = ?," : null}
|
|
${data.hash ? "`hash` = ?" : null}`
|
|
const _values = [
|
|
data.username,
|
|
data.firstname,
|
|
data.lastname,
|
|
data.dob,
|
|
data.email,
|
|
data.is_mail_verified,
|
|
data.is_admin,
|
|
data.gdpr,
|
|
data.hash,
|
|
data.id
|
|
]
|
|
|
|
const _sql = `UPDATE "users" SET ${_template} WHERE 'id' = ?`;
|
|
return resolve(handler.execute(_sql, _values));
|
|
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Retrieves a user from the database based on their ID.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used to execute the query.
|
|
* @param {string} userId - The ID of the user to retrieve from the database.
|
|
*
|
|
* @return {Promise<IDbUser>} - A promise that resolves with the retrieved user object (of type IDbUser).
|
|
* - Rejects with an error if the ID is invalid or if an error occurs during the database operation.
|
|
*/
|
|
getById(handler: MysqlHandler, userId: string): Promise<IDbUser> {
|
|
return new Promise((resolve, reject) => {
|
|
if (userId.length !== 36) return reject('Id invalid');
|
|
const _sql = "SELECT * FROM `users` WHERE `id` = ?";
|
|
const _values = [userId];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbUser);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Retrieves all users from the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used to query the database.
|
|
* @return {Promise<Array<IDbUser>>} - A promise that resolves with an array of user objects from the database.
|
|
* @throws {Error} - If an error occurs while querying the database.
|
|
*/
|
|
getAll(handler: MysqlHandler): Promise<Array<IDbUser>> {
|
|
return new Promise((resolve, reject) => {
|
|
const _sql = "SELECT * FROM `users`";
|
|
try {
|
|
resolve(handler.query(_sql) as unknown as Array<IDbUser>);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Retrieves a user from the database by email.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL database handler instance.
|
|
* @param {string} email - The email of the user to retrieve.
|
|
* @return {Promise<IDbUser>} - A promise that resolves to the retrieved user object.
|
|
* @throws {Error} - If an error occurs while retrieving the user.
|
|
*/
|
|
getByEmail(handler: MysqlHandler, email: string): Promise<IDbUser> {
|
|
return new Promise((resolve, reject) => {
|
|
if (!email) return reject('email is undefined')
|
|
const _sql = "SELECT * FROM `users` WHERE `email` = ?";
|
|
const _values = [email];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbUser);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Retrieves the admin state for a given user ID.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used for executing the query.
|
|
* @param {string} userId - The ID of the user to get the admin state for.
|
|
* @return {Promise<boolean>} A Promise that resolves to a boolean value indicating whether the user is an admin or not.
|
|
* @throws {Error} if an error occurs during the execution of the query.
|
|
*/
|
|
getAdminStateForId(handler: MysqlHandler, userId: string) : Promise<boolean> {
|
|
return new Promise((resolve, reject) => {
|
|
const _sql = "SELECT `is_admin` FROM `users` WHERE `id` = ?";
|
|
const _values = [userId];
|
|
try {
|
|
const isAdmin = handler.execute(_sql, _values)
|
|
isAdmin.then((result) => {
|
|
if (result !== true) return resolve(false);
|
|
return resolve(true)
|
|
});
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Deletes a user from the database.
|
|
* @param {MysqlHandler} handler - The MySQL handler object.
|
|
* @param {string} userId - The ID of the user to delete.
|
|
* @return {Promise<unknown>} - A Promise that resolves when the deletion is successful, or rejects with an error.
|
|
*/
|
|
delete(handler: MysqlHandler, userId: string): Promise<unknown> {
|
|
return new Promise((resolve, reject) => {
|
|
if (!userId) return reject('Id is undefined');
|
|
if (userId.length !== 36) return reject('Id invalid');
|
|
const _sql = "DELETE FROM `users` WHERE `id` = ?";
|
|
const _values = [userId];
|
|
try {
|
|
resolve(handler.execute(_sql, _values));
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
}
|
|
},
|
|
Brand: {
|
|
|
|
/**
|
|
* Inserts a new record into the `brands` table.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used to execute the SQL query.
|
|
* @param {IDbBrand} data - The data to be inserted into the table.
|
|
* @returns {Promise<IDbStatusResult>} - A Promise that resolves to the status result of the insert operation.
|
|
* @throws {Error} - If an error occurs during the execution of the SQL query.
|
|
*/
|
|
insert(handler: MysqlHandler, data: IDbBrand): Promise<IDbStatusResult> {
|
|
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 `brands`(`id`,`display_name`, `slug_name`, `image_blob`) VALUES (?, ?, ?, ?)"
|
|
const _values = [
|
|
data.id,
|
|
data.display_name,
|
|
data.slug_name,
|
|
data.image_blob
|
|
]
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult)
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
|
|
/**
|
|
* Update the brand information in the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used for executing the SQL query.
|
|
* @param {IDbBrand} data - The data object containing the updated brand information.
|
|
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the status result of the update operation.
|
|
* @throws {Error} - If any error occurs during the process.
|
|
*/
|
|
update(handler: MysqlHandler, data: IDbBrand): Promise<IDbStatusResult> {
|
|
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}
|
|
${data.image_blob ? "`slug_name` = ?," : null}`
|
|
|
|
const _values = [
|
|
data.slug_name,
|
|
data.display_name,
|
|
data.image_blob,
|
|
data.id
|
|
]
|
|
const _sql = `UPDATE "brands" SET ${_template} WHERE 'id' = ?`;
|
|
return resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
|
|
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Retrieves all records from the `brands` table.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object to execute the query.
|
|
*
|
|
* @return {Promise<Array<IDbBrand>>} A promise that resolves to an array of IDbBrand objects representing the retrieved records. If an error occurs during the query execution, the promise will be rejected with an Error object.
|
|
*/
|
|
getAll(handler: MysqlHandler): Promise<Array<IDbBrand>> {
|
|
return new Promise((resolve, reject) => {
|
|
const _sql = "SELECT * FROM `brands`";
|
|
try {
|
|
resolve(handler.query(_sql) as unknown as Array<IDbBrand>);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Fetches a category by its Id
|
|
*
|
|
* @param {MysqlHandler} handler - The instance of the MysqlHandler used for executing the query
|
|
* @param {string} brandId - The Id of the category to be fetched
|
|
* @return {Promise<IDbBrand>} - A promise that, when resolved, returns the category matching the Id
|
|
* @throws {Error} - If an error occurs during execution
|
|
* @throws {string} - If brandId is undefined or invalid
|
|
*/
|
|
getById(handler: MysqlHandler, brandId: string): Promise<IDbBrand> {
|
|
return new Promise((resolve, reject) => {
|
|
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];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbBrand);
|
|
} catch (err: unknown) {
|
|
//TODO Reject with null and logger error
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Retrieves a brand from the database by its slug.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used to connect to the database.
|
|
* @param {string} brandSlug - The slug of the brand to retrieve.
|
|
* @returns {Promise<IDbBrand>} - A promise that resolves with the brand object if found, or rejects with an error message.
|
|
*/
|
|
getBySlug(handler: MysqlHandler, brandSlug: string): Promise<IDbBrand> {
|
|
return new Promise((resolve, reject) => {
|
|
if (!brandSlug) return reject('slug is undefined')
|
|
const _sql = "SELECT * FROM `brands` WHERE `slug_name` = ?";
|
|
const _values = [brandSlug];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbBrand);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Deletes a brand from the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The database handler object.
|
|
* @param {string} brandId - The ID of the brand to delete.
|
|
* @returns {Promise<IDbStatusResult>} A promise that resolves to the database status result.
|
|
* @throws {Error} If an error occurs while deleting the brand.
|
|
*/
|
|
delete(handler: MysqlHandler, brandId: string): Promise<IDbStatusResult> {
|
|
//TODO check if has models linked before actions
|
|
return new Promise((resolve, reject) => {
|
|
if (!brandId) return reject('Id is undefined');
|
|
if (brandId.length !== 36) return reject('Id invalid');
|
|
const _sql = "DELETE FROM `brands` WHERE `id` = ?";
|
|
const _values = [brandId];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
}
|
|
|
|
//TODO Get models of the brand
|
|
},
|
|
Model: {
|
|
|
|
/**
|
|
* Retrieves all records from the 'models' table.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used to execute the query.
|
|
* @return {Promise<Array<IDbModel>>} - A promise that resolves to an array of IDbModel objects representing the retrieved records from the 'models' table.
|
|
* @throws {Error} - If there is an error executing the query.
|
|
*/
|
|
getAll(handler: MysqlHandler): Promise<Array<IDbModel>> {
|
|
return new Promise((resolve, reject) => {
|
|
const _sql = "SELECT * FROM `models`";
|
|
try {
|
|
resolve(handler.query(_sql) as unknown as Array<IDbModel>);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Retrieves a database model by slug from a given MySQL handler.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler instance.
|
|
* @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, modelSlug: string): Promise<IDbModel> {
|
|
return new Promise((resolve, reject) => {
|
|
const _sql = "SELECT * FROM `models` WHERE `slug_name` = ?";
|
|
const _values = [modelSlug];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbModel);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* 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` = ?";
|
|
const _values = [modelId];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbModel);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Inserts a record into the `models` table.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object.
|
|
* @param {IDbModel} data - The data object containing the record properties.
|
|
* @throws {string} - Throws an error message if the id is undefined or invalid.
|
|
* @returns {Promise<IDbStatusResult>} - A promise that resolves to the status result of the insert operation.
|
|
*/
|
|
insert(handler: MysqlHandler, data: IDbModel): Promise<IDbStatusResult> {
|
|
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 `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,
|
|
data.brand_id,
|
|
data.category_id,
|
|
data.image_blob,
|
|
data.is_trending,
|
|
data.base_price,
|
|
data.id
|
|
]
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult)
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
//TODO get linked vehicles
|
|
|
|
/**
|
|
* Updates a database model in the "models" table.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object.
|
|
* @param {IDbModel} data - The data object containing the updated model details.
|
|
* @return {Promise<IDbStatusResult>} - A promise that resolves to the status result of the update operation.
|
|
* @throws {Error} - If an error occurs during the update process.
|
|
*/
|
|
update(handler: MysqlHandler, data: IDbModel): Promise<IDbStatusResult> {
|
|
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}
|
|
${data.brand_id ? "`brand_id` = ?," : null}
|
|
${data.category_id ? "`category_id` = ?," : null}
|
|
${data.image_blob ? "`image_blob` = ?," : null}
|
|
${data.is_trending ? "`is_trending` = ?," : null}
|
|
${data.base_price ? "`base_price` = ?," : null}`
|
|
|
|
const _values = [
|
|
data.slug_name,
|
|
data.display_name,
|
|
data.brand_id,
|
|
data.category_id,
|
|
data.image_blob,
|
|
data.is_trending,
|
|
data.base_price,
|
|
data.id
|
|
]
|
|
const _sql = `UPDATE "models" SET ${_template} WHERE 'id' = ?`;
|
|
return resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Deletes a model from the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object.
|
|
* @param {string} modelId - The ID of the model to delete.
|
|
* @returns {Promise<IDbStatusResult>} A promise that resolves to the result of the delete operation.
|
|
* @throws {Error} If an error occurs during the delete operation.
|
|
*/
|
|
delete(handler: MysqlHandler, modelId: string): Promise<IDbStatusResult> {
|
|
//TODO check if has models linked before actions
|
|
return new Promise((resolve, reject) => {
|
|
if (!modelId) return reject('Id is undefined');
|
|
if (modelId.length !== 36) return reject('Id invalid');
|
|
const _sql = "DELETE FROM `models` WHERE `id` = ?";
|
|
const _values = [modelId];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
}
|
|
},
|
|
//ToTest
|
|
Vehicle: {
|
|
/**
|
|
* Inserts a new record into the `vehicles` table.
|
|
* @param {MysqlHandler} handler - The MySQL handler object.
|
|
* @param {IDbVehicle} data - The vehicle data to be inserted.
|
|
* @throws Throws an error if the provided `data` object does not contain the `id` property or if the `id` is not a valid UUID.
|
|
* @returns {Promise<IDbStatusResult>} A promise that resolves to the result of the insert operation.
|
|
*/
|
|
insert(handler: MysqlHandler, data: IDbVehicle): Promise<IDbStatusResult> {
|
|
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 `vehicles`(`model_id`, `plate_number`, `odometer`, `health_state`, `id`) VALUES (?, ?, ?, ?, ?)"
|
|
const _values = [
|
|
data.model_id,
|
|
data.plate_number,
|
|
data.odometer,
|
|
data.health_state,
|
|
data.id
|
|
]
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult)
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Update a vehicle record in the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object for executing the SQL query.
|
|
* @param {IDbVehicle} data - The updated vehicle data.
|
|
* @throws {string} Throws an error if the id is undefined or invalid.
|
|
* @returns {Promise<IDbStatusResult>} Returns a Promise that resolves to the status result of the update operation.
|
|
*/
|
|
update(handler: MysqlHandler, data: IDbVehicle): Promise<IDbStatusResult> {
|
|
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.model_id ? "`model_id` = ?," : null}
|
|
${data.plate_number ? "`plate_number` = ?," : null}
|
|
${data.odometer ? "`odometer` = ?," : null}
|
|
${data.health_state ? "`health_state` = ?," : null}`
|
|
|
|
const _values = [
|
|
data.model_id,
|
|
data.plate_number,
|
|
data.odometer,
|
|
data.health_state,
|
|
data.id
|
|
]
|
|
const _sql = `UPDATE "vehicles" SET ${_template} WHERE 'id' = ?`;
|
|
return resolve(handler.execute(_sql, _values) as unknown as IDbStatusResult);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Retrieves a vehicle from the database by its ID.
|
|
*
|
|
* @param {MysqlHandler} handler - The instance of the MySQL handler.
|
|
* @param {string} vehicleId - The ID of the vehicle to retrieve.
|
|
* @returns {Promise<IDbVehicle>} - A promise that resolves to the retrieved vehicle.
|
|
* @throws {Error} - If an error occurs while retrieving the vehicle.
|
|
*/
|
|
getById(handler: MysqlHandler, vehicleId: string): Promise<IDbVehicle> {
|
|
return new Promise((resolve, reject) => {
|
|
if (!vehicleId) return reject('Id is undefined');
|
|
if (vehicleId.length !== 36) return reject('Id invalid');
|
|
const _sql = "SELECT * FROM `vehicles` WHERE `id` = ?";
|
|
const _values = [vehicleId];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbVehicle);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Retrieves all vehicles from the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler for executing queries.
|
|
* @return {Promise<Array<IDbVehicle>>} A promise that resolves with an array of database vehicles.
|
|
* @throws {Error} If an error occurs during the database query.
|
|
*/
|
|
getAll(handler: MysqlHandler): Promise<Array<IDbVehicle>> {
|
|
return new Promise((resolve, reject) => {
|
|
const _sql = "SELECT * FROM `models`";
|
|
try {
|
|
resolve(handler.query(_sql) as unknown as Array<IDbVehicle>);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Retrieves the available vehicles from the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used to execute the query.
|
|
* @returns {Promise<Array<IDbVehicle>>} A promise that resolves to an array of available vehicles.
|
|
* @throws {Error} If an error occurs while executing the query.
|
|
*/
|
|
getAvailable(handler: MysqlHandler): Promise<Array<IDbVehicle>> {
|
|
return new Promise((resolve, reject) => {
|
|
const _sql = "SELECT * FROM `vehicles` WERE `isAvailable` = 1";
|
|
try {
|
|
resolve(handler.query(_sql) as unknown as Array<IDbVehicle>);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
}
|
|
},
|
|
Category: {
|
|
/**
|
|
* Inserts a category into the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object.
|
|
* @param {IDbCategory} data - The category data to be inserted.
|
|
* @return {Promise<unknown>} - A promise that resolves if the insertion is successful, and rejects with an error if it fails.
|
|
*/
|
|
insert(handler: MysqlHandler, data: IDbCategory): 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 `categories`(`id`,`slug_name`, `display_name`) VALUES (?, ?, ?)"
|
|
const _values = [
|
|
data.id,
|
|
data.slug_name,
|
|
data.display_name
|
|
]
|
|
try {
|
|
resolve(handler.execute(_sql, _values))
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Updates a category in the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler instance.
|
|
* @param {IDbCategory} data - The category data to update.
|
|
* @returns {Promise<number>} - A promise that resolves with the number of affected rows in the database.
|
|
* @throws {Error} - If an error occurs during execution.
|
|
*/
|
|
update(handler: MysqlHandler, data: IDbCategory): Promise<unknown> {
|
|
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) as unknown as number);
|
|
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
/**
|
|
* Retrieves all categories from the database.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler used for executing the query.
|
|
*
|
|
* @return {Promise<Array<IDbCategory>>} - A promise that resolves with an array of category objects from the database.
|
|
* - The category objects are of type IDbCategory.
|
|
* - If an error occurs, the promise will be rejected with an Error object.
|
|
*/
|
|
getAll(handler: MysqlHandler): Promise<Array<IDbCategory>> {
|
|
return new Promise((resolve, reject) => {
|
|
const _sql = "SELECT * FROM `categories`";
|
|
try {
|
|
resolve(handler.query(_sql) as unknown as Array<IDbCategory>);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
/**
|
|
* Retrieves a category from the database by its slug.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object to execute the query.
|
|
* @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, categorySlug: string): Promise<IDbCategory> {
|
|
return new Promise((resolve, reject) => {
|
|
if (!categorySlug) return reject('slug is undefined')
|
|
const _sql = "SELECT * FROM `categories` WHERE `slug_name` = ?";
|
|
const _values = [categorySlug];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbCategory);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
},
|
|
/**
|
|
* Retrieves a category from the database by its ID.
|
|
*
|
|
* @param {MysqlHandler} handler - The MySQL handler object used for execution.
|
|
* @param {string} categoryId - The ID of the category to retrieve.
|
|
* @returns {Promise<IDbCategory>} - A Promise that resolves with the category object.
|
|
* @throws {Error} - If an error occurs during execution.
|
|
*/
|
|
getById(handler: MysqlHandler, categoryId: string): Promise<IDbCategory> {
|
|
return new Promise((resolve, reject) => {
|
|
if (!categoryId) return reject('slug is undefined')
|
|
if (categoryId.length !== 36) return reject('Id invalid');
|
|
const _sql = "SELECT * FROM `categories` WHERE `id` = ?";
|
|
const _values = [categoryId];
|
|
try {
|
|
resolve(handler.execute(_sql, _values) as unknown as IDbCategory);
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
});
|
|
},
|
|
delete(handler: MysqlHandler, categoryId: string) {
|
|
return new Promise((resolve, reject) => {
|
|
if (!categoryId) return reject('Id is undefined');
|
|
if (categoryId.length !== 36) return reject('Id invalid');
|
|
const _sql = "DELETE FROM `categories` WHERE `id` = ?";
|
|
const _values = [categoryId];
|
|
try {
|
|
resolve(handler.execute(_sql, _values));
|
|
} catch (err: unknown) {
|
|
reject(err as Error);
|
|
}
|
|
})
|
|
}
|
|
//TODO Get models in category
|
|
}
|
|
}
|
|
|
|
export default MySqlService |