diff --git a/src/controller/item.controller.js b/src/controller/item.controller.js new file mode 100644 index 0000000..f584e80 --- /dev/null +++ b/src/controller/item.controller.js @@ -0,0 +1,112 @@ +const Item = require("../models/Item"); + +module.exports = { + /** + * Gets the Item data by `id` + */ + getItemByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const itemData = await Item.findById(id); + if (!itemData) { + res.status(404); + return; + } + req.send({ success: true, data: itemData }); + } catch (error) { + next(error); + } + }, + + /** + * Create a Item + */ + createItem: async (req, res, next) => { + const item = { + commonName: req.body.commonName, + formalName: req.body.formalName, + description: req.body.description, + manufacturer: req.body.manufacturer, + size: req.body.size, + color: req.body.color, + type: req.body.type, + unitOfMaterial: req.body.unitOfMaterial, + unitCost: req.body.unitCost, + packageCount: req.body.packageCount, + countPerPallet: req.body.countPerPallet, + countPerPalletPackage: req.body.countPerPalletPackage, + customAttributes: req.body.customAttributes, + }; + + if (Object.values(item).every((_) => _)) { + res.status(400).send("Missing params param"); + return; + } + + try { + const itemData = new Item(item); + + await itemData.save(); + if (!itemData) { + res.status(404); + return; + } + req.send({ success: true, data: itemData }); + } catch (error) { + next(error); + } + }, + + /** + * Update a Item detail + */ + updateItemByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + const item = { + commonName: req.body.commonName, + formalName: req.body.formalName, + description: req.body.description, + manufacturer: req.body.manufacturer, + size: req.body.size, + color: req.body.color, + type: req.body.type, + unitOfMaterial: req.body.unitOfMaterial, + unitCost: req.body.unitCost, + packageCount: req.body.packageCount, + countPerPallet: req.body.countPerPallet, + countPerPalletPackage: req.body.countPerPalletPackage, + customAttributes: req.body.customAttributes, + }; + + try { + const itemData = await Item.findById(id); + if (!itemData) { + res.status(404); + return; + } + + for (const key of Object.keys(item)) { + if (item[key] !== undefined) { + itemData[key] = item[key]; + } + } + + await itemData.save(); + req.send({ success: true, data: itemData }); + } catch (error) { + next(error); + } + }, +}; diff --git a/src/controller/item.router.js b/src/controller/item.router.js new file mode 100644 index 0000000..effbc24 --- /dev/null +++ b/src/controller/item.router.js @@ -0,0 +1,19 @@ +const router = require("express").Router(); +const controller = require("./item.controller"); + +/** + * @route /item/:id + */ +router.get("/:id", controller.getItemByID); + +/** + * @route /item/ + */ +router.post("/", controller.createItem); + +/** + * @route /item/ + */ +router.patch("/:id", controller.updateItemByID); + +module.exports = router; diff --git a/src/models/Item.js b/src/models/Item.js index 1d83044..b9056be 100644 --- a/src/models/Item.js +++ b/src/models/Item.js @@ -47,10 +47,6 @@ const schema = new mongoose.Schema( type: Number, required: true, }, - unitQuantity: { - type: Number, - required: true, - }, packageCount: { type: Number, required: true,