Compare commits
6 commits
1038bad7d5
...
03f8f3a920
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
03f8f3a920 | ||
|
|
9d6cfe41aa | ||
|
|
a20756c0ff | ||
|
|
f4e880a08c | ||
|
|
1bfa37174f | ||
|
|
4805c8040e |
|
|
@ -1,11 +1,9 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"converter/domain"
|
||||
"converter/infrastructure/coinmarketcap"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
|
@ -19,7 +17,7 @@ const (
|
|||
// Note: The returned RateDTO may contain a reversed rate (e.g., if requesting USD->BTC
|
||||
// but only BTC->USD is available), indicated by FromCode/ToCode being swapped from the request
|
||||
type RateProvider interface {
|
||||
GetRate(ctx context.Context, fromCode, toCode string) (coinmarketcap.RateDTO, error)
|
||||
GetRate(fromCode, toCode string) (coinmarketcap.RateDTO, error)
|
||||
}
|
||||
|
||||
// ConvertCurrencyUseCase handles currency conversion operations
|
||||
|
|
@ -37,11 +35,7 @@ func NewConvertCurrencyUseCase(rateProvider RateProvider, converter *domain.Curr
|
|||
}
|
||||
|
||||
// Execute performs currency conversion
|
||||
func (uc *ConvertCurrencyUseCase) Execute(ctx context.Context, amount, fromCode, toCode string) (domain.Money, error) {
|
||||
// Normalize currency codes to uppercase for consistency
|
||||
fromCode = strings.ToUpper(strings.TrimSpace(fromCode))
|
||||
toCode = strings.ToUpper(strings.TrimSpace(toCode))
|
||||
|
||||
func (uc *ConvertCurrencyUseCase) Execute(amount, fromCode, toCode string) (domain.Money, error) {
|
||||
// Create currencies with uniform precision
|
||||
fromCurrency, err := domain.NewCurrency(fromCode, fromCode, CurrencyPrecision)
|
||||
if err != nil {
|
||||
|
|
@ -55,7 +49,7 @@ func (uc *ConvertCurrencyUseCase) Execute(ctx context.Context, amount, fromCode,
|
|||
}
|
||||
|
||||
// Get exchange rate DTO from provider
|
||||
rateDTO, err := uc.rateProvider.GetRate(ctx, fromCode, toCode)
|
||||
rateDTO, err := uc.rateProvider.GetRate(fromCode, toCode)
|
||||
if err != nil {
|
||||
return domain.Money{}, err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,12 @@
|
|||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"converter/app/mocks"
|
||||
"converter/domain"
|
||||
"converter/infrastructure/coinmarketcap"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestConvertCurrencyUseCase_Execute(t *testing.T) {
|
||||
|
|
@ -38,7 +36,7 @@ func TestConvertCurrencyUseCase_Execute(t *testing.T) {
|
|||
Price: 50000.0, // BTC price in USD
|
||||
Source: "coinmarketcap",
|
||||
}
|
||||
m.On("GetRate", mock.Anything, "BTC", "USD").Return(rateDTO, nil)
|
||||
m.On("GetRate", "BTC", "USD").Return(rateDTO, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -58,7 +56,7 @@ func TestConvertCurrencyUseCase_Execute(t *testing.T) {
|
|||
Price: 50000.0, // BTC price in USD (will be inverted)
|
||||
Source: "coinmarketcap",
|
||||
}
|
||||
m.On("GetRate", mock.Anything, "USD", "BTC").Return(rateDTO, nil)
|
||||
m.On("GetRate", "USD", "BTC").Return(rateDTO, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -91,7 +89,7 @@ func TestConvertCurrencyUseCase_Execute(t *testing.T) {
|
|||
wantCode: "",
|
||||
wantErr: true,
|
||||
setupMock: func(m *mocks.RateProvider) {
|
||||
m.On("GetRate", mock.Anything, "XYZ", "ABC").Return(coinmarketcap.RateDTO{}, domain.ErrRateNotFound)
|
||||
m.On("GetRate", "XYZ", "ABC").Return(coinmarketcap.RateDTO{}, domain.ErrRateNotFound)
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -102,7 +100,7 @@ func TestConvertCurrencyUseCase_Execute(t *testing.T) {
|
|||
wantCode: "",
|
||||
wantErr: true,
|
||||
setupMock: func(m *mocks.RateProvider) {
|
||||
m.On("GetRate", mock.Anything, "USD", "INVALID").Return(coinmarketcap.RateDTO{}, domain.ErrUnsupportedCurrency)
|
||||
m.On("GetRate", "USD", "INVALID").Return(coinmarketcap.RateDTO{}, domain.ErrUnsupportedCurrency)
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
@ -129,7 +127,7 @@ func TestConvertCurrencyUseCase_Execute(t *testing.T) {
|
|||
tt.setupMock(mockProvider)
|
||||
|
||||
// Execute
|
||||
result, err := useCase.Execute(context.Background(), tt.amount, tt.fromCode, tt.toCode)
|
||||
result, err := useCase.Execute(tt.amount, tt.fromCode, tt.toCode)
|
||||
|
||||
// Assert error expectations
|
||||
if tt.wantErr {
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
package mocks
|
||||
|
||||
import (
|
||||
context "context"
|
||||
coinmarketcap "converter/infrastructure/coinmarketcap"
|
||||
|
||||
mock "github.com/stretchr/testify/mock"
|
||||
|
|
@ -14,9 +13,9 @@ type RateProvider struct {
|
|||
mock.Mock
|
||||
}
|
||||
|
||||
// GetRate provides a mock function with given fields: ctx, fromCode, toCode
|
||||
func (_m *RateProvider) GetRate(ctx context.Context, fromCode string, toCode string) (coinmarketcap.RateDTO, error) {
|
||||
ret := _m.Called(ctx, fromCode, toCode)
|
||||
// GetRate provides a mock function with given fields: fromCode, toCode
|
||||
func (_m *RateProvider) GetRate(fromCode string, toCode string) (coinmarketcap.RateDTO, error) {
|
||||
ret := _m.Called(fromCode, toCode)
|
||||
|
||||
if len(ret) == 0 {
|
||||
panic("no return value specified for GetRate")
|
||||
|
|
@ -24,17 +23,17 @@ func (_m *RateProvider) GetRate(ctx context.Context, fromCode string, toCode str
|
|||
|
||||
var r0 coinmarketcap.RateDTO
|
||||
var r1 error
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, string) (coinmarketcap.RateDTO, error)); ok {
|
||||
return rf(ctx, fromCode, toCode)
|
||||
if rf, ok := ret.Get(0).(func(string, string) (coinmarketcap.RateDTO, error)); ok {
|
||||
return rf(fromCode, toCode)
|
||||
}
|
||||
if rf, ok := ret.Get(0).(func(context.Context, string, string) coinmarketcap.RateDTO); ok {
|
||||
r0 = rf(ctx, fromCode, toCode)
|
||||
if rf, ok := ret.Get(0).(func(string, string) coinmarketcap.RateDTO); ok {
|
||||
r0 = rf(fromCode, toCode)
|
||||
} else {
|
||||
r0 = ret.Get(0).(coinmarketcap.RateDTO)
|
||||
}
|
||||
|
||||
if rf, ok := ret.Get(1).(func(context.Context, string, string) error); ok {
|
||||
r1 = rf(ctx, fromCode, toCode)
|
||||
if rf, ok := ret.Get(1).(func(string, string) error); ok {
|
||||
r1 = rf(fromCode, toCode)
|
||||
} else {
|
||||
r1 = ret.Error(1)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"converter/app"
|
||||
"converter/domain"
|
||||
"converter/infrastructure/coinmarketcap"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
|
|
@ -44,22 +40,8 @@ func main() {
|
|||
converter := domain.NewCurrencyConverter()
|
||||
useCase := app.NewConvertCurrencyUseCase(client, converter)
|
||||
|
||||
// Create context with timeout and signal handling
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
// Set up graceful shutdown on interrupt signals
|
||||
sigChan := make(chan os.Signal, 1)
|
||||
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
go func() {
|
||||
<-sigChan
|
||||
fmt.Fprintf(os.Stderr, "\nReceived interrupt signal, shutting down...\n")
|
||||
cancel()
|
||||
}()
|
||||
|
||||
// Execute conversion
|
||||
result, err := useCase.Execute(ctx, amount, fromCurrency, toCurrency)
|
||||
result, err := useCase.Execute(amount, fromCurrency, toCurrency)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
|
|
@ -67,4 +49,4 @@ func main() {
|
|||
|
||||
// Output result
|
||||
fmt.Printf("%s %s\n", result.Amount.StringFixed(8), result.Currency.Code)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
package coinmarketcap
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
|
|
@ -41,16 +40,16 @@ func NewClient(apiKey string) *Client {
|
|||
}
|
||||
|
||||
// GetRate fetches exchange rate from CoinMarketCap API
|
||||
func (c *Client) GetRate(ctx context.Context, fromCode, toCode string) (RateDTO, error) {
|
||||
func (c *Client) GetRate(fromCode, toCode string) (RateDTO, error) {
|
||||
// Try direct conversion first
|
||||
rateDTO, err := c.tryGetRate(ctx, fromCode, toCode)
|
||||
rateDTO, err := c.tryGetRate(fromCode, toCode)
|
||||
if err == nil {
|
||||
return rateDTO, nil
|
||||
}
|
||||
|
||||
// If empty result but no other error, try reverse
|
||||
if err == ErrUnknownSymbol {
|
||||
reverseDTO, reverseErr := c.tryGetRate(ctx, toCode, fromCode)
|
||||
reverseDTO, reverseErr := c.tryGetRate(toCode, fromCode)
|
||||
if reverseErr == nil {
|
||||
return reverseDTO, nil
|
||||
}
|
||||
|
|
@ -70,12 +69,11 @@ func (c *Client) GetRate(ctx context.Context, fromCode, toCode string) (RateDTO,
|
|||
}
|
||||
|
||||
// tryGetRate attempts to get rate in specified direction
|
||||
func (c *Client) tryGetRate(ctx context.Context, fromCode, toCode string) (RateDTO, error) {
|
||||
func (c *Client) tryGetRate(fromCode, toCode string) (RateDTO, error) {
|
||||
var response APIResponse
|
||||
|
||||
// Make API request
|
||||
resp, err := c.httpClient.R().
|
||||
SetContext(ctx).
|
||||
SetQueryParam("symbol", fromCode).
|
||||
SetQueryParam("convert", toCode).
|
||||
SetResult(&response).
|
||||
|
|
|
|||
Loading…
Reference in a new issue