Merge pull request #20 from kfnawaz/feat/warehouse-getall
Feat/warehouse getall
This commit is contained in:
@@ -2,6 +2,14 @@ const Warehouse = require("../models/Warehouse");
|
|||||||
const mongoose = require("mongoose");
|
const mongoose = require("mongoose");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
getAllWarehouses: async (req, res, next) => {
|
||||||
|
try {
|
||||||
|
const warehouses = await Warehouse.find();
|
||||||
|
res.send({ success: true, data: warehouses });
|
||||||
|
} catch (error) {
|
||||||
|
next(error);
|
||||||
|
}
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* Gets the warehouse data by `id`
|
* Gets the warehouse data by `id`
|
||||||
*/
|
*/
|
||||||
@@ -9,17 +17,17 @@ module.exports = {
|
|||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
if (!id) {
|
if (!id) {
|
||||||
res.status(400).send("Missing id param");
|
res.status(400).send({ success: false, message: "Missing id param" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const warehouseData = await Warehouse.findById(id);
|
const warehouseData = await Warehouse.findById(id);
|
||||||
if (!warehouseData) {
|
if (!warehouseData) {
|
||||||
res.status(404);
|
res.status(404).send({ success: false, message: "not found" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.send(warehouseData);
|
res.send({ success: true, data: warehouseData });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
@@ -46,10 +54,10 @@ module.exports = {
|
|||||||
|
|
||||||
await warehouseData.save();
|
await warehouseData.save();
|
||||||
if (!warehouseData) {
|
if (!warehouseData) {
|
||||||
res.status(404);
|
res.status(404).send({ success: false, message: "not found" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
res.send(warehouseData);
|
res.send({ success: true, message: warehouseData });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
@@ -84,21 +92,21 @@ module.exports = {
|
|||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
|
|
||||||
if (!id) {
|
if (!id) {
|
||||||
res.status(400).send("Missing id param");
|
res.status(400).send({ success: false, message: "Missing ID param" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name, address, specs, company_id } = req.body;
|
const { name, address, specs, company_id } = req.body;
|
||||||
|
|
||||||
if (!(name || address || specs || company_id)) {
|
if (!(name || address || specs || company_id)) {
|
||||||
res.status(400).send("Missing data in body");
|
res.status(400).send({ success: false, message: "Missing data in body" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const warehouseData = await Warehouse.findById(id);
|
const warehouseData = await Warehouse.findById(id);
|
||||||
if (!warehouseData) {
|
if (!warehouseData) {
|
||||||
res.status(404);
|
res.status(404).send({ success: false, message: "not found" });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,7 +116,7 @@ module.exports = {
|
|||||||
if (company_id) warehouseData.company_id = mongoose.Types.ObjectId(company_id);
|
if (company_id) warehouseData.company_id = mongoose.Types.ObjectId(company_id);
|
||||||
|
|
||||||
await warehouseData.save();
|
await warehouseData.save();
|
||||||
res.send(warehouseData);
|
res.send({ success: true, data: warehouseData });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
next(error);
|
next(error);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,11 @@ const router = require("express").Router();
|
|||||||
const upload = require("../middleware/fileUpload");
|
const upload = require("../middleware/fileUpload");
|
||||||
const controller = require("./warehouse.controller");
|
const controller = require("./warehouse.controller");
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @route /warehouse/get-all
|
||||||
|
*/
|
||||||
|
router.get("/get-all", controller.getAllWarehouses);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @route /warehouse/:id
|
* @route /warehouse/:id
|
||||||
*/
|
*/
|
||||||
@@ -15,11 +20,7 @@ router.post("/", controller.createWarehouse);
|
|||||||
/**
|
/**
|
||||||
* @route /warehouse/add-image
|
* @route /warehouse/add-image
|
||||||
*/
|
*/
|
||||||
router.post(
|
router.post("/add-image/:id", upload.single("warehouse-image"), controller.addWarehouseImage);
|
||||||
"/add-image/:id",
|
|
||||||
upload.single("warehouse-image"),
|
|
||||||
controller.addWarehouseImage
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @route /warehouse/
|
* @route /warehouse/
|
||||||
|
|||||||
@@ -13,8 +13,6 @@ const {
|
|||||||
const db = require("./config/db/connect");
|
const db = require("./config/db/connect");
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
console.log("Connecting to MongoDB ...");
|
|
||||||
|
|
||||||
await db.connect();
|
await db.connect();
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
|
|||||||
Reference in New Issue
Block a user