JS Lab 2
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
import express from "express";
|
||||
import bodyParser from "body-parser";
|
||||
const { json } = bodyParser;
|
||||
import cors from "cors";
|
||||
import db from "./db.js";
|
||||
const _query = db.query.bind(db);
|
||||
import { join } from "path";
|
||||
import { fileURLToPath } from "url";
|
||||
|
||||
const app = express();
|
||||
const PORT = 3000;
|
||||
|
||||
app.use(cors());
|
||||
app.use(json());
|
||||
|
||||
const filename = fileURLToPath(import.meta.url);
|
||||
const dirname = join(filename, "..");
|
||||
|
||||
app.use(express.static(join(dirname, "public")));
|
||||
|
||||
app.post("/add", (req,res) => {
|
||||
const {customer_id, name, email, phone } = req.body;
|
||||
|
||||
if (!customer_id || !name || !email || !phone ) {
|
||||
return res.status(400).json({ error: "All fields are required" });
|
||||
}
|
||||
|
||||
const query = "INSERT INTO customers (customer_id, name, email, phone) VALUES (?, ?, ?, ?)";
|
||||
_query(query, [customer_id, name, email, phone], (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
if (err.code === "ER_DUP_ENTRY") {
|
||||
return res.status(400).json({ error: "Customer ID already exists" });
|
||||
}
|
||||
return res.status(500).json({ error: err.message });
|
||||
}
|
||||
res.join({ message: "Customer added successfully! "});
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/find", (req, res) => {
|
||||
const { customer_id } = req.body;
|
||||
|
||||
if (!customer_id) {
|
||||
return res.status(400).json({ error: "Customer ID is required" });
|
||||
}
|
||||
|
||||
const query = "SELECT * FROM customers WHERE customer_id = ?";
|
||||
_query(query, [customer_id], (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: err.message });
|
||||
}
|
||||
if (results.length === 0) {
|
||||
return res.status(404).json({ error: "Customer not found" });
|
||||
}
|
||||
res.json(results[0]);
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/update", (req, res) => {
|
||||
const { customer_id, name, email, phone } = req.body;
|
||||
|
||||
if (!customer_id || !name || !email || !phone ) {
|
||||
return res.status(400).json({ error: "All fields are required" });
|
||||
}
|
||||
|
||||
const query = "UPDATE customers SET name = ?, email = ?, phone = ? WHERE customer_id = ?";
|
||||
_query(query, [name, email, phone, customer_id], (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: err.message });
|
||||
}
|
||||
|
||||
if (results.affectedRows === 0) {
|
||||
return res.status(404).json({ error: "Customer not found" });
|
||||
}
|
||||
res.json({ message: "Customer updated successfully!" });
|
||||
});
|
||||
});
|
||||
|
||||
app.post("/delete", (req, res) => {
|
||||
const { customer_id } = req.body;
|
||||
|
||||
if (!customer_id) {
|
||||
return res.status(400).json({ error: "Customer ID is required" });
|
||||
}
|
||||
|
||||
const query = "DELETE FROM customers WHERE customer_id = ?";
|
||||
_query(query, [customer_id], (err, results) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res.status(500).json({ error: err.message });
|
||||
}
|
||||
if (results.affectedRows === 0) {
|
||||
return res.status(404).json({ error: "Customer not found" });
|
||||
}
|
||||
res.json({ message: "Customer deleted successfully!" });
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(PORT, () => {
|
||||
console.log(`Server is running on http://localhost:${PORT}`);
|
||||
});
|
||||
Reference in New Issue
Block a user