From c3249a95bab905fa272a953133a643b49bdff557 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 28 Apr 2025 11:49:23 +0530 Subject: Added route to GET single transaction by id --- src/controller/transaction.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/controller/transaction.ts') diff --git a/src/controller/transaction.ts b/src/controller/transaction.ts index 6c818d6..9d4d6da 100644 --- a/src/controller/transaction.ts +++ b/src/controller/transaction.ts @@ -16,7 +16,7 @@ */ import { Request, Response } from "express"; -import { create, read, del } from "../service/transaction"; +import { create, read, readOne, del } from "../service/transaction"; const addNew = async (req: Request, res: Response) => { try { @@ -40,6 +40,22 @@ const get = async (req: Request, res: Response) => { } } +const getOne = async (req: Request, res: Response) => { + try { + // @ts-ignore + const transaction = await readOne(req.userId, parseInt(req.params.transactionId)); // TODO: handle non int value passed (bad req) + res.status(200).json({ transaction: transaction }); + } catch(error) { + // @ts-ignore + if (error.message == "forbidden") { + res.status(403).json({ error: "You are not allowed to view this transaction." }); + } else { + res.status(500).json({ error: "Internal Server Error" }); + console.error(error); + } + } +} + const remove = async (req: Request, res: Response) => { try { // @ts-ignore @@ -65,5 +81,6 @@ const remove = async (req: Request, res: Response) => { export { addNew, get, + getOne, remove } -- cgit v1.2.3