converter/domain/services.go

23 lines
612 B
Go

package domain
// CurrencyConverter is a domain service for currency conversion operations
type CurrencyConverter struct{}
// NewCurrencyConverter creates a new CurrencyConverter
func NewCurrencyConverter() *CurrencyConverter {
return &CurrencyConverter{}
}
// Convert converts money from one currency to another using the provided rate
func (c *CurrencyConverter) Convert(money Money, rate Rate) (Money, error) {
if money.Currency.Code != rate.From.Code {
return Money{}, ErrCurrencyMismatch
}
convertedAmount := money.Amount.Mul(rate.Value)
return NewMoneyFromDecimal(convertedAmount, rate.To)
}