From aa102a610bd2cb0fda180fd6a71f073065e770f1 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Tue, 4 Jun 2019 00:18:46 +0300 Subject: [PATCH 1/4] added action --- Application/v1/actions/actionCurrency.go | 8 ++++---- Application/v1/actions/actionUpdater.go | 11 +++++++++++ Application/v1/routing/router.go | 1 + Domain/repositoriesInterface.go | 1 + Domain/services/ExchangerService.go | 4 ++++ Domain/services/UpdaterService.go | 17 +++++++++++++++++ Infrastructure/applicationModels/Fetcher.go | 7 +++---- Infrastructure/repositories/Repository.go | 19 +++++++++++++++++++ 8 files changed, 60 insertions(+), 8 deletions(-) create mode 100644 Application/v1/actions/actionUpdater.go diff --git a/Application/v1/actions/actionCurrency.go b/Application/v1/actions/actionCurrency.go index e6cd20c..0ed93c0 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().GetName()}) } 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/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/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..652a4f6 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" ) @@ -11,6 +13,8 @@ type Exchanger struct { func (instance *Exchanger) Exchange(data Domain.CurrencyChangeInterface) Domain.AmountInterface { rate := instance.repository.Get(data.GetWantedCurrency(), 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/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..9f472b1 100644 --- a/Infrastructure/repositories/Repository.go +++ b/Infrastructure/repositories/Repository.go @@ -1,6 +1,7 @@ package repositories import ( + "fmt" "sync" "github.com/apmath-web/currency/Domain" @@ -12,6 +13,14 @@ type Repository struct { } func (repository *Repository) Set(from Domain.CurrencyInterface, to Domain.CurrencyInterface, value Domain.RateInterface) error { + //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 } @@ -20,6 +29,16 @@ func (repository *Repository) Get(from Domain.CurrencyInterface, to Domain.Curre return domainModels.GenRate(repository.rates[from.GetName()][to.GetName()]) } +func (repository *Repository) Print() { + + 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") + } + } +} + var repo *Repository var once sync.Once From 89000aa6e5fa94541bfa4cf2267f7d61fc2a4325 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Tue, 4 Jun 2019 12:14:55 +0300 Subject: [PATCH 2/4] temp --- Application/v1/mapper/CurrencyMapper.go | 4 ++-- Application/v1/viewModels/CurrencyViewModel.go | 6 ------ Domain/Models/CurrencyChangeModel.go | 10 +++++----- Domain/domainModelInterface.go | 4 ++-- 4 files changed, 9 insertions(+), 15 deletions(-) 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/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 } From 61681fe616d4199aa7d7c909db10fd3f961170cb Mon Sep 17 00:00:00 2001 From: Anatoly Date: Tue, 4 Jun 2019 12:35:11 +0300 Subject: [PATCH 3/4] change currencyChange --- Application/v1/actions/actionCurrency.go | 2 +- Domain/services/ExchangerService.go | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Application/v1/actions/actionCurrency.go b/Application/v1/actions/actionCurrency.go index 0ed93c0..88b27d9 100644 --- a/Application/v1/actions/actionCurrency.go +++ b/Application/v1/actions/actionCurrency.go @@ -48,5 +48,5 @@ func CurrencyHandler(c *gin.Context) { // log.Fatal(err) // } - c.JSON(http.StatusCreated, gin.H{"amount": ans.GetAmount(), "currency": dm.GetWantedCurrency().GetName()}) + c.JSON(http.StatusCreated, gin.H{"amount": ans.GetAmount(), "currency": dm.GetWantedCurrency()}) } diff --git a/Domain/services/ExchangerService.go b/Domain/services/ExchangerService.go index 652a4f6..a33f3d6 100644 --- a/Domain/services/ExchangerService.go +++ b/Domain/services/ExchangerService.go @@ -12,8 +12,7 @@ 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())) } From b04ff32e9d7971290f0cc2b8e0f83c0c4a2bb5bd Mon Sep 17 00:00:00 2001 From: KostyaLukyanchikov Date: Tue, 4 Jun 2019 14:09:14 +0300 Subject: [PATCH 4/4] move GenRepository to SM --- Infrastructure/ServiceManager.go | 11 ++++++++- Infrastructure/repositories/Repository.go | 28 +++++++---------------- 2 files changed, 18 insertions(+), 21 deletions(-) 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/repositories/Repository.go b/Infrastructure/repositories/Repository.go index 9f472b1..8d3b66a 100644 --- a/Infrastructure/repositories/Repository.go +++ b/Infrastructure/repositories/Repository.go @@ -2,49 +2,37 @@ package repositories import ( "fmt" - "sync" - "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 { //mm, ok :=repository.rates[from.GetName()][to.GetName()] = value.GetRate() - mm, ok := repository.rates[from.GetName()] + mm, ok := repository.Rates[from.GetName()] if !ok { mm = make(map[string]float64) - repository.rates[from.GetName()] = mm + repository.Rates[from.GetName()] = mm } - repository.rates[from.GetName()][to.GetName()] = value.GetRate() + 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()]) } func (repository *Repository) Print() { 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") + for k1 := range repository.Rates { + for k2 := range repository.Rates[k1] { + fmt.Print(k1, " ", k2, " ", repository.Rates[k1][k2], "\n") } } } - -var repo *Repository -var once sync.Once - -func GenRepository() Domain.RepositoryInterface { - once.Do(func() { - repo = &Repository{make(map[string]map[string]float64)} - }) - return repo -}