This commit is contained in:
Kajal Thakur 2025-01-27 14:59:36 +05:30
commit 7836e34239
6 changed files with 1347 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/node_modules

20
Dockerfile Normal file
View File

@ -0,0 +1,20 @@
# Use an official Node.js image from the Docker Hub
FROM node:18
# Set the working directory inside the container
WORKDIR /app
# Copy package.json and package-lock.json (if available)
COPY package*.json ./
# Install dependencies
RUN npm install
# Copy the rest of the application code
COPY . .
# Expose the port the app will run on
EXPOSE 8080
# Command to start the application
CMD ["node", "index.js"]

11
boltic.yaml Normal file
View File

@ -0,0 +1,11 @@
app: cart-crud
region: asia-south1
entrypoint: index.js
build:
builtin: dockerfile
ignorefile: .gitignore
env:
PORT: '8080'

106
index.js Normal file
View File

@ -0,0 +1,106 @@
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 cartData = {
cart: {
accessories: {},
},
};
// CREATE - Add or update cart items
app.post("/cart", (req, res) => {
const { cart } = req.body;
if (!cart) {
return res.status(400).json({ error: "Cart data is required" });
}
// Merge new cart data with existing cart data
cartData = {
...cartData,
cart: {
...cartData.cart,
...cart,
},
};
res.status(201).json({
message: "Cart updated successfully",
cart: cartData,
});
});
// READ - Get the entire cart
app.get("/cart", (req, res) => {
res.json({
message: "Cart retrieved successfully",
cart: cartData,
});
});
// READ - Get a specific item in the cart
app.get("/cart/accessories/:key", (req, res) => {
const { key } = req.params;
const item = cartData.cart.accessories[key];
if (!item) {
return res.status(404).json({ error: `Item with key '${key}' not found` });
}
res.json({
message: "Item retrieved successfully",
item: { [key]: item },
});
});
// UPDATE - Update a specific item in the cart
app.put("/cart/accessories/:key", (req, res) => {
const { key } = req.params;
const { value } = req.body;
if (!value) {
return res.status(400).json({ error: "Value is required" });
}
if (!cartData.cart.accessories[key]) {
return res.status(404).json({ error: `Item with key '${key}' not found` });
}
// Update the item
cartData.cart.accessories[key] = value;
res.json({
message: "Item updated successfully",
cart: cartData,
});
});
// DELETE - Remove a specific item from the cart
app.delete("/cart/accessories/:key", (req, res) => {
const { key } = req.params;
if (!cartData.cart.accessories[key]) {
return res.status(404).json({ error: `Item with key '${key}' not found` });
}
// Delete the item
delete cartData.cart.accessories[key];
res.json({
message: "Item deleted successfully",
cart: cartData,
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});

1192
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

17
package.json Normal file
View File

@ -0,0 +1,17 @@
{
"name": "test-report",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"ejs": "^3.1.10",
"express": "^4.21.1",
"nodemon": "^3.1.7"
}
}