From 3133cacb7efaea18487bc6c3392e124f15af2a16 Mon Sep 17 00:00:00 2001 From: karol Date: Thu, 2 Jul 2026 22:26:31 +0000 Subject: [PATCH] =?UTF-8?q?Actualizaci=C3=B3n=20CRUD=20m=C3=B3dulo=20preci?= =?UTF-8?q?o=20de=20combustibles?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fuel_prices/fuelPrices.controller.ts | 101 ++++++++++++++---- .../fuel_prices/fuelPrices.interface.ts | 10 +- .../modules/fuel_prices/fuelPrices.model.ts | 14 +-- .../modules/fuel_prices/fuelPrices.routes.ts | 6 +- 4 files changed, 96 insertions(+), 35 deletions(-) diff --git a/backend/src/modules/fuel_prices/fuelPrices.controller.ts b/backend/src/modules/fuel_prices/fuelPrices.controller.ts index 3ec3727..75b5ad0 100644 --- a/backend/src/modules/fuel_prices/fuelPrices.controller.ts +++ b/backend/src/modules/fuel_prices/fuelPrices.controller.ts @@ -9,31 +9,61 @@ export class FuelPriceController { req:Request, res:Response ){ - const fuelPrices = await fuelPriceService.getAll(); - - res.json(fuelPrices); + try{ + const fuelPrices = await fuelPriceService.getAll(); + + res.json(fuelPrices); + + }catch(error){ + res.status(500).json({ + message:"Error al obtener precio de combustible", + error + }); + } } async getById( req:Request, res:Response ){ - const fuelPrice = await fuelPriceService.getById( - req.params.id as string - ); + try{ + const fuelPrice = await fuelPriceService.getById( + req.params.id as string + ); - res.json(fuelPrice); + if(!fuelPrice){ + return res.status(404).json({ + message:"Precio de combustible no encontrado" + }); + } + + res.json(fuelPrice); + }catch(error){ + res.status(500).json({ + message:"Error al obtener precio de combustible", + error + }); + + } } async create ( req:Request, res:Response ){ - const fuelPrice = await fuelPriceService.create( - req.body - ); - res.status(201).json(fuelPrice); + try{ + const fuelPrice = await fuelPriceService.create( + req.body + ); + + res.status(201).json(fuelPrice); + } catch(error){ + res.status(500).json({ + message:"Error al crear precio de combustible", + error + }) + } } async update( @@ -41,24 +71,51 @@ export class FuelPriceController { res:Response ){ - const fuelPrice = await fuelPriceService.update( - req.params.id as string, - req.body - ) + try{ + const fuelPrice = await fuelPriceService.update( + req.params.id as string, + req.body + ); + + if(!fuelPrice){ + return res.status(404).json({ + message:"Precio de combustible no encontrado" + }); + } - res.json(fuelPrice); + res.status(200).json(fuelPrice); + }catch(error){ + res.status(500).json({ + message:"Error al actualizar precio de combustible", + error + }); + } } async delete ( req:Request, res:Response ){ - const fuelPrice = await fuelPriceService.delete( - req.params.id as string - ) + try{ + const fuelPrice = await fuelPriceService.delete( + req.params.id as string + ) - res.json({ - message:"Precio eliminado"} - ); + if(!fuelPrice){ + return res.status(404).json({ + message:"Precio de combustible no encontrado" + }); + } + + res.json({ + message:"Precio eliminado"} + ); + }catch(error){ + res.status(500).json({ + message:"Error al eliminar precio de combustible", + error + }) + + } } } diff --git a/backend/src/modules/fuel_prices/fuelPrices.interface.ts b/backend/src/modules/fuel_prices/fuelPrices.interface.ts index dd5dba5..df830c8 100644 --- a/backend/src/modules/fuel_prices/fuelPrices.interface.ts +++ b/backend/src/modules/fuel_prices/fuelPrices.interface.ts @@ -1,12 +1,16 @@ -import mongoose, { mongo, ObjectId } from "mongoose"; +import { Types } from "mongoose"; export interface IFuelPrice { - fuelTypeId:ObjectId; + fuelTypeId:Types.ObjectId; pricePerGallon:number; + effectiveFrom:Date; + effectiveTo:Date; + active:boolean; - createBy:ObjectId; + + createdBy:Types.ObjectId; } \ No newline at end of file diff --git a/backend/src/modules/fuel_prices/fuelPrices.model.ts b/backend/src/modules/fuel_prices/fuelPrices.model.ts index c3b8755..42cf2d1 100644 --- a/backend/src/modules/fuel_prices/fuelPrices.model.ts +++ b/backend/src/modules/fuel_prices/fuelPrices.model.ts @@ -1,9 +1,9 @@ -import mongoose, { mongo } from "mongoose"; +import mongoose from "mongoose"; const fuelPriceSchema = new mongoose.Schema({ fuelTypeId:{ - type:mongoose.Types.ObjectId, + type:mongoose.Schema.Types.ObjectId, ref:"FuelType", required:true, @@ -14,11 +14,11 @@ const fuelPriceSchema = new mongoose.Schema({ required:true }, - effetiveFrom:{ + effectiveFrom:{ type:Date, }, - effetiveTo:{ + effectiveTo:{ type:Date, }, @@ -27,8 +27,8 @@ const fuelPriceSchema = new mongoose.Schema({ default:true }, - createBy:{ - type:mongoose.Types.ObjectId, + createdBy:{ + type:mongoose.Schema.Types.ObjectId, ref:"User", required:true } @@ -38,4 +38,4 @@ const fuelPriceSchema = new mongoose.Schema({ } ); -export default mongoose.model("FuelPrices", fuelPriceSchema) \ No newline at end of file +export default mongoose.model("FuelPrices", fuelPriceSchema); \ No newline at end of file diff --git a/backend/src/modules/fuel_prices/fuelPrices.routes.ts b/backend/src/modules/fuel_prices/fuelPrices.routes.ts index 2e46918..57a5351 100644 --- a/backend/src/modules/fuel_prices/fuelPrices.routes.ts +++ b/backend/src/modules/fuel_prices/fuelPrices.routes.ts @@ -8,13 +8,13 @@ const controller = new FuelPriceController(); fuelPriceRoute.get("/", authMiddleware, controller.getAll); -fuelPriceRoute.get("/id", authMiddleware, controller.getById); +fuelPriceRoute.get("/:id", authMiddleware, controller.getById); fuelPriceRoute.post("/", authMiddleware, controller.create); -fuelPriceRoute.put("/id", authMiddleware, controller.update); +fuelPriceRoute.put("/:id", authMiddleware, controller.update); -fuelPriceRoute.delete("/id", authMiddleware, controller.delete); +fuelPriceRoute.delete("/:id", authMiddleware, controller.delete); export default fuelPriceRoute; \ No newline at end of file