Skip to content

Commit

Permalink
add route for transaction detail
Browse files Browse the repository at this point in the history
  • Loading branch information
zxdhiru committed Oct 9, 2024
1 parent 2e9d9fe commit f95d838
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions features/transactions/routes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const { Router } = require("express");

const { issueBooks, returnBooks, getIssuedBookTransaction, getTransactions } = require("./transactionController");
const { issueBooks, returnBooks, getIssuedBookTransaction, getTransactions, getTransactionDetails } = require("./transactionController");

const transactionRouter = Router()

transactionRouter.get('/', getTransactions);
transactionRouter.get('/:bookId', getIssuedBookTransaction);
transactionRouter.get('book/:bookId', getIssuedBookTransaction);
transactionRouter.get('/:transactionId', getTransactionDetails);
transactionRouter.post('/issue', issueBooks);
transactionRouter.post('/return', returnBooks);

Expand Down
21 changes: 21 additions & 0 deletions features/transactions/transactionController.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,32 @@ const returnBooks = async (req, res) => {
return sendError(res, 500, error.message || "Something went wrong");
}
};
const getTransactionDetails = async (req, res) => {
const { transactionId } = req.params; // Get the transaction ID from the request parameters
try {
// Find transaction by ID
const transaction = await Transaction.findById(transactionId)
.populate("bookIds") // Populate related book details if needed
.populate("studentId"); // Populate related student details if needed

// If no transaction is found, return 404
if (!transaction) {
return sendError(res, 404, "Transaction not found!");
}

// Send successful response with transaction details
sendResponse(res, 200, transaction);
} catch (error) {
console.error("Error fetching transaction:", error); // Log error for debugging
sendError(res, 500, "Something went wrong while fetching transaction details.");
}
};


module.exports = {
issueBooks,
returnBooks,
getTransactions,
getTransactionDetails,
getIssuedBookTransaction
};

0 comments on commit f95d838

Please sign in to comment.