A robust RESTful Web API built with .NET 8.0 for managing stock portfolios, including user authentication, stock tracking, comments, and portfolio management.
- User Authentication β Secure registration and login with JWT tokens
- Stock Management β Full CRUD operations for stocks with filtering/sorting
- Comment System β Users can comment on stocks
- Portfolio Management β Track and manage user stock portfolios
- API Documentation β Interactive Swagger UI for testing endpoints
| Technology | Purpose |
|---|---|
| ASP.NET Core 8.0 | Web API Framework |
| Entity Framework Core 8.0 | ORM & Database Access |
| SQL Server | Database |
| ASP.NET Identity | User Authentication |
| JWT Bearer Tokens | Authorization |
| Swagger/OpenAPI | API Documentation |
| Newtonsoft.Json | JSON Serialization |
API/
βββ Controllers/ # API Endpoints
β βββ AccountController.cs # User registration & login
β βββ StockController.cs # Stock CRUD operations
β βββ CommentController.cs # Comment management
β βββ PortfolioController.cs # Portfolio management
βββ Models/ # Entity Models
β βββ AppUser.cs # User model (extends IdentityUser)
β βββ Stock.cs # Stock entity
β βββ Comment.cs # Comment entity
β βββ Portfolio.cs # Portfolio entity
βββ Dtos/ # Data Transfer Objects
β βββ Account/ # Login, Register, NewUser DTOs
β βββ Stock/ # Stock request/response DTOs
β βββ Comment/ # Comment DTOs
βββ Repository/ # Data Access Layer
β βββ StockRepository.cs
β βββ CommentRepository.cs
β βββ PortfolioRepository.cs
βββ Interfaces/ # Repository Interfaces
βββ Mappers/ # Entity <-> DTO Mappers
βββ Helpers/ # Query objects & utilities
βββ Extensions/ # Extension methods
βββ Service/ # Token service
βββ Data/ # Database context
βββ Program.cs # Application entry point
βββ appsettings.json # Configuration
βββ api.csproj # Project file
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
POST |
/api/Account/register |
Register new user | β |
POST |
/api/Account/login |
Login & get JWT token | β |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
GET |
/api/stock |
Get all stocks (with filters) | β |
GET |
/api/stock/{id} |
Get stock by ID | β |
POST |
/api/stock |
Create new stock | β |
PUT |
/api/stock/{id} |
Update stock | β |
DELETE |
/api/stock/{id} |
Delete stock | β |
Query Parameters for GET /api/stock:
Symbolβ Filter by stock symbolCompanyNameβ Filter by company nameSortByβ Sort results by fieldIsDescendingβ Sort direction (true/false)
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
GET |
/api/comment |
Get all comments | β |
GET |
/api/comment/{id} |
Get comment by ID | β |
POST |
/api/comment/{stockId} |
Add comment to stock | β |
PUT |
/api/comment/{id} |
Update comment | β |
DELETE |
/api/comment/{id} |
Delete comment | β |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
GET |
/api/portfolio |
Get user's portfolio | β |
POST |
/api/portfolio?symbol=XXX |
Add stock to portfolio | β |
- .NET 8.0 SDK
- SQL Server (or SQL Server Express)
- IDE: Visual Studio 2022 or VS Code
-
Clone the repository
git clone <repository-url> cd API
-
Update the connection string
Edit
appsettings.jsonwith your SQL Server details:{ "ConnectionStrings": { "DefaultConnection": "server=YOUR_SERVER;database=YOUR_DATABASE;trusted_connection=true;TrustServerCertificate=true" } } -
Configure JWT settings (optional)
Update JWT settings in
appsettings.json:{ "JWT": { "Issuer": "http://localhost:5246", "Audience": "http://localhost:5246", "SigningKey": "your-secure-signing-key-min-64-chars" } } -
Apply database migrations
dotnet ef database update
-
Run the application
dotnet run
-
Access Swagger UI
Navigate to:
https://localhost:5001/swaggerorhttp://localhost:5000/swagger
-
Register a new user via
POST /api/Account/register{ "username": "johndoe", "email": "john@example.com", "password": "SecurePass123!" } -
Login via
POST /api/Account/loginto receive JWT token{ "userName": "johndoe", "password": "SecurePass123!" } -
Use the token in subsequent requests:
Authorization: Bearer <your-jwt-token>
- Minimum 8 characters
- At least 1 uppercase letter
- At least 1 lowercase letter
- At least 1 digit
- At least 1 special character
- At least 1 unique character
| Field | Type | Description |
|---|---|---|
| Id | int | Primary key |
| Symbol | string | Stock ticker symbol |
| CompanyName | string | Company name |
| Purchase | decimal | Purchase price |
| LastDiv | decimal | Last dividend |
| Industry | string | Industry sector |
| MarketCap | long | Market capitalization |
| Field | Type | Description |
|---|---|---|
| Id | int | Primary key |
| Title | string | Comment title |
| Content | string | Comment content |
| CreatedOn | DateTime | Creation timestamp |
| StockId | int | Related stock ID |
| AppUserId | string | Author user ID |
| Field | Type | Description |
|---|---|---|
| AppUserId | string | User ID (FK) |
| StockId | int | Stock ID (FK) |
curl -X POST "https://localhost:5001/api/stock" \
-H "Content-Type: application/json" \
-d '{
"symbol": "AAPL",
"companyName": "Apple Inc.",
"purchase": 150.00,
"lastDiv": 0.24,
"industry": "Technology",
"marketCap": 2500000000000
}'curl -X GET "https://localhost:5001/api/stock?Symbol=AAPL&SortBy=CompanyName&IsDescending=true" \
-H "Authorization: Bearer <your-token>"- Run the application
- Open Swagger UI in your browser
- Click Authorize button (π)
- Enter:
Bearer <your-jwt-token> - Test any endpoint interactively
This project is open source and available under the MIT License.
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
For questions or suggestions, please open an issue in the repository.
Made with β€οΈ using .NET 8.0