This commit is contained in:
Kajal Thakur 2025-01-20 19:20:37 +05:30
parent 40d5a89cd5
commit e74fa5a8ee

248
index.js
View File

@ -1,126 +1,3 @@
// 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");
@ -131,90 +8,121 @@ const PORT = 8080;
app.use(bodyParser.json()); app.use(bodyParser.json());
// In-memory data store for demonstration // In-memory data store for demonstration
let cartData = { let attributesData = {
cart: {}, attributes: [],
}; };
// CREATE - Add or update cart items // CREATE - Add or update attributes
app.post("/cart", (req, res) => { app.post("/attributes", (req, res) => {
const { cart } = req.body; const { attributes } = req.body;
if (!cart || !cart.product || !cart.id) { if (!attributes || !Array.isArray(attributes)) {
return res.status(400).json({ error: "Product and ID are required" }); return res.status(400).json({ error: "Attributes must be an array" });
} }
// Add or update the cart item using the id as the key for (const attribute of attributes) {
cartData.cart[cart.id] = { if (!attribute.key || !attribute.value) {
product: cart.product, return res.status(400).json({
id: cart.id, 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({ res.status(201).json({
message: "Cart updated successfully", message: "Attributes updated successfully",
cart: cartData, attributes: attributesData.attributes,
}); });
}); });
// READ - Get the entire cart // READ - Get all attributes
app.get("/cart", (req, res) => { app.get("/attributes", (req, res) => {
res.json({ res.json({
message: "Cart retrieved successfully", message: "Attributes retrieved successfully",
cart: cartData, attributes: attributesData.attributes,
}); });
}); });
// READ - Get a specific item in the cart by id // READ - Get a specific attribute by key
app.get("/cart/:id", (req, res) => { app.get("/attributes/:key", (req, res) => {
const { id } = req.params; const { key } = req.params;
const item = cartData.cart[id]; const attribute = attributesData.attributes.find(
(item) => item.key === key
);
if (!item) { if (!attribute) {
return res.status(404).json({ error: `Item with id '${id}' not found` }); return res
.status(404)
.json({ error: `Attribute with key '${key}' not found` });
} }
res.json({ res.json({
message: "Item retrieved successfully", message: "Attribute retrieved successfully",
item: { [id]: item }, attribute,
}); });
}); });
// UPDATE - Update a specific item in the cart by id // UPDATE - Update a specific attribute by key
app.put("/cart/:id", (req, res) => { app.put("/attributes/:key", (req, res) => {
const { id } = req.params; const { key } = req.params;
const { product } = req.body; const { value } = req.body;
if (!product) { if (!value) {
return res.status(400).json({ error: "Product is required" }); return res
.status(400)
.json({ error: "Value is required to update the attribute" });
} }
if (!cartData.cart[id]) { const attribute = attributesData.attributes.find(
return res.status(404).json({ error: `Item with id '${id}' not found` }); (item) => item.key === key
);
if (!attribute) {
return res
.status(404)
.json({ error: `Attribute with key '${key}' not found` });
} }
// Update the item // Update the value
cartData.cart[id].product = product; attribute.value = value;
res.json({ res.json({
message: "Item updated successfully", message: "Attribute updated successfully",
cart: cartData, attributes: attributesData.attributes,
}); });
}); });
// DELETE - Remove a specific item from the cart by id // DELETE - Remove a specific attribute by key
app.delete("/cart/:id", (req, res) => { app.delete("/attributes/:key", (req, res) => {
const { id } = req.params; const { key } = req.params;
if (!cartData.cart[id]) { const index = attributesData.attributes.findIndex(
return res.status(404).json({ error: `Item with id '${id}' not found` }); (item) => item.key === key
);
if (index === -1) {
return res
.status(404)
.json({ error: `Attribute with key '${key}' not found` });
} }
// Delete the item // Remove the attribute
delete cartData.cart[id]; attributesData.attributes.splice(index, 1);
res.json({ res.json({
message: "Item deleted successfully", message: "Attribute deleted successfully",
cart: cartData, attributes: attributesData.attributes,
}); });
}); });