This is a Spring Boot-based application designed to manage user stock portfolios. It provides RESTful APIs to manage users, stocks, and trades, including features like portfolio calculation and randomization of stock prices.
- Docker
- IDE (e.g., IntelliJ IDEA, Eclipse)
- Java 17 or later
- Maven 3.6+
- MySQL Server
flowchart TD
A[Client] -->|API Requests| B[Spring Boot Application]
B --> C[Controller Layer]
C --> D[Service Layer]
D --> E[Repository Layer]
E --> F[(MySQL Database)]
sequenceDiagram;
participant Client
participant Controller
participant Service
participant Repository
participant Database
Client->>Controller: POST /api/trade
Controller->>Service: recordTrade(tradeRequest)
Service->>Repository: Save trade data
Repository->>Database: Insert trade record
Database-->>Repository: Acknowledge Save
Repository-->>Service: Return Trade Data
Service-->>Controller: Return Response
Controller-->>Client: Return Success Message
-
Clone the repository:
git clone https://github.com/ShrishRajGupta/PortfolioGroww.git cd PortfolioGroww
-
Setup package
mvn clean package
-
Build Docker image:
docker build -t demo-app .
Maven build will be executes during creation of the docker image.
Note:if you run this command for first time it will take some time in order to download base image from DockerHub
-
Run the application:
docker run -d --name demo-app -p 8080:8080 demo-app
-
Access the APIs:
- Base URL:
http://localhost:8080/api
- Base URL:
- Kubectl
- Docker
- Helm
-
Create
secrets.yml
file with the following content:apiVersion: v1 kind: Secret metadata: name: mysql-secrets type: Opaque data: mysql-root-password: <base64-encoded-root-password> mysql-database: <base64-encoded-database-name> mysql-user: <base64-encoded-username> mysql-password: <base64-encoded-password>
Replace
<base64-encoded-...>
with the base64-encoded values of your MySQL root password, database name, username, and password.Note: You can use the following command to encode a string to base64:
echo -n "your-string" | base64'
-
Create a Kubernetes secret:
kubectl apply -f secrets.yml
-
Create a Kubernetes deployment:
kubectl apply -f k8s/deployment.yml
Endpoint: POST /api/trade
Description: Records a trade for a user and stock.
Request Body:
{
"userAccountId": 1,
"stockId": 5,
"tradeType": "BUY",
"quantity": 10
}
Response:
{
"status": "SUCCESS",
"message": "Trade recorded successfully."
}
Endpoint: GET /api/portfolio/{userId}
Description: Retrieves the portfolio details for a specific user.
Response:
{
"holdings": [
{
"stockName": "Stock1",
"stockId": 1,
"quantity": 10,
"buyPrice": 100.0,
"currentPrice": 105.0,
"gainLoss": 50.0
}
],
"totalHoldingValue": 1050.0,
"totalBuyPrice": 1000.0,
"totalPL": 50.0,
"totalPLPercentage": 5.0
}
Endpoint: GET /api/stocks/{stock_id}
Description: Retrieves stock details for the specified stock ID.
Response:
{
"id": 5,
"name": "Stock5",
"openPrice": 105.0,
"closePrice": 110.0,
"highPrice": 115.0,
"lowPrice": 100.0,
"settlementPrice": 107.5
}
Endpoint: POST /api/stocks/update
Description: Updates stock data by processing a CSV file.
Request:
- Form-Data with a file key containing the CSV file.
Response:
"Stocks updated successfully."
Endpoint: GET /api/populate/users
Description: Adds 10 dummy user accounts to the database.
Response:
"10 users added successfully."
Endpoint: GET /api/populate/stocks
Description: Adds 10 dummy stocks with predefined price ranges to the database.
Response:
"10 stocks added successfully."
Endpoint: GET /api/populate/trades
Description: Creates random trades for all users and stocks, with random trade types (BUY
or SELL
), quantities, and prices.
Response:
"Trades with random data added for all users and stocks."
Endpoint: PUT /api/populate/stocks/update-prices
Description: Updates stock prices (open, close, high, low, and settlement) to random values for all stocks in the database.
Response:
"Stock prices randomized successfully."
Description: Handles CRUD operations for UserAccount
entities.
Methods:
Optional<UserAccount> findByEmail(String email);
Description: Handles CRUD operations for Stock
entities.
Methods:
Optional<Stock> findByName(String name);
Description: Handles CRUD operations for Trade
entities.
Methods:
List<Trade> findByUserAccountId(Long userAccountId);
List<Trade> findByStockId(Long stockId);
Fields:
Long id
String name
String email
LocalDateTime createdAt
Fields:
Long id
String name
Double openPrice
Double closePrice
Double highPrice
Double lowPrice
Double settlementPrice
Fields:
Long id
UserAccount userAccount
Stock stock
String tradeType
Integer quantity
Double price
LocalDateTime createdAt
PR's are welcome !Found a Bug ?
Create an Issue.
Leave a β If you think this project is cool.
---For any issues, feel free to reach out via email at shrishrg@gmail.com
or create an issue in the repository.