improved product details page
This commit is contained in:
@@ -91,3 +91,42 @@ export const allProducts = async () => {
|
||||
return { code: "ep002" };
|
||||
}
|
||||
};
|
||||
|
||||
export const setAmount = async (itemUUID, amount) => {
|
||||
const [result] = await pool.query(
|
||||
`
|
||||
UPDATE products SET amount = ? WHERE uuid = UUID_TO_BIN(?)
|
||||
`,
|
||||
[amount, itemUUID],
|
||||
);
|
||||
|
||||
if (result.affectedRows > 0) {
|
||||
return { code: "sp004" }; // success
|
||||
} else {
|
||||
return { code: "ep004" }; // error
|
||||
}
|
||||
};
|
||||
|
||||
export const updateItem = async (itemUUID, newValues) => {
|
||||
const [result] = await pool.query(
|
||||
`
|
||||
UPDATE products SET name = ?, description = ?, price = ?, amount = ?, storage_location = UUID_TO_BIN(?), expiry_date = ?, bottling_date = ? WHERE uuid = UUID_TO_BIN(?);
|
||||
`,
|
||||
[
|
||||
newValues.name,
|
||||
newValues.description,
|
||||
newValues.price,
|
||||
newValues.amount,
|
||||
newValues.storage_location_uuid,
|
||||
newValues.expiry_date,
|
||||
newValues.bottling_date,
|
||||
itemUUID,
|
||||
],
|
||||
);
|
||||
|
||||
if (result.affectedRows > 0) {
|
||||
return { code: "sp005" }; // success
|
||||
} else {
|
||||
return { code: "ep005" }; // error
|
||||
}
|
||||
};
|
||||
|
||||
@@ -5,6 +5,8 @@ import {
|
||||
allProducts,
|
||||
newProduct,
|
||||
productDetails,
|
||||
setAmount,
|
||||
updateItem,
|
||||
} from "./database/products.database.js";
|
||||
dotenv.config();
|
||||
const router = express.Router();
|
||||
@@ -95,4 +97,54 @@ router.get("/view", authenticate, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
router.put("/mutate/set-amount", authenticate, async (req, res) => {
|
||||
const amount = req.query.amount;
|
||||
const itemUUID = req.query.item;
|
||||
|
||||
const result = await setAmount(itemUUID, amount);
|
||||
|
||||
if (result.code === "ep004") {
|
||||
res.status(406).json({
|
||||
success: false,
|
||||
code: "ep004",
|
||||
data: null,
|
||||
message: "Error while updating product amount",
|
||||
});
|
||||
}
|
||||
|
||||
if (result.code === "sp004") {
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
code: "sp004",
|
||||
data: null,
|
||||
message: "",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post("/mutate/update-item", authenticate, async (req, res) => {
|
||||
const itemUUID = req.query.item;
|
||||
const newValues = req.body;
|
||||
|
||||
const result = await updateItem(itemUUID, newValues);
|
||||
|
||||
if (result.code === "ep005") {
|
||||
res.status(406).json({
|
||||
success: false,
|
||||
code: "ep005",
|
||||
data: null,
|
||||
message: "Error while updating product",
|
||||
});
|
||||
}
|
||||
|
||||
if (result.code === "sp005") {
|
||||
res.status(200).json({
|
||||
success: true,
|
||||
code: "sp005",
|
||||
data: null,
|
||||
message: "",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
||||
Reference in New Issue
Block a user