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:
@@ -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);
|
||||
}
|
||||
});
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user