Define domain service
This commit is contained in:
parent
4716b5105f
commit
9fd6c01458
22
domain/services.go
Normal file
22
domain/services.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
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)
|
||||
}
|
||||
|
||||
Loading…
Reference in a new issue