diff --git a/src/controller/material.controller.js b/src/controller/material.controller.js new file mode 100644 index 0000000..da5fb4e --- /dev/null +++ b/src/controller/material.controller.js @@ -0,0 +1,122 @@ +const mongoose = require("mongoose"); +const Material = require("../models/Material"); +const Inventory = require("../models/Inventory"); + +module.exports = { + /** + * Gets the Material data by `id` + */ + getMaterialByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + try { + const materialData = await Material.findById(id); + if (!materialData) { + res.status(404); + return; + } + req.send({ success: true, data: materialData }); + } catch (error) { + next(error); + } + }, + + /** + * Create a Material + */ + createMaterial: async (req, res, next) => { + const { name, parentId, inventoryId } = req.body; + + try { + let parent; + if (parentId && mongoose.isValidObjectId(parentId)) { + parent = await Material.findById(parent); + } else if (parentId && !mongoose.isValidObjectId(parentId)) { + res.status(400).send("Invalid params parentId"); + return; + } + + let inventory; + if (inventoryId && mongoose.isValidObjectId(inventoryId)) { + inventory = await Inventory.findById(inventoryId); + } else { + res.status(400).send("Invalid params inventoryId"); + return; + } + + const materialData = new Material({ + name, + parent, + inventory, + }); + + await materialData.save(); + if (!materialData) { + res.status(404); + return; + } + req.send({ success: true, data: materialData }); + } catch (error) { + next(error); + } + }, + + /** + * Update a Material detail + */ + updateMaterialByID: async (req, res, next) => { + const { id } = req.params; + + if (!id) { + res.status(400).send("Missing id param"); + return; + } + + const { name, parentId, inventoryId } = req.body; + + if (!(name || parentId || inventoryId)) { + res.status(400).send("Missing data in body"); + return; + } + + try { + const materialData = await Material.findById(id); + if (!materialData) { + res.status(404); + return; + } + + if (name) { + materialData.name = name; + } + + let parent; + if (parentId && mongoose.isValidObjectId(parentId)) { + parent = await Material.findById(parent); + materialData.parent = parent; + } else if (parentId && !mongoose.isValidObjectId(parentId)) { + res.status(400).send("Invalid params parentId"); + return; + } + + let inventory; + if (inventoryId && mongoose.isValidObjectId(inventoryId)) { + inventory = await Inventory.findById(inventoryId); + materialData.inventory = inventory; + } else { + res.status(400).send("Invalid params inventoryId"); + return; + } + + await materialData.save(); + req.send({ success: true, data: materialData }); + } catch (error) { + next(error); + } + }, +}; diff --git a/src/controller/material.router.js b/src/controller/material.router.js new file mode 100644 index 0000000..7fa0873 --- /dev/null +++ b/src/controller/material.router.js @@ -0,0 +1,19 @@ +const router = require("express").Router(); +const controller = require("./material.controller"); + +/** + * @route /material/:id + */ +router.get("/:id", controller.getMaterialByID); + +/** + * @route /material/ + */ +router.post("/", controller.createMaterial); + +/** + * @route /material/ + */ +router.patch("/:id", controller.updateMaterialByID); + +module.exports = router; diff --git a/src/models/Material.js b/src/models/Material.js index 4aaf280..3f39983 100644 --- a/src/models/Material.js +++ b/src/models/Material.js @@ -7,21 +7,15 @@ const schema = new mongoose.Schema( required: true, trim: true, }, - family: [ - { - name: { - type: String, - required: true, - trim: true, - }, - depth: { - type: Number, - required: true, - min: 1, - max: 10 - }, - }, - ], + parent: { + type: mongoose.Schema.Types.ObjectId, + ref: "Material", + }, + inventory: { + type: mongoose.Schema.Types.ObjectId, + ref: "Inventory", + required: true + }, }, { timestamps: true,