Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Application/v1/actions/actionCurrency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()})
}
11 changes: 11 additions & 0 deletions Application/v1/actions/actionUpdater.go
Original file line number Diff line number Diff line change
@@ -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()
}
4 changes: 2 additions & 2 deletions Application/v1/mapper/CurrencyMapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
1 change: 1 addition & 0 deletions Application/v1/routing/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func GenRouter() *gin.Engine {
v1 := router.Group("/v1")
{
v1.POST("/", actions.CurrencyHandler)
v1.POST("/update", actions.UpdaterHandler)
}
return router
}
6 changes: 0 additions & 6 deletions Application/v1/viewModels/CurrencyViewModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions Domain/Models/CurrencyChangeModel.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ 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
}

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}
}
4 changes: 2 additions & 2 deletions Domain/domainModelInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type CurrencyRateInterface interface {
}

type CurrencyChangeInterface interface {
GetWantedCurrency() CurrencyInterface
GetBaseCurrency() CurrencyInterface
GetWantedCurrency() string
GetBaseCurrency() string
GetAmount() int
}

Expand Down
1 change: 1 addition & 0 deletions Domain/repositoriesInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package Domain
type RepositoryInterface interface {
Set(from CurrencyInterface, to CurrencyInterface, value RateInterface) error
Get(from CurrencyInterface, to CurrencyInterface) RateInterface
Print()
}
5 changes: 4 additions & 1 deletion Domain/services/ExchangerService.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package services

import (
"fmt"

"github.com/apmath-web/currency/Domain"
domainModels "github.com/apmath-web/currency/Domain/Models"
)
Expand All @@ -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()))
}

Expand Down
17 changes: 17 additions & 0 deletions Domain/services/UpdaterService.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -25,6 +40,8 @@ func (instance *Updater) Update() error {
domainModels.GenRate(currency1.GetRate()/currency2.GetRate()))
}
}

// instance.repository.Print()
return nil
}

Expand Down
11 changes: 10 additions & 1 deletion Infrastructure/ServiceManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type ServiceManagerClass struct {
}

func (sm *ServiceManagerClass) GetRepository() Domain.RepositoryInterface {
return repositories.GenRepository()
return GenRepository()
}

func (sm *ServiceManagerClass) GetFetcher() Domain.FetcherInterface {
Expand All @@ -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
}
7 changes: 3 additions & 4 deletions Infrastructure/applicationModels/Fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package applicationModels

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -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

}
Expand Down
31 changes: 19 additions & 12 deletions Infrastructure/repositories/Repository.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
}