Added: row
This commit is contained in:
@@ -5,6 +5,7 @@ const warehouseRouter = require("./warehouse.router");
|
||||
const zoneRouter = require("./zone.router");
|
||||
const areaRouter = require("./area.router");
|
||||
const bayRouter = require("./bay.router");
|
||||
const rowRouter = require("./row.router");
|
||||
|
||||
router.use("/user", userRouter);
|
||||
router.use("/company", companyRouter);
|
||||
@@ -12,6 +13,7 @@ router.use("/warehouse", warehouseRouter);
|
||||
router.use("/zone", zoneRouter);
|
||||
router.use("/area", areaRouter);
|
||||
router.use("/bay", bayRouter);
|
||||
router.use("/row", rowRouter);
|
||||
|
||||
router.get("/", (req, res) => {
|
||||
res.send("Hello world");
|
||||
|
||||
94
src/controller/row.controller.js
Normal file
94
src/controller/row.controller.js
Normal file
@@ -0,0 +1,94 @@
|
||||
const Row = require("../models/Row");
|
||||
const mongoose = require("mongoose");
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* Gets the row data by `id`
|
||||
*/
|
||||
getRowByID: async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
|
||||
if (!id) {
|
||||
res.status(400).send("Missing id param");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const rowData = await Row.findById(id);
|
||||
if (!rowData) {
|
||||
res.status(404);
|
||||
return;
|
||||
}
|
||||
req.send(rowData);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Create a row
|
||||
*/
|
||||
createRow: async (req, res, next) => {
|
||||
const { name, number, specs, area_id } = req.body;
|
||||
|
||||
if (!(name && number)) {
|
||||
res.status(400).send("Missing params param");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const rowData = new Row({
|
||||
name,
|
||||
number: parseInt(number),
|
||||
specs,
|
||||
area_id: mongoose.Types.ObjectId(area_id),
|
||||
});
|
||||
|
||||
await rowData.save();
|
||||
if (!rowData) {
|
||||
res.status(404);
|
||||
return;
|
||||
}
|
||||
req.send(rowData);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* Update a rows detail
|
||||
*/
|
||||
updateRowByID: async (req, res, next) => {
|
||||
const { id } = req.params;
|
||||
|
||||
if (!id) {
|
||||
res.status(400).send("Missing id param");
|
||||
return;
|
||||
}
|
||||
|
||||
const { name, number, specs, area_id } = req.body;
|
||||
|
||||
if (!(name || number || specs || area_id)) {
|
||||
res.status(400).send("Missing data in body");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const rowData = await Row.findById(id);
|
||||
if (!rowData) {
|
||||
res.status(404);
|
||||
return;
|
||||
}
|
||||
|
||||
if (name) rowData.name = name;
|
||||
if (number) rowData.number = parseInt(number);
|
||||
if (specs) rowData.specs = specs;
|
||||
if (area_id) rowData.area_id = mongoose.Types.ObjectId(area_id);
|
||||
|
||||
await rowData.save();
|
||||
req.send(rowData);
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
},
|
||||
};
|
||||
19
src/controller/row.router.js
Normal file
19
src/controller/row.router.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const router = require("express").Router();
|
||||
const controller = require("./row.controller");
|
||||
|
||||
/**
|
||||
* @route /row/:id
|
||||
*/
|
||||
router.get("/:id", controller.getRowByID);
|
||||
|
||||
/**
|
||||
* @route /row/
|
||||
*/
|
||||
router.post("/", controller.createRow);
|
||||
|
||||
/**
|
||||
* @route /row/
|
||||
*/
|
||||
router.patch("/:id", controller.updateRowByID);
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user