Add exchange rates support with separate table and API integration#145
Add exchange rates support with separate table and API integration#145Haeyzed wants to merge 1 commit intonnjeim:masterfrom
Conversation
Haeyzed
left a comment
There was a problem hiding this comment.
feat: add exchange rates support with separate table and API integration
Implements exchange rates feature as requested in #<issue_number>.
Following maintainer's suggestion, exchange rates are stored in a separate
database table (exchange_rates) with a relationship to currencies, allowing
historical tracking of rates while using the latest entry for computations.
Features:
- Create exchange_rates table migration with currency relationship
- Add ExchangeRate model with Currency relationship
- Add exchangeRates relationship to Currency model
- Support fetching exchange rates via exchangeRates field in Currency API
- Add ExchangeRateService with support for multiple API providers:
- exchangerate-api.com (free tier, no API key required)
- Fixer.io (requires API key)
- CurrencyLayer (requires API key)
- Custom API endpoint (configurable)
- Add world:update-exchange-rates artisan command
- Add configuration options in world.php config file
- Automatically return latest exchange rate when requested
The implementation allows users to:
- Configure their preferred exchange rate API provider
- Fetch and store exchange rates via artisan command
- Schedule automatic updates using Laravel's task scheduler
- Access latest exchange rates through existing Currency API endpoints
- Maintain historical exchange rate data for analytics
Breaking changes: None
|
@Haeyzed |
|
@nnjeim ok, thanks ill be looking forward to that. |
|
Kindly notify after testing. |
|
@nnjeim Pls help look at this feature i really need it |
|
@Haeyzed |
@nnjeim I dont have access to merge. You will be the one to help merge. |
@Haeyzed following your test results. i would look into limiting the currency exchange rates updates to selected currencies and not all. |
Detailed PR Description
Description
This PR implements exchange rates support for the World package, addressing the feature request to add exchange rate functionality. Following the maintainer's guidance, exchange rates are stored in a separate
exchange_ratestable with a relationship to currencies, where the latest entry is used for computations.Changes Made
Database
exchange_ratestable migration with:currency_id(foreign key to currencies)exchange_rate(decimal, 15,6 precision)base_currency(string, defaults to USD)created_atandupdated_attimestampscurrency_idandcreated_atfor efficient latest rate queriesModels
✅ Created
ExchangeRatemodel with:Currencymodel✅ Updated
Currencymodel:exchangeRates()hasMany relationshipServices
ExchangeRateServiceclass with:Commands
world:update-exchange-ratesartisan command:Configuration
config/world.php:API Integration
CurrencyIndexAction to supportexchangeRatesrelationCurrencyIndexTransformer to return latest exchange rate when requestedUsage
1. Enable Exchange Rates
Add to your
.envfile:2. For Free Tier (exchangerate-api.com)
No API key required:
3. For Paid APIs
4. Fetch Exchange Rates
Run the artisan command:
5. Schedule Automatic Updates
In
app/Console/Kernel.php:6. Use in Code
7. Use via API
Files Changed
New Files
src/Database/Migrations/2024_12_20_000000_create_exchange_rates_table.phpsrc/Models/ExchangeRate.phpsrc/Models/Traits/ExchangeRateRelations.phpsrc/Services/ExchangeRateService.phpsrc/Commands/UpdateExchangeRates.phpModified Files
config/world.php- Added exchange rate configurationsrc/Models/Currency.php- Added exchangeRates relationship docblocksrc/Models/Traits/CurrencyRelations.php- Added exchangeRates() methodsrc/Actions/Currency/IndexAction.php- Added exchangeRates to availableRelationssrc/Actions/Currency/Transformers/IndexTransformer.php- Added exchangeRates handlingsrc/WorldServiceProvider.php- Registered UpdateExchangeRates commandTesting
Benefits
Related Issue
Addresses the feature request for exchange rate support. The implementation follows the maintainer's suggestion to use a separate table with relationships rather than adding a column directly to the currencies table.
Checklist