Add exchanger from fixer.io and dynamic load rates
This commit is contained in:
parent
9e7e50a1ba
commit
614092fb7d
@ -3,11 +3,8 @@ package com.example.demo
|
|||||||
import com.example.demo.domain.Currency
|
import com.example.demo.domain.Currency
|
||||||
import com.example.demo.domain.Money
|
import com.example.demo.domain.Money
|
||||||
import com.example.demo.domain.MoneyConfig
|
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.exchange
|
||||||
import com.example.demo.domain.plus
|
import com.example.demo.rest.RestWebClient
|
||||||
import com.example.demo.domain.toStdMoneyFormatable
|
|
||||||
import org.springframework.boot.CommandLineRunner
|
import org.springframework.boot.CommandLineRunner
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication
|
import org.springframework.boot.autoconfigure.SpringBootApplication
|
||||||
import org.springframework.boot.runApplication
|
import org.springframework.boot.runApplication
|
||||||
@ -27,7 +24,7 @@ class DemoApplication : CommandLineRunner {
|
|||||||
// val result = MoneyConfig.parse(config)
|
// val result = MoneyConfig.parse(config)
|
||||||
// println(result)
|
// 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 value1 = MoneyDyn(value = 2.0, currency = "USD")
|
||||||
// val value2 = MoneyDyn(value = 20000.0, currency = "KHR")
|
// val value2 = MoneyDyn(value = 20000.0, currency = "KHR")
|
||||||
@ -38,13 +35,22 @@ class DemoApplication : CommandLineRunner {
|
|||||||
// // val result = value1.exchange("KHR")
|
// // val result = value1.exchange("KHR")
|
||||||
// println(value1.exchangeTo("KHR")) // 7
|
// println(value1.exchangeTo("KHR")) // 7
|
||||||
|
|
||||||
val moneyUsd = Money(2.0)
|
// val moneyUsd = Money(2.0)
|
||||||
.addMoney(20000.0, "USD")
|
// .addMoney(20000.0, "USD")
|
||||||
.exchange("KHR")
|
// .exchange("KHR")
|
||||||
.toStdMoneyFormatable()
|
// .toStdMoneyFormatable()
|
||||||
.formatMoneyDisplay()
|
// .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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
13
src/main/kotlin/com/example/demo/config/BeanConfig.kt
Normal file
13
src/main/kotlin/com/example/demo/config/BeanConfig.kt
Normal 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()
|
||||||
|
}
|
||||||
|
}
|
@ -27,6 +27,7 @@ class Money (
|
|||||||
|
|
||||||
enum class Currency (val symbol: Char, val displayName: String) {
|
enum class Currency (val symbol: Char, val displayName: String) {
|
||||||
USD('$', "Dollar"),
|
USD('$', "Dollar"),
|
||||||
|
EUR('E', ""),
|
||||||
KHR('R', "Riel");
|
KHR('R', "Riel");
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
@ -38,6 +38,16 @@ object MoneyConfig {
|
|||||||
return result
|
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
|
// all currencies with its rate
|
||||||
fun getConfig() = this.config
|
fun getConfig() = this.config
|
||||||
|
|
||||||
|
6
src/main/kotlin/com/example/demo/domain/RateModel.kt
Normal file
6
src/main/kotlin/com/example/demo/domain/RateModel.kt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
package com.example.demo.domain
|
||||||
|
|
||||||
|
data class RateModel(
|
||||||
|
var curreny: String? = null,
|
||||||
|
var rate: Double? = null
|
||||||
|
)
|
30
src/main/kotlin/com/example/demo/rest/RestWebClient.kt
Normal file
30
src/main/kotlin/com/example/demo/rest/RestWebClient.kt
Normal 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
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user