This tutorial will guide you through creating a user, managing their portfolio, and tracking portfolio value using the PortfolioManager API.
- PortfolioManager running locally or deployed
- Access to Swagger UI or an HTTP client (curl, Postman, etc.)
- MongoDB connection configured
Open your browser and navigate to:
- Local:
http://localhost:3000/swagger - Production:
https://your-domain.com/swagger
Swagger UI provides an interactive interface for testing API endpoints.
Creating a user automatically creates an associated empty portfolio.
- Find the POST /api/User endpoint
- Click "Try it out"
- Enter the request body:
{
"username": "alice",
"email": "alice@example.com"
}- Click "Execute"
curl -X POST http://localhost:3000/api/User \
-H "Content-Type: application/json" \
-d '{
"username": "alice",
"email": "alice@example.com"
}'{
"id": "67890abcdef12345",
"username": "alice",
"email": "alice@example.com",
"portfolioId": "12345abcdef67890",
"createdAt": "2025-01-15T10:30:00Z"
}Note: Save the portfolioId - you'll need it in the next steps!
Before adding stocks to a portfolio, they must exist in the database. In a real system, stocks would be pre-populated or added via an admin interface.
For this tutorial, you can manually insert stocks into MongoDB or use an API endpoint if available.
Example stock document:
{
"name": "Apple Inc.",
"alias": "AAPL",
"price": 150.25,
"currency": "USD",
"lastUpdated": "2025-01-15T10:00:00Z"
}Retrieve the enriched portfolio (with full stock details) by username.
- Find GET /api/Portfolio/user/{username}
- Click "Try it out"
- Enter username:
alice - Click "Execute"
curl http://localhost:3000/api/Portfolio/user/alice{
"id": "12345abcdef67890",
"userId": "67890abcdef12345",
"username": "alice",
"stocks": [],
"totalValueUsd": 0,
"lastUpdated": "2025-01-15T10:30:00Z"
}The portfolio is currently empty (no stocks).
Now add stocks to Alice's portfolio using the stock name or alias.
- Find POST /api/Portfolio/{id}/stocks
- Click "Try it out"
- Enter portfolio ID:
12345abcdef67890 - Enter request body:
{
"stockIdentifier": "AAPL",
"quantity": 10
}- Click "Execute"
curl -X POST http://localhost:3000/api/Portfolio/12345abcdef67890/stocks \
-H "Content-Type: application/json" \
-d '{
"stockIdentifier": "AAPL",
"quantity": 10
}'Repeat the process to add more stocks:
# Add Tesla stock
curl -X POST http://localhost:3000/api/Portfolio/12345abcdef67890/stocks \
-H "Content-Type: application/json" \
-d '{
"stockIdentifier": "TSLA",
"quantity": 5
}'
# Add Taiwan Semiconductor
curl -X POST http://localhost:3000/api/Portfolio/12345abcdef67890/stocks \
-H "Content-Type: application/json" \
-d '{
"stockIdentifier": "2330.TW",
"quantity": 100
}'Retrieve the portfolio again to see the added stocks with full details.
curl http://localhost:3000/api/Portfolio/user/alice{
"id": "12345abcdef67890",
"userId": "67890abcdef12345",
"username": "alice",
"stocks": [
{
"stockId": "stock-id-1",
"quantity": 10,
"name": "Apple Inc.",
"alias": "AAPL",
"price": 150.25,
"currency": "USD",
"totalValue": 1502.50
},
{
"stockId": "stock-id-2",
"quantity": 5,
"name": "Tesla Inc.",
"alias": "TSLA",
"price": 245.80,
"currency": "USD",
"totalValue": 1229.00
},
{
"stockId": "stock-id-3",
"quantity": 100,
"name": "Taiwan Semiconductor",
"alias": "2330.TW",
"price": 550.00,
"currency": "TWD",
"totalValue": 55000.00
}
],
"totalValueUsd": 4476.98,
"lastUpdated": "2025-01-15T11:00:00Z"
}Modify the quantity of a stock in the portfolio.
- Find PUT /api/Portfolio/{id}/stocks/{stockId}
- Click "Try it out"
- Enter portfolio ID:
12345abcdef67890 - Enter stock ID:
stock-id-1 - Enter query parameter
quantity:15 - Click "Execute"
curl -X PUT "http://localhost:3000/api/Portfolio/12345abcdef67890/stocks/stock-id-1?quantity=15"This updates the AAPL quantity from 10 to 15 shares.
After the scheduled job (RecordDailyValueJob) runs, you can view historical portfolio values.
Note: The job runs on weekdays at 13:35 and Saturdays at 05:35 (Asia/Taipei time). For testing, you may need to wait or manually trigger the job.
curl "http://localhost:3000/api/PortfolioDailyValue/12345abcdef67890/history?range=OneMonth"{
"portfolioId": "12345abcdef67890",
"range": "OneMonth",
"data": [
{
"date": "2025-01-14T00:00:00Z",
"totalValueTwd": 140000.00
},
{
"date": "2025-01-15T00:00:00Z",
"totalValueTwd": 145250.00
}
],
"count": 2
}curl "http://localhost:3000/api/PortfolioDailyValue/12345abcdef67890/summary?range=OneMonth"{
"portfolioId": "12345abcdef67890",
"range": "OneMonth",
"currentValue": 145250.00,
"startValue": 140000.00,
"minValue": 138500.00,
"maxValue": 148000.00,
"averageValue": 142375.00,
"change": 5250.00,
"changePercent": 3.75,
"dataPoints": 30,
"startDate": "2024-12-15T00:00:00Z",
"endDate": "2025-01-15T00:00:00Z"
}Update stock prices as market conditions change.
curl -X PUT "http://localhost:3000/api/Stock/name/AAPL/price?price=155.50"This updates the price of AAPL stock, which will be reflected in portfolio valuations.
Remove a stock from the portfolio when no longer held.
curl -X DELETE http://localhost:3000/api/Portfolio/12345abcdef67890/stocks/stock-id-2This removes Tesla (TSLA) from Alice's portfolio.
Congratulations! You've learned how to:
- ✅ Create a user (which auto-creates a portfolio)
- ✅ Add stocks to a portfolio
- ✅ View enriched portfolio with stock details
- ✅ Update stock quantities
- ✅ View historical daily values and summaries
- ✅ Update stock prices
- ✅ Remove stocks from a portfolio
- Explore Exchange Rates: Learn how currency conversion works in Exchange Rate Guide
- Background Jobs: Understand how daily value recording works in Architecture Overview
- API Reference: See full API documentation in API Reference
Problem: When adding a stock to a portfolio, you receive "Stock not found".
Solution: Ensure the stock exists in the database. Stocks must be added before they can be included in portfolios.
Problem: Username or email is already taken.
Solution: Use a different username or email, or delete the existing user first.
Problem: Daily values are not being recorded.
Solution:
- Check that the Quartz scheduler is running
- Verify the scheduled job times in application logs
- Ensure the ExchangeRateService can fetch rates
- Check MongoDB connection for write permissions
Happy portfolio management! 🚀