feat: added material endpoints
This commit is contained in:
122
src/controller/material.controller.js
Normal file
122
src/controller/material.controller.js
Normal file
@@ -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);
|
||||
}
|
||||
},
|
||||
};
|
||||
19
src/controller/material.router.js
Normal file
19
src/controller/material.router.js
Normal file
@@ -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;
|
||||
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user