Skip to content

Commit

Permalink
Add get endpoints to fetch data
Browse files Browse the repository at this point in the history
  • Loading branch information
AkshayCHD committed Aug 1, 2021
1 parent e4c3d8e commit 4a32559
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 13 deletions.
32 changes: 32 additions & 0 deletions src/controllers/holding.controller.ts
Original file line number Diff line number Diff line change
@@ -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();
21 changes: 21 additions & 0 deletions src/controllers/security.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
50 changes: 50 additions & 0 deletions src/controllers/transaction.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
20 changes: 20 additions & 0 deletions src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
7 changes: 7 additions & 0 deletions src/routes/holding.route.ts
Original file line number Diff line number Diff line change
@@ -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;
2 changes: 2 additions & 0 deletions src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
14 changes: 8 additions & 6 deletions src/routes/security.route.ts
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 11 additions & 6 deletions src/routes/transaction.route.ts
Original file line number Diff line number Diff line change
@@ -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
);
Expand All @@ -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;
2 changes: 2 additions & 0 deletions src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ router.put(
userController.topUpUser
);

router.get("/", userController.getUser);

export default router;

0 comments on commit 4a32559

Please sign in to comment.