diff --git a/src/controllers/holding.controller.ts b/src/controllers/holding.controller.ts new file mode 100644 index 0000000..c8ee1b1 --- /dev/null +++ b/src/controllers/holding.controller.ts @@ -0,0 +1,32 @@ +import { NextFunction, Request, Response } from "express"; +import { validationResult } from "express-validator"; +import jwt from "jsonwebtoken"; +import APIError from "../exeption/APIError"; +import ValidationError from "../exeption/ValidationError"; +import Holding from "../models/holdings.model"; +import Locals from "../providers/Locals"; + +class HoldingController { + public getHoldings = async ( + request: Request, + response: Response, + next: NextFunction + ) => { + try { + const errors = validationResult(request); + if (!errors.isEmpty()) { + throw new ValidationError(errors, 400); + } + const userId = request.user._id; + const holdings = await Holding.find({ user: userId }).limit(50).skip(0); + response.json({ + message: "Holding fetched Successfully", + holdings: holdings, + }); + } catch (error) { + next(error); + } + }; +} + +export default new HoldingController(); diff --git a/src/controllers/security.controller.ts b/src/controllers/security.controller.ts index d497f73..f4a66e2 100644 --- a/src/controllers/security.controller.ts +++ b/src/controllers/security.controller.ts @@ -97,6 +97,27 @@ class SecurityController { next(error); } } + + public getSecurities = async ( + request: Request, + response: Response, + next: NextFunction + ) => { + try { + const errors = validationResult(request); + if (!errors.isEmpty()) { + throw new ValidationError(errors, 400); + } + const userId = request.user._id; + const securities = await Security.find().limit(50).skip(0); + response.json({ + message: "Security fetched Successfully", + securities: securities, + }); + } catch (error) { + next(error); + } + }; } export default new SecurityController(); diff --git a/src/controllers/transaction.controller.ts b/src/controllers/transaction.controller.ts index 5ebdcd7..dfbac6a 100644 --- a/src/controllers/transaction.controller.ts +++ b/src/controllers/transaction.controller.ts @@ -377,6 +377,56 @@ class TransactionController { next(error); } }; + + public getTransactions = async ( + request: Request, + response: Response, + next: NextFunction + ) => { + try { + const errors = validationResult(request); + if (!errors.isEmpty()) { + throw new ValidationError(errors, 400); + } + const userId = request.user._id; + const transactions = await Transaction.find({ user: userId }) + .limit(50) + .skip(0); + response.json({ + message: "Transaction fetched Successfully", + transactions: transactions, + }); + } catch (error) { + next(error); + } + }; + + public getTransactionsByTicker = async ( + request: Request, + response: Response, + next: NextFunction + ) => { + try { + const errors = validationResult(request); + if (!errors.isEmpty()) { + throw new ValidationError(errors, 400); + } + const userId = request.user._id; + const { ticker } = request.params; + const transactions = await Transaction.find({ + user: userId, + ticker: ticker, + }) + .limit(50) + .skip(0); + response.json({ + message: "Transaction fetched Successfully", + transactions: transactions, + }); + } catch (error) { + next(error); + } + }; } export default new TransactionController(); diff --git a/src/controllers/user.controller.ts b/src/controllers/user.controller.ts index bf060ed..6ef39ca 100644 --- a/src/controllers/user.controller.ts +++ b/src/controllers/user.controller.ts @@ -56,6 +56,26 @@ class UserController { next(error); } } + public async getUser( + request: Request, + response: Response, + next: NextFunction + ) { + try { + const errors = validationResult(request); + if (!errors.isEmpty()) { + throw new ValidationError(errors, 400); + } + const userId = request.user._id; + let user = await User.findById(userId); + if (!user) { + throw new APIError("Invalid user id provided", 400); + } + response.json({ message: "User fetched successfully", user: user }); + } catch (error) { + next(error); + } + } } export default new UserController(); diff --git a/src/index.ts b/src/index.ts index 0ac5125..c22306b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,7 +4,7 @@ import Locals from "./providers/Locals"; import * as path from "path"; import * as dotenv from "dotenv"; -dotenv.config({ path: path.join(__dirname, ".env") }); +dotenv.config({ path: path.join(__dirname, "../.env") }); /** * Run the Database pool */ diff --git a/src/routes/holding.route.ts b/src/routes/holding.route.ts new file mode 100644 index 0000000..103238e --- /dev/null +++ b/src/routes/holding.route.ts @@ -0,0 +1,7 @@ +import { Router } from "express"; +import { body } from "express-validator"; +import holdingController from "../controllers/holding.controller"; +const router = Router(); + +router.get("/", holdingController.getHoldings); +export default router; diff --git a/src/routes/index.ts b/src/routes/index.ts index 98d6731..54eb00a 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -3,11 +3,13 @@ import { Router } from "express"; import userRoutes from "./user.route"; import transactionRoutes from "./transaction.route"; import securityRoutes from "./security.route"; +import holdingRoutes from "./holding.route"; const router = Router(); router.use("/user", userRoutes); router.use("/transaction", transactionRoutes); router.use("/security", securityRoutes); +router.use("/holding", holdingRoutes); export default router; diff --git a/src/routes/security.route.ts b/src/routes/security.route.ts index bd353c4..63c2b04 100644 --- a/src/routes/security.route.ts +++ b/src/routes/security.route.ts @@ -1,28 +1,30 @@ import { Router } from "express"; import { body, param } from "express-validator"; import { currentPriceError, tickerError } from "../constants/security.contant"; -import userController from "../controllers/security.controller"; +import securityController from "../controllers/security.controller"; const router = Router(); router.post( "/", body("ticker", tickerError).isString().isLength({ min: 3, max: 10 }), body("totalShares").isInt({ min: 10, max: 100000 }), - body("currentPrice", currentPriceError).isInt({ min: 1, max: 100000 }), - userController.createSecurity + body("currentPrice", currentPriceError).isFloat({ min: 1, max: 100000 }), + securityController.createSecurity ); router.delete( "/:ticker", param("ticker", tickerError).isString().isLength({ min: 3, max: 10 }), - userController.deleteSecurity + securityController.deleteSecurity ); router.put( "/:ticker", param("ticker", tickerError).isString().isLength({ min: 3, max: 10 }), - body("currentPrice", currentPriceError).isInt({ min: 1, max: 100000 }), - userController.updateCurrentPrice + body("currentPrice", currentPriceError).isFloat({ min: 1, max: 100000 }), + securityController.updateCurrentPrice ); +router.get("/", securityController.getSecurities); + export default router; diff --git a/src/routes/transaction.route.ts b/src/routes/transaction.route.ts index 8cec052..365e823 100644 --- a/src/routes/transaction.route.ts +++ b/src/routes/transaction.route.ts @@ -1,22 +1,19 @@ import { Router } from "express"; import { body, param } from "express-validator"; +import { tickerError } from "../constants/security.contant"; import transactionController from "../controllers/transaction.controller"; const router = Router(); router.post( "/buy/:ticker", - param("ticker", "ticker should be a string of length between 3 - 10") - .isString() - .isLength({ min: 3, max: 10 }), + param("ticker", tickerError).isString().isLength({ min: 3, max: 10 }), body("shareCount").isInt({ min: 1, max: 100000 }), transactionController.buyShares ); router.post( "/sell/:ticker", - param("ticker", "ticker should be a string of length between 3 - 10") - .isString() - .isLength({ min: 3, max: 10 }), + param("ticker", tickerError).isString().isLength({ min: 3, max: 10 }), body("shareCount").isInt({ min: 0, max: 100000 }), transactionController.sellShares ); @@ -35,4 +32,12 @@ router.put( transactionController.updateTransaction ); +router.get( + "/:ticker", + param("ticker", tickerError).isString().isLength({ min: 3, max: 10 }), + transactionController.getTransactionsByTicker +); + +router.get("/", transactionController.getTransactions); + export default router; diff --git a/src/routes/user.route.ts b/src/routes/user.route.ts index baa0260..7b0a0a2 100644 --- a/src/routes/user.route.ts +++ b/src/routes/user.route.ts @@ -17,4 +17,6 @@ router.put( userController.topUpUser ); +router.get("/", userController.getUser); + export default router;