Skip to content

Commit 3478eb5

Browse files
authored
Merge pull request #55 from Capstone-C2SE02-TI/TASK48-VIEW-PORTFOLIO
feat: 🎸 View Portfolio
2 parents 300f7f1 + 2f3c0b8 commit 3478eb5

File tree

5 files changed

+76
-0
lines changed

5 files changed

+76
-0
lines changed

src/controllers/Portfolio.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import _ from "lodash";
2+
import { getPortfolio } from "../services/crudDatabase/portfolio.js";
3+
4+
function PortfolioController() {
5+
this.getPortfolio = async (req, res, next) => {
6+
const walletAddress = req.params.walletAddress;
7+
await getPortfolio(walletAddress)
8+
.then((data) =>
9+
data
10+
? res.status(200).json({
11+
message: "successfully",
12+
error: null,
13+
data: data
14+
})
15+
: res.status(400).json({
16+
message: "failed",
17+
error: null,
18+
data: null
19+
})
20+
)
21+
.catch((error) =>
22+
res.status(400).json({
23+
message: "failed",
24+
error: error,
25+
data: null
26+
})
27+
);
28+
};
29+
}
30+
31+
export default new PortfolioController();

src/routes/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ import userRouter from "./user.js";
33
import displayRouter from "./display.js";
44
import blogRouter from "./blog.js";
55
import commentRouter from "./comment.js";
6+
import portfolioRouter from "./portfolio.js";
67

78
function routing(app) {
89
app.use("/auth", authRouter);
910
app.use("/display", displayRouter);
1011
app.use("/user", userRouter);
1112
app.use("/blog", blogRouter);
1213
app.use("/comment", commentRouter);
14+
app.use("/portfolio", portfolioRouter);
1315
app.use("*", (req, res, next) => {
1416
res.status(404).json({
1517
message: "not-found",

src/routes/portfolio.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import PortfolioController from "../controllers/Portfolio.js";
2+
import express from "express";
3+
const router = express.Router();
4+
5+
router.get("/:walletAddress", PortfolioController.getPortfolio);
6+
7+
export default router;
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { InvestorModel } from "../../models/index.js";
2+
3+
export const getPortfolio = async (userWalletAddress) => {
4+
const users = await InvestorModel.find({
5+
followers: { $in: [userWalletAddress] }
6+
}).lean();
7+
8+
const datas = users.map((user) => {
9+
return {
10+
sharkId: user.sharkId,
11+
totalAssets: user.totalAssets,
12+
actualGrowth: user.percent24h,
13+
walletAddress: user.walletAddress,
14+
totalTransactions: user.transactionsHistory.length
15+
};
16+
});
17+
18+
return datas;
19+
};

src/swaggers/portfolio.yaml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Online Swagger yaml file formatter: https://editor.swagger.io/
2+
3+
/portfolio/{walletAddress}:
4+
get:
5+
summary: Get user portfolio
6+
tags:
7+
- Portfolio
8+
parameters:
9+
- in: path
10+
name: walletAddress
11+
schema:
12+
type: string
13+
responses:
14+
200:
15+
description: Successfully
16+
400:
17+
description: Failed

0 commit comments

Comments
 (0)