From 614092fb7d4892b5734ef4bee8871949abe68839 Mon Sep 17 00:00:00 2001 From: Sambo Chea Date: Fri, 28 Aug 2020 19:27:28 +0700 Subject: [PATCH] Add exchanger from fixer.io and dynamic load rates --- .../com/example/demo/DemoApplication.kt | 28 ++++++++++------- .../com/example/demo/config/BeanConfig.kt | 13 ++++++++ .../kotlin/com/example/demo/domain/Money.kt | 1 + .../com/example/demo/domain/MoneyConfig.kt | 10 +++++++ .../com/example/demo/domain/RateModel.kt | 6 ++++ .../com/example/demo/rest/RestWebClient.kt | 30 +++++++++++++++++++ 6 files changed, 77 insertions(+), 11 deletions(-) create mode 100644 src/main/kotlin/com/example/demo/config/BeanConfig.kt create mode 100644 src/main/kotlin/com/example/demo/domain/RateModel.kt create mode 100644 src/main/kotlin/com/example/demo/rest/RestWebClient.kt diff --git a/src/main/kotlin/com/example/demo/DemoApplication.kt b/src/main/kotlin/com/example/demo/DemoApplication.kt index 697056e..1284d9d 100644 --- a/src/main/kotlin/com/example/demo/DemoApplication.kt +++ b/src/main/kotlin/com/example/demo/DemoApplication.kt @@ -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) } } diff --git a/src/main/kotlin/com/example/demo/config/BeanConfig.kt b/src/main/kotlin/com/example/demo/config/BeanConfig.kt new file mode 100644 index 0000000..1f94d2c --- /dev/null +++ b/src/main/kotlin/com/example/demo/config/BeanConfig.kt @@ -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() + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/domain/Money.kt b/src/main/kotlin/com/example/demo/domain/Money.kt index 5da27d3..882ae5a 100644 --- a/src/main/kotlin/com/example/demo/domain/Money.kt +++ b/src/main/kotlin/com/example/demo/domain/Money.kt @@ -27,6 +27,7 @@ class Money ( enum class Currency (val symbol: Char, val displayName: String) { USD('$', "Dollar"), + EUR('E', ""), KHR('R', "Riel"); companion object { diff --git a/src/main/kotlin/com/example/demo/domain/MoneyConfig.kt b/src/main/kotlin/com/example/demo/domain/MoneyConfig.kt index 7da321e..fbdc6fc 100644 --- a/src/main/kotlin/com/example/demo/domain/MoneyConfig.kt +++ b/src/main/kotlin/com/example/demo/domain/MoneyConfig.kt @@ -38,6 +38,16 @@ object MoneyConfig { return result } + fun loadFrom(rates: List) { + 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 diff --git a/src/main/kotlin/com/example/demo/domain/RateModel.kt b/src/main/kotlin/com/example/demo/domain/RateModel.kt new file mode 100644 index 0000000..f4d7976 --- /dev/null +++ b/src/main/kotlin/com/example/demo/domain/RateModel.kt @@ -0,0 +1,6 @@ +package com.example.demo.domain + +data class RateModel( + var curreny: String? = null, + var rate: Double? = null +) \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/rest/RestWebClient.kt b/src/main/kotlin/com/example/demo/rest/RestWebClient.kt new file mode 100644 index 0000000..4ca82ad --- /dev/null +++ b/src/main/kotlin/com/example/demo/rest/RestWebClient.kt @@ -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(requestUrl) + return response + } + + fun getRequestForModel(requestUrl: String): JsonNode? { + val response = restTemplate.getForObject(requestUrl) + return response + } + + fun getRates(requestUrl: String): List { + val response = getRequestForModel(requestUrl) ?: return emptyList() + val rates = response.get("rates") + val rateData = mutableListOf() + rates.fields() + .forEachRemaining { + rateData.add(RateModel(it.key, it.value.asDouble())) + } + return rateData + } +} \ No newline at end of file