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