diff --git a/src/controller/area.controller.js b/src/controller/area.controller.js new file mode 100644 index 0000000..3c6e02a --- /dev/null +++ b/src/controller/area.controller.js @@ -0,0 +1,94 @@ +const Area = require("../models/Area"); +const mongoose = require("mongoose"); + +module.exports = { + /** + * Gets the area data by `id` + */ + getAreaByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const areaData = await Area.findById(id); + if (!areaData) { + res.status(404); + return; + } + req.send(areaData); + } catch (error) { + next(error); + } + }, + + /** + * Create a area + */ + createArea: async (req, res, next) => { + const { name, type, specs, zone_id } = req.body; + + if (!(name && type)) { + res.status(400).send("Missing params param"); + return; + } + + try { + const areaData = new Area({ + name, + type, + specs, + zone_id: mongoose.Types.ObjectId(zone_id), + }); + + await areaData.save(); + if (!areaData) { + res.status(404); + return; + } + req.send(areaData); + } catch (error) { + next(error); + } + }, + + /** + * Update a areas detail + */ + updateAreaByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + const { name, type, specs, zone_id } = req.body; + + if (!(name || type || specs || zone_id)) { + res.status(400).send("Missing data in body"); + return; + } + + try { + const areaData = await Area.findById(id); + if (!areaData) { + res.status(404); + return; + } + + if (name) areaData.name = name; + if (type) areaData.type = type; + if (specs) areaData.specs = specs; + if (zone_id) areaData.zone_id = mongoose.Types.ObjectId(zone_id); + + await areaData.save(); + req.send(areaData); + } catch (error) { + next(error); + } + }, +}; diff --git a/src/controller/area.router.js b/src/controller/area.router.js new file mode 100644 index 0000000..d2c0c0a --- /dev/null +++ b/src/controller/area.router.js @@ -0,0 +1,19 @@ +const router = require("express").Router(); +const controller = require("./area.controller"); + +/** + * @route /area/:id + */ +router.get("/:id", controller.getAreaByID); + +/** + * @route /area/ + */ +router.post("/", controller.createArea); + +/** + * @route /area/ + */ +router.patch("/:id", controller.updateAreaByID); + +module.exports = router; diff --git a/src/controller/bay.controller.js b/src/controller/bay.controller.js new file mode 100644 index 0000000..3168b4f --- /dev/null +++ b/src/controller/bay.controller.js @@ -0,0 +1,96 @@ +const Bay = require("../models/Bay"); +const mongoose = require("mongoose"); + +module.exports = { + /** + * Gets the bay data by `id` + */ + getBayByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const bayData = await Bay.findById(id); + if (!bayData) { + res.status(404); + return; + } + req.send(bayData); + } catch (error) { + next(error); + } + }, + + /** + * Create a bay + */ + createBay: async (req, res, next) => { + const { name, number, type, specs, row_id } = req.body; + + if (!(name && type && number)) { + res.status(400).send("Missing params param"); + return; + } + + try { + const bayData = new Bay({ + name, + number: parseInt(number), + type, + specs, + row_id: mongoose.Types.ObjectId(row_id), + }); + + await bayData.save(); + if (!bayData) { + res.status(404); + return; + } + req.send(bayData); + } catch (error) { + next(error); + } + }, + + /** + * Update a bays detail + */ + updateBayByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + const { name, number, type, specs, row_id } = req.body; + + if (!(name || number || type || specs || row_id)) { + res.status(400).send("Missing data in body"); + return; + } + + try { + const bayData = await Bay.findById(id); + if (!bayData) { + res.status(404); + return; + } + + if (name) bayData.name = name; + if (number) bayData.number = parseInt(number); + if (type) bayData.type = type; + if (specs) bayData.specs = specs; + if (row_id) bayData.row_id = mongoose.Types.ObjectId(row_id); + + await bayData.save(); + req.send(bayData); + } catch (error) { + next(error); + } + }, +}; diff --git a/src/controller/bay.router.js b/src/controller/bay.router.js new file mode 100644 index 0000000..b487363 --- /dev/null +++ b/src/controller/bay.router.js @@ -0,0 +1,19 @@ +const router = require("express").Router(); +const controller = require("./bay.controller"); + +/** + * @route /bay/:id + */ +router.get("/:id", controller.getBayByID); + +/** + * @route /bay/ + */ +router.post("/", controller.createBay); + +/** + * @route /bay/ + */ +router.patch("/:id", controller.updateBayByID); + +module.exports = router; diff --git a/src/controller/company.controller.js b/src/controller/company.controller.js new file mode 100644 index 0000000..76d780d --- /dev/null +++ b/src/controller/company.controller.js @@ -0,0 +1,93 @@ +const Company = require("../models/Company"); + +module.exports = { + /** + * Gets the company data by `id` + */ + getCompanyByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const companyData = await Company.findById(id); + if (!companyData) { + res.status(404); + return; + } + req.send(companyData); + } catch (error) { + next(error); + } + }, + + /** + * Create a company + */ + createCompany: async (req, res, next) => { + const { name, address, contact_info, business_info } = req.body; + + if (!(name && address)) { + res.status(400).send("Missing params param"); + return; + } + + try { + const companyData = new Company({ + name, + address, + contact_info, + business_info, + }); + + await companyData.save(); + if (!companyData) { + res.status(404); + return; + } + req.send(companyData); + } catch (error) { + next(error); + } + }, + + /** + * Update a companys detail + */ + updateCompanyByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + const { name, address, contact_info, business_info } = req.body; + + if (!(name || address || contact_info || business_info)) { + res.status(400).send("Missing data in body"); + return; + } + + try { + const companyData = await Company.findById(id); + if (!companyData) { + res.status(404); + return; + } + + if (name) companyData.name = name; + if (address) companyData.address = address; + if (contact_info) companyData.contact_info = contact_info; + if (business_info) companyData.business_info = business_info; + + await companyData.save(); + req.send(companyData); + } catch (error) { + next(error); + } + }, +}; diff --git a/src/controller/company.router.js b/src/controller/company.router.js new file mode 100644 index 0000000..faedeb7 --- /dev/null +++ b/src/controller/company.router.js @@ -0,0 +1,19 @@ +const router = require("express").Router(); +const controller = require("./company.controller"); + +/** + * @route /company/:id + */ +router.get("/:id", controller.getCompanyByID); + +/** + * @route /company/ + */ +router.post("/", controller.createCompany); + +/** + * @route /company/ + */ +router.patch("/:id", controller.updateCompanyByID); + +module.exports = router; diff --git a/src/controller/index.js b/src/controller/index.js index 6d2d094..138e5fd 100644 --- a/src/controller/index.js +++ b/src/controller/index.js @@ -4,9 +4,25 @@ const userRoleRouter = require("./userRole.router"); const userPermissionRouter = require("./userPermission.router"); const { AuthenticateMiddleware } = require("../config/auth"); -router.use("/user", userRouter); +const companyRouter = require("./company.router"); +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"); +const levelRouter = require("./level.router"); + router.use("/user-role", AuthenticateMiddleware, userRoleRouter); router.use("/user-permission", AuthenticateMiddleware, userPermissionRouter); +router.use("/user", userRouter); +router.use("/company", companyRouter); +router.use("/warehouse", warehouseRouter); +router.use("/zone", zoneRouter); +router.use("/area", areaRouter); +router.use("/bay", bayRouter); +router.use("/row", rowRouter); +router.use("/level", levelRouter); + router.get("/", (req, res) => { res.send({ success: true, message: "Hello world" }); diff --git a/src/controller/level.controller.js b/src/controller/level.controller.js new file mode 100644 index 0000000..60f1586 --- /dev/null +++ b/src/controller/level.controller.js @@ -0,0 +1,94 @@ +const Level = require("../models/Level"); +const mongoose = require("mongoose"); + +module.exports = { + /** + * Gets the level data by `id` + */ + getLevelByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const levelData = await Level.findById(id); + if (!levelData) { + res.status(404); + return; + } + req.send(levelData); + } catch (error) { + next(error); + } + }, + + /** + * Create a level + */ + createLevel: async (req, res, next) => { + const { name, number, specs, bay_id } = req.body; + + if (!(name && number)) { + res.status(400).send("Missing params param"); + return; + } + + try { + const levelData = new Level({ + name, + number: parseInt(number), + specs, + bay_id: mongoose.Types.ObjectId(bay_id), + }); + + await levelData.save(); + if (!levelData) { + res.status(404); + return; + } + req.send(levelData); + } catch (error) { + next(error); + } + }, + + /** + * Update a levels detail + */ + updateLevelByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + const { name, number, specs, bay_id } = req.body; + + if (!(name || number || specs || bay_id)) { + res.status(400).send("Missing data in body"); + return; + } + + try { + const levelData = await Level.findById(id); + if (!levelData) { + res.status(404); + return; + } + + if (name) levelData.name = name; + if (number) levelData.number = parseInt(number); + if (specs) levelData.specs = specs; + if (bay_id) levelData.bay_id = mongoose.Types.ObjectId(bay_id); + + await levelData.save(); + req.send(levelData); + } catch (error) { + next(error); + } + }, +}; diff --git a/src/controller/level.router.js b/src/controller/level.router.js new file mode 100644 index 0000000..5271ea6 --- /dev/null +++ b/src/controller/level.router.js @@ -0,0 +1,19 @@ +const router = require("express").Router(); +const controller = require("./level.controller"); + +/** + * @route /level/:id + */ +router.get("/:id", controller.getLevelByID); + +/** + * @route /level/ + */ +router.post("/", controller.createLevel); + +/** + * @route /level/ + */ +router.patch("/:id", controller.updateLevelByID); + +module.exports = router; diff --git a/src/controller/row.controller.js b/src/controller/row.controller.js new file mode 100644 index 0000000..780f299 --- /dev/null +++ b/src/controller/row.controller.js @@ -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); + } + }, +}; diff --git a/src/controller/row.router.js b/src/controller/row.router.js new file mode 100644 index 0000000..0a5cd4c --- /dev/null +++ b/src/controller/row.router.js @@ -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; diff --git a/src/controller/warehouse.controller.js b/src/controller/warehouse.controller.js new file mode 100644 index 0000000..4acf1a0 --- /dev/null +++ b/src/controller/warehouse.controller.js @@ -0,0 +1,94 @@ +const Warehouse = require("../models/Warehouse"); +const mongoose = require("mongoose"); + +module.exports = { + /** + * Gets the warehouse data by `id` + */ + getWarehouseByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const warehouseData = await Warehouse.findById(id); + if (!warehouseData) { + res.status(404); + return; + } + req.send(warehouseData); + } catch (error) { + next(error); + } + }, + + /** + * Create a warehouse + */ + createWarehouse: async (req, res, next) => { + const { name, address, specs, company_id } = req.body; + + if (!(name && address)) { + res.status(400).send("Missing params param"); + return; + } + + try { + const warehouseData = new Warehouse({ + name, + address, + specs, + company_id: mongoose.Types.ObjectId(company_id), + }); + + await warehouseData.save(); + if (!warehouseData) { + res.status(404); + return; + } + req.send(warehouseData); + } catch (error) { + next(error); + } + }, + + /** + * Update a warehouses detail + */ + updateWarehouseByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + const { name, address, specs, company_id } = req.body; + + if (!(name || address || specs || company_id)) { + res.status(400).send("Missing data in body"); + return; + } + + try { + const warehouseData = await Warehouse.findById(id); + if (!warehouseData) { + res.status(404); + return; + } + + if (name) warehouseData.name = name; + if (address) warehouseData.address = address; + if (specs) warehouseData.specs = specs; + if (company_id) warehouseData.company_id = mongoose.Types.ObjectId(company_id); + + await warehouseData.save(); + req.send(warehouseData); + } catch (error) { + next(error); + } + }, +}; diff --git a/src/controller/warehouse.router.js b/src/controller/warehouse.router.js new file mode 100644 index 0000000..98b4dc7 --- /dev/null +++ b/src/controller/warehouse.router.js @@ -0,0 +1,19 @@ +const router = require("express").Router(); +const controller = require("./warehouse.controller"); + +/** + * @route /warehouse/:id + */ +router.get("/:id", controller.getWarehouseByID); + +/** + * @route /warehouse/ + */ +router.post("/", controller.createWarehouse); + +/** + * @route /warehouse/ + */ +router.patch("/:id", controller.updateWarehouseByID); + +module.exports = router; diff --git a/src/controller/zone.controller.js b/src/controller/zone.controller.js new file mode 100644 index 0000000..623dced --- /dev/null +++ b/src/controller/zone.controller.js @@ -0,0 +1,94 @@ +const Zone = require("../models/Zone"); +const mongoose = require("mongoose"); + +module.exports = { + /** + * Gets the zone data by `id` + */ + getZoneByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const zoneData = await Zone.findById(id); + if (!zoneData) { + res.status(404); + return; + } + req.send(zoneData); + } catch (error) { + next(error); + } + }, + + /** + * Create a zone + */ + createZone: async (req, res, next) => { + const { name, type, specs, warehouse_id } = req.body; + + if (!(name && type)) { + res.status(400).send("Missing params param"); + return; + } + + try { + const zoneData = new Zone({ + name, + type, + specs, + warehouse_id: mongoose.Types.ObjectId(warehouse_id), + }); + + await zoneData.save(); + if (!zoneData) { + res.status(404); + return; + } + req.send(zoneData); + } catch (error) { + next(error); + } + }, + + /** + * Update a zones detail + */ + updateZoneByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + const { name, type, specs, warehouse_id } = req.body; + + if (!(name || type || specs || warehouse_id)) { + res.status(400).send("Missing data in body"); + return; + } + + try { + const zoneData = await Zone.findById(id); + if (!zoneData) { + res.status(404); + return; + } + + if (name) zoneData.name = name; + if (type) zoneData.type = type; + if (specs) zoneData.specs = specs; + if (warehouse_id) zoneData.warehouse_id = mongoose.Types.ObjectId(warehouse_id); + + await zoneData.save(); + req.send(zoneData); + } catch (error) { + next(error); + } + }, +}; diff --git a/src/controller/zone.router.js b/src/controller/zone.router.js new file mode 100644 index 0000000..fa79d10 --- /dev/null +++ b/src/controller/zone.router.js @@ -0,0 +1,19 @@ +const router = require("express").Router(); +const controller = require("./zone.controller"); + +/** + * @route /zone/:id + */ +router.get("/:id", controller.getZoneByID); + +/** + * @route /zone/ + */ +router.post("/", controller.createZone); + +/** + * @route /zone/ + */ +router.patch("/:id", controller.updateZoneByID); + +module.exports = router; diff --git a/src/models/Level.js b/src/models/Level.js index d3e6a9c..43b04c0 100644 --- a/src/models/Level.js +++ b/src/models/Level.js @@ -9,7 +9,7 @@ const schema = new mongoose.Schema( }, number: { required: true, - type: String, + type: Number, trim: true, }, specs: { @@ -17,6 +17,10 @@ const schema = new mongoose.Schema( type: String, trim: true, }, + bay_id: { + type: mongoose.Schema.Types.ObjectId, + ref: "Bay", + }, sub_levels: [ { type: {