This commit is contained in:
Kajal Thakur 2025-01-20 19:18:01 +05:30
parent 507f2d5a9f
commit 40d5a89cd5

247
index.js
View File

@ -1,121 +1,224 @@
// const express = require("express");
// const bodyParser = require("body-parser");
// const app = express();
// // Middleware to parse JSON
// app.use(bodyParser.json());
// // In-memory data store for demonstration
// let attributesData = {
// attributes: [],
// };
// // CREATE - Add or update attributes
// app.post("/attributes", (req, res) => {
// const { attributes } = req.body;
// if (!attributes || !Array.isArray(attributes)) {
// return res.status(400).json({ error: "Attributes must be an array" });
// }
// for (const attribute of attributes) {
// if (!attribute.key || !attribute.value) {
// return res.status(400).json({
// error: "Each attribute must have a 'key' and 'value'",
// });
// }
// // Check if the attribute already exists; if yes, update it
// const existingIndex = attributesData.attributes.findIndex(
// (item) => item.key === attribute.key
// );
// if (existingIndex !== -1) {
// attributesData.attributes[existingIndex].value = attribute.value;
// } else {
// // Add new attribute
// attributesData.attributes.push(attribute);
// }
// }
// res.status(201).json({
// message: "Attributes updated successfully",
// attributes: attributesData.attributes,
// });
// });
// // READ - Get all attributes
// app.get("/attributes", (req, res) => {
// res.json({
// message: "Attributes retrieved successfully",
// attributes: attributesData.attributes,
// });
// });
// // READ - Get a specific attribute by key
// app.get("/attributes/:key", (req, res) => {
// const { key } = req.params;
// const attribute = attributesData.attributes.find(
// (item) => item.key === key
// );
// if (!attribute) {
// return res.status(404).json({ error: `Attribute with key '${key}' not found` });
// }
// res.json({
// message: "Attribute retrieved successfully",
// attribute,
// });
// });
// // UPDATE - Update a specific attribute by key
// app.put("/attributes/:key", (req, res) => {
// const { key } = req.params;
// const { value } = req.body;
// if (!value) {
// return res.status(400).json({ error: "Value is required to update the attribute" });
// }
// const attribute = attributesData.attributes.find(
// (item) => item.key === key
// );
// if (!attribute) {
// return res.status(404).json({ error: `Attribute with key '${key}' not found` });
// }
// // Update the value
// attribute.value = value;
// res.json({
// message: "Attribute updated successfully",
// attributes: attributesData.attributes,
// });
// });
// // DELETE - Remove a specific attribute by key
// app.delete("/attributes/:key", (req, res) => {
// const { key } = req.params;
// const index = attributesData.attributes.findIndex(
// (item) => item.key === key
// );
// if (index === -1) {
// return res.status(404).json({ error: `Attribute with key '${key}' not found` });
// }
// // Remove the attribute
// attributesData.attributes.splice(index, 1);
// res.json({
// message: "Attribute deleted successfully",
// attributes: attributesData.attributes,
// });
// });
// // Export app for serverless deployment
// module.exports = app;
const express = require("express"); const express = require("express");
const bodyParser = require("body-parser"); const bodyParser = require("body-parser");
const app = express(); const app = express();
const PORT = 8080;
// Middleware to parse JSON // Middleware to parse JSON
app.use(bodyParser.json()); app.use(bodyParser.json());
// In-memory data store for demonstration // In-memory data store for demonstration
let attributesData = { let cartData = {
attributes: [], cart: {},
}; };
// CREATE - Add or update attributes // CREATE - Add or update cart items
app.post("/attributes", (req, res) => { app.post("/cart", (req, res) => {
const { attributes } = req.body; const { cart } = req.body;
if (!attributes || !Array.isArray(attributes)) { if (!cart || !cart.product || !cart.id) {
return res.status(400).json({ error: "Attributes must be an array" }); return res.status(400).json({ error: "Product and ID are required" });
} }
for (const attribute of attributes) { // Add or update the cart item using the id as the key
if (!attribute.key || !attribute.value) { cartData.cart[cart.id] = {
return res.status(400).json({ product: cart.product,
error: "Each attribute must have a 'key' and 'value'", id: cart.id,
}); };
}
// Check if the attribute already exists; if yes, update it
const existingIndex = attributesData.attributes.findIndex(
(item) => item.key === attribute.key
);
if (existingIndex !== -1) {
attributesData.attributes[existingIndex].value = attribute.value;
} else {
// Add new attribute
attributesData.attributes.push(attribute);
}
}
res.status(201).json({ res.status(201).json({
message: "Attributes updated successfully", message: "Cart updated successfully",
attributes: attributesData.attributes, cart: cartData,
}); });
}); });
// READ - Get all attributes // READ - Get the entire cart
app.get("/attributes", (req, res) => { app.get("/cart", (req, res) => {
res.json({ res.json({
message: "Attributes retrieved successfully", message: "Cart retrieved successfully",
attributes: attributesData.attributes, cart: cartData,
}); });
}); });
// READ - Get a specific attribute by key // READ - Get a specific item in the cart by id
app.get("/attributes/:key", (req, res) => { app.get("/cart/:id", (req, res) => {
const { key } = req.params; const { id } = req.params;
const attribute = attributesData.attributes.find( const item = cartData.cart[id];
(item) => item.key === key
);
if (!attribute) { if (!item) {
return res.status(404).json({ error: `Attribute with key '${key}' not found` }); return res.status(404).json({ error: `Item with id '${id}' not found` });
} }
res.json({ res.json({
message: "Attribute retrieved successfully", message: "Item retrieved successfully",
attribute, item: { [id]: item },
}); });
}); });
// UPDATE - Update a specific attribute by key // UPDATE - Update a specific item in the cart by id
app.put("/attributes/:key", (req, res) => { app.put("/cart/:id", (req, res) => {
const { key } = req.params; const { id } = req.params;
const { value } = req.body; const { product } = req.body;
if (!value) { if (!product) {
return res.status(400).json({ error: "Value is required to update the attribute" }); return res.status(400).json({ error: "Product is required" });
} }
const attribute = attributesData.attributes.find( if (!cartData.cart[id]) {
(item) => item.key === key return res.status(404).json({ error: `Item with id '${id}' not found` });
);
if (!attribute) {
return res.status(404).json({ error: `Attribute with key '${key}' not found` });
} }
// Update the value // Update the item
attribute.value = value; cartData.cart[id].product = product;
res.json({ res.json({
message: "Attribute updated successfully", message: "Item updated successfully",
attributes: attributesData.attributes, cart: cartData,
}); });
}); });
// DELETE - Remove a specific attribute by key // DELETE - Remove a specific item from the cart by id
app.delete("/attributes/:key", (req, res) => { app.delete("/cart/:id", (req, res) => {
const { key } = req.params; const { id } = req.params;
const index = attributesData.attributes.findIndex( if (!cartData.cart[id]) {
(item) => item.key === key return res.status(404).json({ error: `Item with id '${id}' not found` });
);
if (index === -1) {
return res.status(404).json({ error: `Attribute with key '${key}' not found` });
} }
// Remove the attribute // Delete the item
attributesData.attributes.splice(index, 1); delete cartData.cart[id];
res.json({ res.json({
message: "Attribute deleted successfully", message: "Item deleted successfully",
attributes: attributesData.attributes, cart: cartData,
}); });
}); });
// Export app for serverless deployment // Start the server
module.exports = app; app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});