Add exchanger from fixer.io and dynamic load rates

This commit is contained in:
Sambo Chea 2020-08-28 19:27:28 +07:00
parent 9e7e50a1ba
commit 614092fb7d
6 changed files with 77 additions and 11 deletions

View File

@ -3,11 +3,8 @@ package com.example.demo
import com.example.demo.domain.Currency
import com.example.demo.domain.Money
import com.example.demo.domain.MoneyConfig
import com.example.demo.domain.MoneyDyn
import com.example.demo.domain.addMoney
import com.example.demo.domain.exchange
import com.example.demo.domain.plus
import com.example.demo.domain.toStdMoneyFormatable
import com.example.demo.rest.RestWebClient
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@ -27,7 +24,7 @@ class DemoApplication : CommandLineRunner {
// val result = MoneyConfig.parse(config)
// println(result)
MoneyConfig.parse("USD=1,KHR=4000,EUR=0.99")
// MoneyConfig.parse("USD=1,KHR=4000,EUR=0.99")
// val value1 = MoneyDyn(value = 2.0, currency = "USD")
// val value2 = MoneyDyn(value = 20000.0, currency = "KHR")
@ -38,13 +35,22 @@ class DemoApplication : CommandLineRunner {
// // val result = value1.exchange("KHR")
// println(value1.exchangeTo("KHR")) // 7
val moneyUsd = Money(2.0)
.addMoney(20000.0, "USD")
.exchange("KHR")
.toStdMoneyFormatable()
.formatMoneyDisplay()
// val moneyUsd = Money(2.0)
// .addMoney(20000.0, "USD")
// .exchange("KHR")
// .toStdMoneyFormatable()
// .formatMoneyDisplay()
//
// println(moneyUsd)
println(moneyUsd)
val url = "http://data.fixer.io/api/latest?access_key=381f3bac3cb8beed200cc4e17c0f8bb4&symbols=USD,KHR,EUR"
val result = RestWebClient.getRates(url)
// println(result)
MoneyConfig.loadFrom(result)
val moneyUsd = Money(10.0, currency = Currency.USD)
val moneyResult = moneyUsd.exchange("KHR")
println(moneyResult)
}
}

View File

@ -0,0 +1,13 @@
package com.example.demo.config
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.web.client.RestTemplate
@Configuration
class BeanConfig {
@Bean
fun restTemplate(): RestTemplate {
return RestTemplate()
}
}

View File

@ -27,6 +27,7 @@ class Money (
enum class Currency (val symbol: Char, val displayName: String) {
USD('$', "Dollar"),
EUR('E', ""),
KHR('R', "Riel");
companion object {

View File

@ -38,6 +38,16 @@ object MoneyConfig {
return result
}
fun loadFrom(rates: List<RateModel>) {
this.config.clear()
rates.forEach {
if (!it.curreny.isNullOrEmpty()) {
this.config[it.curreny!!] = it.rate ?: 1.0
}
}
}
// all currencies with its rate
fun getConfig() = this.config

View File

@ -0,0 +1,6 @@
package com.example.demo.domain
data class RateModel(
var curreny: String? = null,
var rate: Double? = null
)

View File

@ -0,0 +1,30 @@
package com.example.demo.rest
import com.example.demo.domain.RateModel
import com.fasterxml.jackson.databind.JsonNode
import org.springframework.web.client.RestTemplate
import org.springframework.web.client.getForObject
object RestWebClient {
private val restTemplate = RestTemplate()
fun getRequest(requestUrl: String): Any? {
val response = restTemplate.getForObject<Any>(requestUrl)
return response
}
fun getRequestForModel(requestUrl: String): JsonNode? {
val response = restTemplate.getForObject<JsonNode>(requestUrl)
return response
}
fun getRates(requestUrl: String): List<RateModel> {
val response = getRequestForModel(requestUrl) ?: return emptyList()
val rates = response.get("rates")
val rateData = mutableListOf<RateModel>()
rates.fields()
.forEachRemaining {
rateData.add(RateModel(it.key, it.value.asDouble()))
}
return rateData
}
}