feat(services): add getById function to mysql.service

This commit adds a getById function to the mysql.service.ts file. The added functionality allows retrieving category data from the mysql database by id. Error handling has been included for undefined or invalid id cases.

Issue: #6
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-04-25 12:30:32 +02:00
parent ead64291df
commit 3328c5a1df
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -440,7 +440,20 @@ const MySqlService = {
reject(err as Error);
}
})
},
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);
}
});
},
}
}