From f7e4b49ac8c5aea86d74abd9e5566046f6b98f57 Mon Sep 17 00:00:00 2001 From: aruhani <163067503+aruhani@users.noreply.github.com> Date: Sun, 20 Apr 2025 10:42:52 -0600 Subject: [PATCH] Backend for Transactions Added in 3 files, for the backend --- backend/controllers/transaction.js | 153 +++++++++++++++++++++++++++++ backend/index.js | 2 + backend/routes/transaction.js | 30 ++++++ 3 files changed, 185 insertions(+) create mode 100644 backend/controllers/transaction.js create mode 100644 backend/routes/transaction.js diff --git a/backend/controllers/transaction.js b/backend/controllers/transaction.js new file mode 100644 index 0000000..6072d2f --- /dev/null +++ b/backend/controllers/transaction.js @@ -0,0 +1,153 @@ +// controllers/transaction.js + +const db = require("../utils/database"); + +// Create a new transaction +exports.createTransaction = async (req, res) => { + const { userID, productID, date, paymentStatus } = req.body; + + try { + const [result] = await db.execute( + `INSERT INTO Transaction (UserID, ProductID, Date, PaymentStatus) + VALUES (?, ?, ?, ?)`, + [userID, productID, date, paymentStatus] + ); + + res.json({ + success: true, + message: "Transaction created successfully", + transactionID: result.insertId, + }); + } catch (error) { + console.error("Error creating transaction:", error); + res.status(500).json({ error: "Could not create transaction" }); + } +}; + +// Get all transactions for a given product +exports.getTransactionsByProduct = async (req, res) => { + const { productID } = req.params; + + try { + const [transactions] = await db.execute( + `SELECT + TransactionID, + UserID, + ProductID, + Date, + PaymentStatus + FROM Transaction + WHERE ProductID = ?`, + [productID] + ); + + res.json({ + success: true, + transactions, + }); + } catch (error) { + console.error("Error fetching transactions by product:", error); + res.status(500).json({ error: "Could not retrieve transactions" }); + } +}; + +// Get all transactions for a given user +exports.getTransactionsByUser = async (req, res) => { + const { userID } = req.body; + + try { + const [transactions] = await db.execute( + `SELECT + TransactionID, + UserID, + ProductID, + Date, + PaymentStatus + FROM Transaction + WHERE UserID = ?`, + [userID] + ); + + res.json({ + success: true, + transactions, + }); + } catch (error) { + console.error("Error fetching transactions by user:", error); + res.status(500).json({ error: "Could not retrieve transactions" }); + } +}; + +// Get all transactions in the system +exports.getAllTransactions = async (req, res) => { + try { + const [transactions] = await db.execute( + `SELECT + TransactionID, + UserID, + ProductID, + Date, + PaymentStatus + FROM Transaction` + ); + + res.json({ + success: true, + transactions, + }); + } catch (error) { + console.error("Error fetching all transactions:", error); + res.status(500).json({ error: "Could not retrieve transactions" }); + } +}; + +// Update the payment status of a transaction +exports.updatePaymentStatus = async (req, res) => { + const { transactionID, paymentStatus } = req.body; + + try { + const [result] = await db.execute( + `UPDATE Transaction + SET PaymentStatus = ? + WHERE TransactionID = ?`, + [paymentStatus, transactionID] + ); + + if (result.affectedRows === 0) { + return res.status(404).json({ success: false, message: "Transaction not found" }); + } + + res.json({ + success: true, + message: "Payment status updated successfully", + }); + } catch (error) { + console.error("Error updating payment status:", error); + res.status(500).json({ error: "Could not update payment status" }); + } +}; + +// Delete a transaction +exports.deleteTransaction = async (req, res) => { + const { transactionID } = req.body; + + try { + const [result] = await db.execute( + `DELETE FROM Transaction + WHERE TransactionID = ?`, + [transactionID] + ); + + if (result.affectedRows === 0) { + return res.status(404).json({ success: false, message: "Transaction not found" }); + } + + res.json({ + success: true, + message: "Transaction deleted successfully", + }); + } catch (error) { + console.error("Error deleting transaction:", error); + res.status(500).json({ error: "Could not delete transaction" }); + } +}; diff --git a/backend/index.js b/backend/index.js index 7afa3fe..e3b875f 100644 --- a/backend/index.js +++ b/backend/index.js @@ -9,6 +9,7 @@ const searchRouter = require("./routes/search"); const recommendedRouter = require("./routes/recommendation"); const history = require("./routes/history"); const review = require("./routes/review"); +const transactionRouter = require("./routes/transaction"); const { generateEmailTransporter } = require("./utils/mail"); const { @@ -42,6 +43,7 @@ app.use("/api/search", searchRouter); app.use("/api/engine", recommendedRouter); app.use("/api/history", history); app.use("/api/review", review); +app.use("/api/transaction", transactionRouter); // Set up a scheduler to run cleanup every hour diff --git a/backend/routes/transaction.js b/backend/routes/transaction.js new file mode 100644 index 0000000..b9ccb75 --- /dev/null +++ b/backend/routes/transaction.js @@ -0,0 +1,30 @@ +// routes/transaction.js +const express = require("express"); +const txCtrl = require("../controllers/transaction"); // <— grab the module +const router = express.Router(); + +// logging middleware +router.use((req, res, next) => { + console.log(`Incoming ${req.method} ${req.originalUrl}`); + next(); +}); + +// Create a new transaction +router.post("/createTransaction", txCtrl.createTransaction); + +// Get all transactions for a specific product +router.get("/getTransactionsByProduct/:productID", txCtrl.getTransactionsByProduct); + +// Get all transactions for a specific user +router.post("/getTransactionsByUser", txCtrl.getTransactionsByUser); + +// Get all transactions in the system +router.get("/getAllTransactions", txCtrl.getAllTransactions); + +// Update payment status on a transaction +router.patch("/updatePaymentStatus", txCtrl.updatePaymentStatus); + +// Delete a transaction +router.delete("/deleteTransaction", txCtrl.deleteTransaction); + +module.exports = router;