feat(controllers): implement vehicle controller

Added a new `vehicle.controller.ts` file under controllers. It includes multiple functions related to vehicle management such as `createVehicle`, `updateVehicle`, `getAllVehicle`, `getVehicleById`, `getAvailableVehicle`, and `deleteVehicle`. These functions handle the creation, updates, retrieval, and deletion of a vehicle respectively. With these additions, the application now has the ability to manage vehicles through the `VehicleController` module.

Issue: #22
Signed-off-by: Mathis <yidhra@tuta.io>
This commit is contained in:
Mathis H (Avnyr) 2024-05-03 11:17:23 +02:00
parent c9ca39ddfa
commit 170b9a5693
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -0,0 +1,174 @@
import type { IDbVehicle } from "@interfaces/database/IDbVehicle";
import VehicleService from "@services/vehicle.service";
import { isDebugMode } from "@utils/debugState";
import type { Request, Response } from "express";
import { Logger } from "tslog";
//import {validationResult} from "express-validator";
const logger = new Logger({
name: "VehicleController",
});
/**
* Creates a new vehicle in the database.
*
* @param {Request} req - The request object containing the vehicle details in the body.
* @param {Response} res - The response object used to send the result of the operation.
* @returns {Promise<Response>} The response with the result of the operation.
*/
async function createVehicle(req: Request, res: Response): Promise<Response> {
const body: IDbVehicle = req.body;
const createResult = await VehicleService.create({
plate_number: `${body.plate_number}`,
model_id: `${body.plate_number}`,
odometer: Number.parseInt(`${body.odometer}`),
health_state: Number.parseInt(`${body.health_state}`),
});
if (!createResult) {
logger.error("Failed to create vehicle");
return res.status(500).json({
error: "Failed to create vehicle",
});
}
logger.info(`Vehicle created successfully ! (${body.plate_number})`);
return res.status(201).json({
message: "Vehicle created successfully",
});
}
/**
* Updates a vehicle in the database.
*
* @param {Request} req - The request object containing the vehicle data in the request body and the vehicle ID in the request parameters.
* @param {Response} res - The response object used to send the result of the update operation.
*
* @return {Promise<Response>} A promise that resolves to the response object with a status and a JSON body indicating the result of the update operation.
*/
async function updateVehicle(req: Request, res: Response): Promise<Response> {
const body: IDbVehicle = req.body;
const vehicleId = req.params["vehicleId"];
if (!vehicleId || vehicleId.length !== 36) {
if (isDebugMode()) logger.error("Vehicle ID is missing");
return res.status(400).json({
error: "Vehicle ID is missing or not valid",
});
}
const updateResult = await VehicleService.update({
plate_number: `${body.plate_number}`,
model_id: `${body.plate_number}`,
odometer: Number.parseInt(`${body.odometer}`),
health_state: Number.parseInt(`${body.health_state}`),
});
if (!updateResult) {
logger.error("Failed to update vehicle");
return res.status(500).json({
error: "Failed to update vehicle",
});
}
logger.info(`Vehicle updated successfully ! (${body.plate_number})`);
return res.status(200).json({
message: "Vehicle updated successfully",
});
}
/**
* Retrieves all vehicles from the vehicle service.
*
* @param {Response} res - The response object from the client.
* @returns {Promise<Response>} A promise that resolves to a response object containing the result of the operation. The result is a JSON object containing all vehicles.
*/
async function getAllVehicle(res: Response): Promise<Response> {
const getAllVehicleResult = await VehicleService.getAll();
if (!getAllVehicleResult) {
logger.error("Failed to get all vehicles");
return res.status(500).json({
error: "Failed to get all vehicles",
});
}
return res.status(200).json(getAllVehicleResult);
}
/**
* Retrieves the available vehicles from the VehicleService
*
* @param {Response} res - The Response object to send the result to
* @returns {Promise<Response<any, Record<string, any>>>} - A promise that resolves to a Response object containing the available vehicles or an error message
*/
async function getAvailableVehicle(res: Response): Promise<Response<any, Record<string, any>>> {
const getAvailableVehicleResult = await VehicleService.getAvailable();
if (!getAvailableVehicleResult) {
logger.error("Failed to get available vehicles");
return res.status(500).json({
error: "Failed to get available vehicles",
});
}
return res.status(200).json(getAvailableVehicleResult);
}
/**
* Retrieves a vehicle by its ID.
*
* @param {Request} req - The request object containing the vehicle ID.
* @param {Response} res - The response object used to send the result.
*
* @return {Promise<Response>} A promise that resolves to the result of the retrieval operation.
*/
async function getVehicleById(req: Request, res: Response): Promise<Response> {
const vehicleId = req.params["vehicleId"];
if (!vehicleId || vehicleId.length !== 36) {
if (isDebugMode()) logger.error("Vehicle ID is missing or not valid");
return res.status(400).json({
error: "Vehicle ID is missing or not valid",
});
}
const getVehicleResult = await VehicleService.getById(vehicleId);
if (!getVehicleResult) {
logger.error(`Failed to get vehicle by ID: ${vehicleId}`);
return res.status(500).json({
error: `Failed to get vehicle by ID: ${vehicleId}`,
});
}
return res.status(200).json(getVehicleResult);
}
/**
* Deletes a vehicle.
*
* @param {Object} req - The request object.
* @param {Object} res - The response object.
*
* @return {Promise<Response>} A promise that resolves to the response JSON object.
*/
async function deleteVehicle(req: Request, res: Response): Promise<Response> {
const vehicleId = req.params["vehicleId"];
if (!vehicleId || vehicleId.length !== 36) {
if (isDebugMode()) logger.error("Vehicle ID is missing or not valid");
return res.status(400).json({
error: "Vehicle ID is missing or not valid",
});
}
const deleteResult = await VehicleService.delete(vehicleId);
if (!deleteResult) {
logger.error(`Failed to delete vehicle with ID: ${vehicleId}`);
return res.status(500).json({
error: `Failed to delete vehicle with ID: ${vehicleId}`,
});
}
logger.info(`Vehicle deleted successfully ! (ID: ${vehicleId})`);
return res.status(200).json({
message: "Vehicle deleted successfully",
});
}
if (isDebugMode()) logger.debug("\nController loaded.");
const VehicleController = {
create: createVehicle,
update: updateVehicle,
getAll: getAllVehicle,
delete: deleteVehicle,
getById: getVehicleById,
getAvailable: getAvailableVehicle,
};
export default VehicleController;