Prerequisites
Before you begin, ensure you have the following installed:Docker
Version 20.10 or higher
Docker Compose
Version 2.0 or higher
.NET 8 SDK
Required for local development (optional for Docker)
PostgreSQL Client
psql or any PostgreSQL client for database setup
Setup Steps
Configure Environment Variables
Create a
.env file in the project root with your PostgreSQL password:The default configuration uses
alexpg as the username and currencies as the database name. These can be modified in docker-compose.yaml.Start PostgreSQL with Docker Compose
Launch the PostgreSQL container:This will start PostgreSQL on port 5432 with a persistent volume named
currencies.Initialize the Database
Connect to PostgreSQL and create the required tables:Run the following SQL commands from
data.sql:Build and Run the API
Build the Docker image and run the application:Alternatively, run locally with .NET:The API will be available at
http://localhost:8080Your First API Call
Let’s perform a currency conversion from USD to RUB:Expected Response
The conversion endpoint is defined in
ExchangeController.cs:16. It intelligently handles direct rates, reverse rates, and cross-rates automatically.Additional API Endpoints
Try these other endpoints to explore the API:Get All Currencies
Search Currencies
Get All Exchange Rates
Add a New Currency
Add an Exchange Rate
Configuration
Automatic Rate Updates
The API automatically updates exchange rates from the Central Bank of Russia. Configure the update frequency inappsettings.json:
appsettings.json
Program.cs:29-33 using the CBRExchangeRate hosted service.
CORS Configuration
If you’re building a frontend application, configure allowed origins inappsettings.json:
appsettings.json
Database Connection
Modify the connection string inappsettings.json if needed:
appsettings.json
For Docker deployment, use
Host=postgres instead of Host=localhost to connect to the PostgreSQL container.Troubleshooting
Database Connection Errors
If you see connection errors, ensure:- PostgreSQL container is running:
docker ps - Connection string matches your configuration
- Database and tables are created
Rate Updates Not Working
The automatic rate update service (CBRExchangeRate.cs:13) requires:
- Currency pairs with format
[CURRENCY_CODE]→RUBin the database - Network access to the Central Bank of Russia SOAP service
- Valid currency codes matching CBR’s currency list
Validation Errors
The API validates:- Currency codes: 3-5 uppercase letters (validated in
Currency.cs:25) - Currency names: 3-60 characters, letters and spaces only
- Exchange rates: Must be between 0 and 99,999,999 (validated in
ExchangeRate.cs:28-31)
Next Steps
Architecture
Understand the clean architecture design and layer responsibilities
API Reference
Explore all available endpoints and their parameters