diff --git a/backend/routes/api.js b/backend/routes/api.js index 4b23e96..f0e7a2a 100644 --- a/backend/routes/api.js +++ b/backend/routes/api.js @@ -7,6 +7,8 @@ import { deleteLoanFromDatabase, getBorrowableItemsFromDatabase, createLoanInDatabase, + onTake, + onReturn, } from "../services/database.js"; import { authenticate, generateToken } from "../services/tokenService.js"; const router = express.Router(); @@ -85,6 +87,26 @@ router.post("/borrowableItems", authenticate, async (req, res) => { } }); +router.post("/takeLoan/:id", authenticate, async (req, res) => { + const loanId = req.params.id; + const result = await onTake(loanId); + if (result.success) { + res.status(200).json({ message: "Loan taken successfully" }); + } else { + res.status(500).json({ message: "Failed to take loan" }); + } +}); + +router.post("/returnLoan/:id", authenticate, async (req, res) => { + const loanId = req.params.id; + const result = await onReturn(loanId); + if (result.success) { + res.status(200).json({ message: "Loan returned successfully" }); + } else { + res.status(500).json({ message: "Failed to return loan" }); + } +}); + router.post("/createLoan", authenticate, async (req, res) => { try { const { items, startDate, endDate } = req.body || {}; diff --git a/backend/services/database.js b/backend/services/database.js index b8d3277..990b928 100644 --- a/backend/services/database.js +++ b/backend/services/database.js @@ -294,3 +294,29 @@ export const createLoanInDatabase = async ( conn.release(); } }; + +// These functions are only temporary, and will be deleted when the full bin is set up. + +export const onTake = async (loanId) => { + const [result] = await pool.query( + "UPDATE loans SET take_date = NOW() WHERE id = ?", + [loanId] + ); + + if (result.affectedRows > 0) { + return { success: true }; + } + return { success: false }; +}; + +export const onReturn = async (loanId) => { + const [result] = await pool.query( + "UPDATE loans SET returned_date = NOW() WHERE id = ?", + [loanId] + ); + + if (result.affectedRows > 0) { + return { success: true }; + } + return { success: false }; +}; diff --git a/frontend/src/components/Form4.tsx b/frontend/src/components/Form4.tsx index ea3c4c1..c78aa22 100644 --- a/frontend/src/components/Form4.tsx +++ b/frontend/src/components/Form4.tsx @@ -4,6 +4,7 @@ import { handleDeleteLoan } from "../utils/userHandler"; import { useMutation, useQuery } from "@tanstack/react-query"; import Cookies from "js-cookie"; import { queryClient } from "../utils/queryClient"; +import { onTake, onReturn } from "../utils/userHandler"; type Loan = { id: number; @@ -49,6 +50,20 @@ const Form4: React.FC = () => { }, }); + const takeMutation = useMutation({ + mutationFn: (loanID: number) => onTake(loanID), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["userLoans"] }); + }, + }); + + const returnMutation = useMutation({ + mutationFn: (loanID: number) => onReturn(loanID), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["userLoans"] }); + }, + }); + const onDelete = (loanID: number) => deleteMutation.mutate(loanID); if (isFetching) { @@ -99,11 +114,32 @@ const Form4: React.FC = () => {