This commit is contained in:
louiscklaw
2025-01-31 20:14:02 +08:00
parent 49e275d85d
commit 5c584709c4
706 changed files with 40207 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,78 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>IERG4210 Admin Panel</title>
<style>
#drop-area {
width: 300px;
height: 200px;
border: 2px dashed #ccc;
border-radius: 5px;
text-align: center;
font-family: Arial, sans-serif;
margin: 0 auto;
margin-top: 20px;
}
#drop-area.highlight {
background-color: #f1f1f1;
}
#drop-area .message {
margin: 80px auto;
font-size: 18px;
color: #888;
}
#thumbnail {
max-width: 200px;
max-height: 200px;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>IERG4210 Admin Panel</h1>
<h1>Add Category</h1>
<form action="/addCategory" method="POST">
<label for="categoryName">Category Name:</label>
<input type="text" id="categoryName" name="categoryName" required>
<button type="submit">Add Category</button>
</form>
<h1>Add Product</h1>
<form action="/addProduct" method="POST">
<label for="productName">Product Name:</label>
<input type="text" id="productName" name="productName" required>
<label for="category">Category:</label>
<select id="category" name="category" required>
<option value="" selected disabled>Select a category</option>
<script>
// Fetch categories from the server and populate the dropdown options
fetch('/categories')
.then(response => response.json())
.then(categories => {
const categorySelect = document.getElementById('category');
categories.forEach(category => {
const option = document.createElement('option');
option.value = category.catid;
option.textContent = category.name;
categorySelect.appendChild(option);
});
})
.catch(error => console.error(error));
</script>
</select>
<label for="price">Price:</label>
<input type="number" id="price" name="price" required>
<label for="description">Description:</label>
<textarea id="description" name="description" required></textarea>
<label for="image">Image:</label>
<input type="file" id="image" name="image" accept="image/*" required>
<button type="submit">Add Product</button>
</form>
</body>
</html>

View File

@@ -0,0 +1,85 @@
// Import the 'path' module
const path = require("path");
const express = require("express");
const sqlite3 = require("sqlite3").verbose();
const app = express();
const db = new sqlite3.Database("cart.db");
// Use middleware to parse the request body
app.use(express.urlencoded({ extended: true }));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "form.html"));
});
app.get("/categories", (req, res) => {
const selectCategoriesQuery = `SELECT * FROM categories`;
db.all(selectCategoriesQuery, (err, rows) => {
if (err) {
console.error(err);
res.status(500).json({ error: "Failed to fetch categories" });
} else {
res.json(rows);
}
});
});
// Handle POST request to add a new category
app.post("/addCategory", (req, res) => {
const categoryName = req.body.categoryName;
// Insert the new category into the categories table
const insertCategoryQuery = `INSERT INTO categories (name) VALUES (?)`;
db.run(insertCategoryQuery, [categoryName], (err) => {
if (err) {
console.error(err);
res.status(500).send("Error adding category");
} else {
res.send(`
<script>
alert('Category added successfully');
window.location.href = '/';
</script>
`);
}
});
});
// Handle POST request to add a new product
app.post("/addProduct", (req, res) => {
const productName = req.body.productName;
const category = req.body.category;
const price = req.body.price;
const description = req.body.description;
let imageUrl = "";
if (req.file) {
imageUrl = req.file.filename;
}
// Insert the new product into the products table
const insertProductQuery = `INSERT INTO products (name, catid, price, description) VALUES (?, ?, ?, ?)`;
db.run(
insertProductQuery,
[productName, category, price, description],
(err) => {
if (err) {
console.error(err);
res.status(500).send("Error adding product");
} else {
res.send(`
<script>
alert('Category added successfully');
window.location.href = '/';
</script>
`);
}
}
);
});
// Start the server
app.listen(3000, () => {
console.log("Server started on port 3000");
});