Actualizar CRUD módulo bombas de combustible
This commit is contained in:
@ -9,55 +9,114 @@ export class FuelPumpController {
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try{
|
||||
const fuelPumps = await fuelPumpService.getAll();
|
||||
|
||||
res.json(fuelPumps);
|
||||
}catch(error){
|
||||
res.status(500).json({
|
||||
message:"Error al obtener bomba de combustible",
|
||||
error
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async getById(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try{
|
||||
const fuelPump = await fuelPumpService.getById(
|
||||
req.params.id as string
|
||||
);
|
||||
|
||||
if(!fuelPump){
|
||||
return res.status(404).json({
|
||||
message:"Bomba combustible no encontrada"
|
||||
});
|
||||
}
|
||||
|
||||
res.json(fuelPump)
|
||||
|
||||
}catch(error){
|
||||
res.status(500).json({
|
||||
message:"Error al obtener bomba de combustible",
|
||||
error
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async create(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try{
|
||||
const fuelPump = await fuelPumpService.create(
|
||||
req.body
|
||||
);
|
||||
|
||||
res.status(201).json(fuelPump);
|
||||
|
||||
}catch(error){
|
||||
res.status(500).json({
|
||||
message:"Error al crear bomba de combustible",
|
||||
error
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
async update(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
|
||||
try{
|
||||
const fuelPump = await fuelPumpService.update(
|
||||
req.params.id as string,
|
||||
req.body
|
||||
);
|
||||
|
||||
res.json(fuelPump);
|
||||
if(!fuelPump){
|
||||
return res.status(404).json({
|
||||
message:"Bomba de combustible no encontrada"
|
||||
})
|
||||
}
|
||||
|
||||
res.status(200).json(fuelPump);
|
||||
}catch(error){
|
||||
res.status(500).json({
|
||||
message:"Error al actualizar bomba de combustible",
|
||||
error
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async delete(
|
||||
req:Request,
|
||||
res:Response
|
||||
){
|
||||
try{
|
||||
const fuelPump = await fuelPumpService.delete(
|
||||
req.params.id as string
|
||||
)
|
||||
);
|
||||
|
||||
if(!fuelPump){
|
||||
return res.status(404).json({
|
||||
message:"Bomba de combustible no encontrada"
|
||||
})
|
||||
}
|
||||
|
||||
res.json({
|
||||
message:"Bomba de combustible eliminada"
|
||||
message:"Bomba de combustible eliminada correctamente"
|
||||
});
|
||||
|
||||
}catch(error){
|
||||
res.status(500).json({
|
||||
message:"Error al eliminar bomba de combustible",
|
||||
error
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,10 @@
|
||||
import { ObjectId } from "mongoose";
|
||||
import { Types } from "mongoose";
|
||||
|
||||
export interface IFuelPumps {
|
||||
export interface IFuelPump {
|
||||
|
||||
pumpName: string;
|
||||
fuelTypeId:ObjectId;
|
||||
|
||||
fuelTypeId: Types.ObjectId;
|
||||
|
||||
active: boolean;
|
||||
}
|
||||
@ -9,7 +9,7 @@ const fuelPumpSchema = new mongoose.Schema({
|
||||
},
|
||||
|
||||
fuelTypeId:{
|
||||
type:mongoose.Types.ObjectId,
|
||||
type:mongoose.Schema.Types.ObjectId,
|
||||
ref:"FuelType",
|
||||
required:true
|
||||
},
|
||||
|
||||
@ -8,12 +8,12 @@ const controller = new FuelPumpController();
|
||||
|
||||
fuelPumpRouter.get("/", authMiddleware, controller.getAll);
|
||||
|
||||
fuelPumpRouter.get("/id", authMiddleware, controller.getById);
|
||||
fuelPumpRouter.get("/:id", authMiddleware, controller.getById);
|
||||
|
||||
fuelPumpRouter.post("/", authMiddleware, controller.create);
|
||||
|
||||
fuelPumpRouter.put("/id", authMiddleware, controller.update);
|
||||
fuelPumpRouter.put("/:id", authMiddleware, controller.update);
|
||||
|
||||
fuelPumpRouter.delete("/id", authMiddleware, controller.delete);
|
||||
fuelPumpRouter.delete("/:id", authMiddleware, controller.delete);
|
||||
|
||||
export default fuelPumpRouter;
|
||||
@ -1,21 +1,24 @@
|
||||
import fuelPumpModel from "./fuelPump.model"
|
||||
import fuelPumpModel from "./fuelPump.model";
|
||||
import { IFuelPump } from "./fuelPump.interface";
|
||||
|
||||
export class FuelPumpService {
|
||||
|
||||
async getAll() {
|
||||
return await fuelPumpModel.find();
|
||||
return await fuelPumpModel
|
||||
.find()
|
||||
.populate("fuelTypeId");
|
||||
}
|
||||
|
||||
async getById(
|
||||
id:string
|
||||
){
|
||||
return await fuelPumpModel.findById(
|
||||
id
|
||||
);
|
||||
return await fuelPumpModel.
|
||||
findById(id)
|
||||
.populate("fuelTypeId")
|
||||
}
|
||||
|
||||
async create(
|
||||
data:any
|
||||
data:IFuelPump
|
||||
){
|
||||
return await fuelPumpModel.create(
|
||||
data
|
||||
@ -24,7 +27,7 @@ export class FuelPumpService {
|
||||
|
||||
async update(
|
||||
id:string,
|
||||
data:any
|
||||
data:Partial<IFuelPump>
|
||||
){
|
||||
return await fuelPumpModel.findByIdAndUpdate(
|
||||
id,
|
||||
@ -43,4 +46,3 @@ export class FuelPumpService {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user