test01
This commit is contained in:
parent
e74fa5a8ee
commit
aa77000150
160
index.js
160
index.js
|
|
@ -1,3 +1,136 @@
|
||||||
|
// const express = require("express");
|
||||||
|
// const bodyParser = require("body-parser");
|
||||||
|
|
||||||
|
// const app = express();
|
||||||
|
// const PORT = 8080;
|
||||||
|
|
||||||
|
// // 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,
|
||||||
|
// });
|
||||||
|
// });
|
||||||
|
|
||||||
|
// // Start the server
|
||||||
|
// app.listen(PORT, () => {
|
||||||
|
// console.log(`Server is running on http://localhost:${PORT}`);
|
||||||
|
// });
|
||||||
|
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const bodyParser = require("body-parser");
|
const bodyParser = require("body-parser");
|
||||||
|
|
||||||
|
|
@ -12,35 +145,34 @@ let attributesData = {
|
||||||
attributes: [],
|
attributes: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
// CREATE - Add or update attributes
|
// CREATE - Add or update attributes dynamically from "activities"
|
||||||
app.post("/attributes", (req, res) => {
|
app.post("/activities", (req, res) => {
|
||||||
const { attributes } = req.body;
|
const { activities } = req.body;
|
||||||
|
|
||||||
if (!attributes || !Array.isArray(attributes)) {
|
if (!activities || typeof activities !== "object") {
|
||||||
return res.status(400).json({ error: "Attributes must be an array" });
|
return res.status(400).json({ error: "Activities must be an object" });
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const attribute of attributes) {
|
// Transform activities object into attributes array
|
||||||
if (!attribute.key || !attribute.value) {
|
const attributes = Object.entries(activities).map(([key, value]) => ({
|
||||||
return res.status(400).json({
|
key,
|
||||||
error: "Each attribute must have a 'key' and 'value'",
|
value,
|
||||||
});
|
}));
|
||||||
}
|
|
||||||
|
|
||||||
// Check if the attribute already exists; if yes, update it
|
// Add or update attributes
|
||||||
|
for (const attribute of attributes) {
|
||||||
const existingIndex = attributesData.attributes.findIndex(
|
const existingIndex = attributesData.attributes.findIndex(
|
||||||
(item) => item.key === attribute.key
|
(item) => item.key === attribute.key
|
||||||
);
|
);
|
||||||
if (existingIndex !== -1) {
|
if (existingIndex !== -1) {
|
||||||
attributesData.attributes[existingIndex].value = attribute.value;
|
attributesData.attributes[existingIndex].value = attribute.value;
|
||||||
} else {
|
} else {
|
||||||
// Add new attribute
|
|
||||||
attributesData.attributes.push(attribute);
|
attributesData.attributes.push(attribute);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
res.status(201).json({
|
res.status(201).json({
|
||||||
message: "Attributes updated successfully",
|
message: "Activities processed successfully",
|
||||||
attributes: attributesData.attributes,
|
attributes: attributesData.attributes,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user