Compare commits

...

4 Commits

Author SHA1 Message Date
8fe0fa57d8
feat(services): add Vehicle methods to mysql service
This commit introduces several new Vehicle methods to the mysql service (insert, update, getBySlug, getAll, getAvailable, getDue). These methods are scaffolded but not fully implemented, with `insert` being the most complete at this stage.

Issue: #20
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-29 11:31:50 +02:00
2796b514eb
feat(routes): add BrandController methods to catalog routes
The commit introduces BrandController methods to the catalog router. Specifically, it implements create, getAll, getBySlug, update, and delete functionalities for brand routes. This update enhances brand management within the catalog.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-29 11:30:59 +02:00
0053c0ce19
feat(interfaces): add IDbVehicle interface
Add a new interface called `IDbVehicle` in the interfaces module. The `IDbVehicle` interface includes properties such as `id`, `plate_number`, `model_id`, `odometer`, and `health_state` for defining a vehicle object in our database.

Issue: #20
Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-29 11:30:13 +02:00
041e77efcd
feat(controllers): include request param to getAll functions
Changes include the inclusion of the request parameter (`_req`) in the `getAllBrand` and `getAllCategory` functions and some code format adjustment in `category.controller.ts`. This addition allows more flexibility in handling the request if needed in the future. The `total` field has also been added to the category output to conveniently provide category count.

Signed-off-by: Mathis <yidhra@tuta.io>
2024-04-29 11:29:29 +02:00
5 changed files with 83 additions and 12 deletions

View File

@ -92,12 +92,13 @@ async function getBySlugBrand(req: Request, res: Response): Promise<Response> {
}
/**
* Retrieves all brands and sends the response.
* Retrieves all brands.
*
* @param {Request} _req - The request object.
* @param {Response} res - The response object.
* @returns {Promise<Response>} - A Promise that resolves with the response object.
* @returns {Promise<Response>} - A promise with the response object.
*/
async function getAllBrand(res: Response): Promise<Response> {
async function getAllBrand(_req: Request, res: Response): Promise<Response> {
const brands = await BrandService.getAll();
if (!brands) {
logger.error("Failed to retrieve brands");

View File

@ -97,24 +97,27 @@ async function deleteCategory(req: Request, res: Response): Promise<Response> {
/**
* Retrieves all categories.
*
* @param _req
* @param {Response} res - The response object.
* @return {Promise<Response>} - A promise that resolves to the response object.
*/
async function getAllCategory(res: Response): Promise<Response> {
async function getAllCategory(_req: Request, res: Response): Promise<Response> {
const categories = await CategoryService.getAll();
if (!categories) {
logger.error("Failed to get categories");
return res.status(500).json({ error: "Failed to get categories" });
}
logger.info("Categories retrieved successfully");
//ToTest categories output type
return res.status(200).json({
return res
.status(200)
.json({
iat: Date.now(),
categories: categories.map((category: IDbCategory) => ({
id: category.id,
display_name: category.display_name,
slug_name: category.slug_name
}))
})),
total: categories.length
});
}

View File

@ -0,0 +1,7 @@
export interface IDbVehicle {
id?: string;
plate_number: string;
model_id: string;
odometer: number;
health_state: number;
}

View File

@ -3,6 +3,7 @@ import AdminGuard from "@validators/AdminGuard";
import UserGuard from "@validators/UserGuard";
import CategoryController from "@controllers/category.controller";
import ModelController from "@controllers/model.controller";
import BrandController from "@controllers/brand.controller";
const CatalogRouter: Router = express.Router();
@ -33,11 +34,11 @@ CatalogRouter.route('/category/:categorySlug')
//-- BRAND >>
CatalogRouter.route('/brand/new').post(AdminGuard)
CatalogRouter.route('/brand/all').get()
CatalogRouter.route('/brand/new').post(AdminGuard, BrandController.create)
CatalogRouter.route('/brand/all').get(BrandController.getAll)
CatalogRouter.route('/brand/:brandSlug')
.get(UserGuard)
.patch(AdminGuard)
.delete(AdminGuard)
.get(UserGuard, BrandController.getBySlug)
.patch(AdminGuard, BrandController.update)
.delete(AdminGuard, BrandController.delete)
export default CatalogRouter;

View File

@ -6,6 +6,7 @@ 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 = {
@ -580,6 +581,64 @@ const MySqlService = {
})
}
},
Vehicle: {
insert(handler: MysqlHandler, data: IDbVehicle) {
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);
}
})
},
//TODO update by id
update(handler: MysqlHandler, data: IDbVehicle) {
return new Promise((resolve, reject) => {
})
},
//TODO get by slug
getBySlug(handler: MysqlHandler, data: IDbVehicle) {
return new Promise((resolve, reject) => {
})
},
//TODO get all
getAll(handler: MysqlHandler, data: IDbVehicle) {
return new Promise((resolve, reject) => {
})
},
//TODO get available
getAvailable(handler: MysqlHandler, data: IDbVehicle) {
return new Promise((resolve, reject) => {
})
},
//TODO get out of due date
getDue(handler: MysqlHandler, data: IDbVehicle) {
return new Promise((resolve, reject) => {
})
}
},
Category: {
/**
* Inserts a category into the database.