diff --git a/Application/v1/actions/actionCurrency.go b/Application/v1/actions/actionCurrency.go index e6cd20c..88b27d9 100644 --- a/Application/v1/actions/actionCurrency.go +++ b/Application/v1/actions/actionCurrency.go @@ -32,13 +32,13 @@ func CurrencyHandler(c *gin.Context) { dm := mapper.CurrencyMapper(vm) - updater := Infrastructure.GetServiceManager().GetUpdaterService() - updater.Update() + // updater := Infrastructure.GetServiceManager().GetUpdaterService() + // updater.Update() exchanger := Infrastructure.GetServiceManager().GetExchangerService() ans := exchanger.Exchange(dm) - fmt.Print(ans.GetAmount()) + fmt.Print("Answer", ans.GetAmount()) // fetcher := applicationModels.Fetcher{} // rates := fetcher.FetchAll() @@ -48,5 +48,5 @@ func CurrencyHandler(c *gin.Context) { // log.Fatal(err) // } - // c.JSON(http.StatusCreated, gin.H{"amount": resultAmount, "currency": dm.GetWantedCurrency().GetName()}) + c.JSON(http.StatusCreated, gin.H{"amount": ans.GetAmount(), "currency": dm.GetWantedCurrency()}) } diff --git a/Application/v1/actions/actionUpdater.go b/Application/v1/actions/actionUpdater.go new file mode 100644 index 0000000..100c050 --- /dev/null +++ b/Application/v1/actions/actionUpdater.go @@ -0,0 +1,11 @@ +package actions + +import ( + "github.com/apmath-web/currency/Infrastructure" + "github.com/gin-gonic/gin" +) + +func UpdaterHandler(c *gin.Context) { + updater := Infrastructure.GetServiceManager().GetUpdaterService() + updater.Update() +} diff --git a/Application/v1/mapper/CurrencyMapper.go b/Application/v1/mapper/CurrencyMapper.go index 2e59085..92bd502 100644 --- a/Application/v1/mapper/CurrencyMapper.go +++ b/Application/v1/mapper/CurrencyMapper.go @@ -7,8 +7,8 @@ import ( ) func CurrencyMapper(vm viewModels.CurrencyViewModel) Domain.CurrencyChangeInterface { - currentCurrency := domainModels.GenCurrency(vm.GetCurrentCurrency()) - wantedCurrency := domainModels.GenCurrency(vm.GetWantedCurrency()) + currentCurrency := vm.GetCurrentCurrency() + wantedCurrency := vm.GetWantedCurrency() amount := vm.GetAmount() return domainModels.GenCurrencyChange(currentCurrency, wantedCurrency, amount) } diff --git a/Application/v1/routing/router.go b/Application/v1/routing/router.go index 143a7d5..88c4a5f 100644 --- a/Application/v1/routing/router.go +++ b/Application/v1/routing/router.go @@ -10,6 +10,7 @@ func GenRouter() *gin.Engine { v1 := router.Group("/v1") { v1.POST("/", actions.CurrencyHandler) + v1.POST("/update", actions.UpdaterHandler) } return router } diff --git a/Application/v1/viewModels/CurrencyViewModel.go b/Application/v1/viewModels/CurrencyViewModel.go index bde03aa..df3bf9a 100644 --- a/Application/v1/viewModels/CurrencyViewModel.go +++ b/Application/v1/viewModels/CurrencyViewModel.go @@ -85,12 +85,6 @@ func (c *CurrencyViewModel) UnmarshalJSON(b []byte) error { return nil } -func (c *CurrencyViewModel) Hydrate(model Domain.CurrencyChangeInterface) { - c.Amount = model.GetAmount() - c.CurrentCurrency = model.GetBaseCurrency().GetName() - c.WantedCurrency = model.GetWantedCurrency().GetName() -} - func (c *CurrencyViewModel) Validate() bool { if c.validateAmount() && c.validateCurrentCurrency() && c.validateWantedCurrency() { return true diff --git a/Domain/Models/CurrencyChangeModel.go b/Domain/Models/CurrencyChangeModel.go index 094296e..61f09f9 100644 --- a/Domain/Models/CurrencyChangeModel.go +++ b/Domain/Models/CurrencyChangeModel.go @@ -4,11 +4,11 @@ import "github.com/apmath-web/currency/Domain" type CurrencyChange struct { amount int - baseCurrency Domain.CurrencyInterface - wantedCurrency Domain.CurrencyInterface + baseCurrency string + wantedCurrency string } -func (i *CurrencyChange) GetBaseCurrency() Domain.CurrencyInterface { +func (i *CurrencyChange) GetBaseCurrency() string { return i.baseCurrency } @@ -16,10 +16,10 @@ func (i *CurrencyChange) GetAmount() int { return i.amount } -func (i *CurrencyChange) GetWantedCurrency() Domain.CurrencyInterface { +func (i *CurrencyChange) GetWantedCurrency() string { return i.wantedCurrency } -func GenCurrencyChange(baseCurrency Domain.CurrencyInterface, wantedCurrency Domain.CurrencyInterface, amount int) Domain.CurrencyChangeInterface { +func GenCurrencyChange(baseCurrency string, wantedCurrency string, amount int) Domain.CurrencyChangeInterface { return &CurrencyChange{amount, baseCurrency, wantedCurrency} } diff --git a/Domain/domainModelInterface.go b/Domain/domainModelInterface.go index 146c589..663467f 100644 --- a/Domain/domainModelInterface.go +++ b/Domain/domainModelInterface.go @@ -15,8 +15,8 @@ type CurrencyRateInterface interface { } type CurrencyChangeInterface interface { - GetWantedCurrency() CurrencyInterface - GetBaseCurrency() CurrencyInterface + GetWantedCurrency() string + GetBaseCurrency() string GetAmount() int } diff --git a/Domain/repositoriesInterface.go b/Domain/repositoriesInterface.go index 899be93..8f47598 100644 --- a/Domain/repositoriesInterface.go +++ b/Domain/repositoriesInterface.go @@ -3,4 +3,5 @@ package Domain type RepositoryInterface interface { Set(from CurrencyInterface, to CurrencyInterface, value RateInterface) error Get(from CurrencyInterface, to CurrencyInterface) RateInterface + Print() } diff --git a/Domain/services/ExchangerService.go b/Domain/services/ExchangerService.go index a0e7fc6..a33f3d6 100644 --- a/Domain/services/ExchangerService.go +++ b/Domain/services/ExchangerService.go @@ -1,6 +1,8 @@ package services import ( + "fmt" + "github.com/apmath-web/currency/Domain" domainModels "github.com/apmath-web/currency/Domain/Models" ) @@ -10,7 +12,8 @@ type Exchanger struct { } func (instance *Exchanger) Exchange(data Domain.CurrencyChangeInterface) Domain.AmountInterface { - rate := instance.repository.Get(data.GetWantedCurrency(), data.GetBaseCurrency()) + rate := instance.repository.Get(domainModels.GenCurrency(data.GetWantedCurrency()), domainModels.GenCurrency(data.GetBaseCurrency())) + fmt.Print("FromExhanger Rate", rate) return domainModels.GenAmount(int(float64(data.GetAmount()) * rate.GetRate())) } diff --git a/Domain/services/UpdaterService.go b/Domain/services/UpdaterService.go index d381407..945e77e 100644 --- a/Domain/services/UpdaterService.go +++ b/Domain/services/UpdaterService.go @@ -17,6 +17,21 @@ func (instance *Updater) Update() error { domainModels.GenRate(1.0)) rates := instance.fetcher.FetchAll() + + for _, currency := range rates { + instance.repository.Set( + domainModels.GenCurrency("RUB"), + domainModels.GenCurrency(currency.GetCurrency()), + domainModels.GenRate(currency.GetRate()), + ) + instance.repository.Set( + domainModels.GenCurrency(currency.GetCurrency()), + domainModels.GenCurrency("RUB"), + domainModels.GenRate(1/currency.GetRate()), + ) + + } + for _, currency1 := range rates { for _, currency2 := range rates { instance.repository.Set( @@ -25,6 +40,8 @@ func (instance *Updater) Update() error { domainModels.GenRate(currency1.GetRate()/currency2.GetRate())) } } + + // instance.repository.Print() return nil } diff --git a/Infrastructure/ServiceManager.go b/Infrastructure/ServiceManager.go index 5c9471c..d5fd1e9 100644 --- a/Infrastructure/ServiceManager.go +++ b/Infrastructure/ServiceManager.go @@ -13,7 +13,7 @@ type ServiceManagerClass struct { } func (sm *ServiceManagerClass) GetRepository() Domain.RepositoryInterface { - return repositories.GenRepository() + return GenRepository() } func (sm *ServiceManagerClass) GetFetcher() Domain.FetcherInterface { @@ -39,3 +39,12 @@ func GetServiceManager() *ServiceManagerClass { }) return serviceManager } + +var repo *repositories.Repository + +func GenRepository() Domain.RepositoryInterface { + once.Do(func() { + repo = &repositories.Repository{Rates: make(map[string]map[string]float64)} + }) + return repo +} \ No newline at end of file diff --git a/Infrastructure/applicationModels/Fetcher.go b/Infrastructure/applicationModels/Fetcher.go index 7515887..aa76b9e 100644 --- a/Infrastructure/applicationModels/Fetcher.go +++ b/Infrastructure/applicationModels/Fetcher.go @@ -2,7 +2,6 @@ package applicationModels import ( "encoding/json" - "fmt" "io/ioutil" "log" "net/http" @@ -38,9 +37,9 @@ func (i *Fetcher) FetchAll() []Domain.FetchRateInterface { rate := infoAboutCurrency["Value"].(float64) fetchRates = append(fetchRates, GenFetchRate(key, rate)) } - for _, obj := range fetchRates { - fmt.Print(obj.GetCurrency(), " ", obj.GetRate(), "\n") - } + // for _, obj := range fetchRates { + // //fmt.Print(obj.GetCurrency(), " ", obj.GetRate(), "\n") + // } return fetchRates } diff --git a/Infrastructure/repositories/Repository.go b/Infrastructure/repositories/Repository.go index 0bd4e1c..8d3b66a 100644 --- a/Infrastructure/repositories/Repository.go +++ b/Infrastructure/repositories/Repository.go @@ -1,31 +1,38 @@ package repositories import ( - "sync" - + "fmt" "github.com/apmath-web/currency/Domain" domainModels "github.com/apmath-web/currency/Domain/Models" ) type Repository struct { - rates map[string]map[string]float64 + Rates map[string]map[string]float64 } func (repository *Repository) Set(from Domain.CurrencyInterface, to Domain.CurrencyInterface, value Domain.RateInterface) error { - repository.rates[from.GetName()][to.GetName()] = value.GetRate() + //mm, ok :=repository.rates[from.GetName()][to.GetName()] = value.GetRate() + mm, ok := repository.Rates[from.GetName()] + + if !ok { + mm = make(map[string]float64) + repository.Rates[from.GetName()] = mm + } + + repository.Rates[from.GetName()][to.GetName()] = value.GetRate() return nil } func (repository *Repository) Get(from Domain.CurrencyInterface, to Domain.CurrencyInterface) Domain.RateInterface { - return domainModels.GenRate(repository.rates[from.GetName()][to.GetName()]) + return domainModels.GenRate(repository.Rates[from.GetName()][to.GetName()]) } -var repo *Repository -var once sync.Once +func (repository *Repository) Print() { -func GenRepository() Domain.RepositoryInterface { - once.Do(func() { - repo = &Repository{make(map[string]map[string]float64)} - }) - return repo + fmt.Print("=======Print repo==========\n") + for k1 := range repository.Rates { + for k2 := range repository.Rates[k1] { + fmt.Print(k1, " ", k2, " ", repository.Rates[k1][k2], "\n") + } + } }