feat: added item controllers
This commit is contained in:
112
src/controller/item.controller.js
Normal file
112
src/controller/item.controller.js
Normal file
@@ -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);
|
||||
}
|
||||
},
|
||||
};
|
||||
19
src/controller/item.router.js
Normal file
19
src/controller/item.router.js
Normal file
@@ -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;
|
||||
@@ -47,10 +47,6 @@ const schema = new mongoose.Schema(
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
unitQuantity: {
|
||||
type: Number,
|
||||
required: true,
|
||||
},
|
||||
packageCount: {
|
||||
type: Number,
|
||||
required: true,
|
||||
|
||||
Reference in New Issue
Block a user