diff --git a/build.gradle.kts b/build.gradle.kts index 2fd2f15..e43e4cf 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,10 +1,11 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile plugins { + java id("org.springframework.boot") version "2.3.3.RELEASE" id("io.spring.dependency-management") version "1.0.10.RELEASE" - kotlin("jvm") version "1.3.72" - kotlin("plugin.spring") version "1.3.72" + kotlin("jvm") version "1.4.0" + kotlin("plugin.spring") version "1.4.0" } group = "com.example" @@ -18,6 +19,11 @@ repositories { dependencies { implementation("org.springframework.boot:spring-boot-starter-web") implementation("com.fasterxml.jackson.module:jackson-module-kotlin") + implementation("io.springfox:springfox-swagger2:2.9.2") + implementation("io.springfox:springfox-swagger-ui:2.9.2") + + developmentOnly("org.springframework.boot:spring-boot-devtools") + implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") testImplementation("org.springframework.boot:spring-boot-starter-test") { diff --git a/src/main/kotlin/com/example/demo/DemoApplication.kt b/src/main/kotlin/com/example/demo/DemoApplication.kt index 1284d9d..2af1268 100644 --- a/src/main/kotlin/com/example/demo/DemoApplication.kt +++ b/src/main/kotlin/com/example/demo/DemoApplication.kt @@ -4,6 +4,10 @@ import com.example.demo.domain.Currency import com.example.demo.domain.Money import com.example.demo.domain.MoneyConfig import com.example.demo.domain.exchange +import com.example.demo.exchange.FixerIOExchangeAdapter +import com.example.demo.exchange.LocalExchangeAdapter +import com.example.demo.exchange.MoneyExchange +import com.example.demo.exchange.YahooExchangeAdapter import com.example.demo.rest.RestWebClient import org.springframework.boot.CommandLineRunner import org.springframework.boot.autoconfigure.SpringBootApplication @@ -43,14 +47,24 @@ class DemoApplication : CommandLineRunner { // // 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 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) - val moneyUsd = Money(10.0, currency = Currency.USD) - val moneyResult = moneyUsd.exchange("KHR") - println(moneyResult) + // val exchangeAdapter1 = LocalExchangeAdapter() + // val exchangeAdapter2 = FixerIOExchangeAdapter() + // val exchangeAdapter3 = YahooExchangeAdapter() + // + // val exchanger = MoneyExchange(exchangeAdapter3) + // + // val moneyUsd = Money(10.0) + // val result = exchanger.exchange(moneyUsd, "KHR") + // println(result) } } diff --git a/src/main/kotlin/com/example/demo/config/SpringFoxConfig.kt b/src/main/kotlin/com/example/demo/config/SpringFoxConfig.kt new file mode 100644 index 0000000..3db471c --- /dev/null +++ b/src/main/kotlin/com/example/demo/config/SpringFoxConfig.kt @@ -0,0 +1,22 @@ +package com.example.demo.config + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import springfox.documentation.builders.PathSelectors +import springfox.documentation.builders.RequestHandlerSelectors +import springfox.documentation.spi.DocumentationType +import springfox.documentation.spring.web.plugins.Docket +import springfox.documentation.swagger2.annotations.EnableSwagger2 + +@Configuration +@EnableSwagger2 +class SpringFoxConfig { + @Bean + fun api(): Docket? { + return Docket(DocumentationType.SWAGGER_2) + .select() + .apis(RequestHandlerSelectors.any()) + .paths(PathSelectors.any()) + .build() + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/config/WebConfig.kt b/src/main/kotlin/com/example/demo/config/WebConfig.kt new file mode 100644 index 0000000..0c12273 --- /dev/null +++ b/src/main/kotlin/com/example/demo/config/WebConfig.kt @@ -0,0 +1,17 @@ +package com.example.demo.config + +import org.springframework.context.annotation.Configuration +import org.springframework.web.servlet.config.annotation.EnableWebMvc +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer + +@Configuration +@EnableWebMvc +class WebConfig : WebMvcConfigurer { + override fun addResourceHandlers(registry: ResourceHandlerRegistry) { + registry.addResourceHandler("swagger-ui.html") + .addResourceLocations("classpath:/META-INF/resources/") + registry.addResourceHandler("/webjars/**") + .addResourceLocations("classpath:/META-INF/resources/webjars/") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/exchange/ExchangeAdapter.kt b/src/main/kotlin/com/example/demo/exchange/ExchangeAdapter.kt new file mode 100644 index 0000000..83b5baf --- /dev/null +++ b/src/main/kotlin/com/example/demo/exchange/ExchangeAdapter.kt @@ -0,0 +1,10 @@ +package com.example.demo.exchange + +interface ExchangeAdapter { + /** + * @param source String Source Currency + * @param sourceAmount String Source Amount + * @return targetAmount Double + */ + fun exchange(source: String, sourceAmount: Double, target: String): Double +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/exchange/FixerIOExchangeAdapter.kt b/src/main/kotlin/com/example/demo/exchange/FixerIOExchangeAdapter.kt new file mode 100644 index 0000000..7dc1348 --- /dev/null +++ b/src/main/kotlin/com/example/demo/exchange/FixerIOExchangeAdapter.kt @@ -0,0 +1,7 @@ +package com.example.demo.exchange + +class FixerIOExchangeAdapter : ExchangeAdapter { + override fun exchange(source: String, sourceAmount: Double, target: String): Double { + TODO("Not yet implemented") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/exchange/LocalExchangeAdapter.kt b/src/main/kotlin/com/example/demo/exchange/LocalExchangeAdapter.kt new file mode 100644 index 0000000..dc11913 --- /dev/null +++ b/src/main/kotlin/com/example/demo/exchange/LocalExchangeAdapter.kt @@ -0,0 +1,7 @@ +package com.example.demo.exchange + +class LocalExchangeAdapter : ExchangeAdapter { + override fun exchange(source: String, sourceAmount: Double, target: String): Double { + TODO("Not yet implemented") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/exchange/MoneyExchange.kt b/src/main/kotlin/com/example/demo/exchange/MoneyExchange.kt new file mode 100644 index 0000000..3249890 --- /dev/null +++ b/src/main/kotlin/com/example/demo/exchange/MoneyExchange.kt @@ -0,0 +1,14 @@ +package com.example.demo.exchange + +import com.example.demo.domain.Currency +import com.example.demo.domain.Money +import com.example.demo.domain.StdMoney + +class MoneyExchange ( + private val exchangeAdapter: ExchangeAdapter +) { + fun exchange(source: StdMoney, toCurrency: String): StdMoney { + val exchangeRate = exchangeAdapter.exchange(source.getMoneyCurrency(), source.getMoneyValue(), toCurrency) + return Money(value = exchangeRate, currency = Currency.create(toCurrency)) + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/exchange/YahooExchangeAdapter.kt b/src/main/kotlin/com/example/demo/exchange/YahooExchangeAdapter.kt new file mode 100644 index 0000000..2b73c51 --- /dev/null +++ b/src/main/kotlin/com/example/demo/exchange/YahooExchangeAdapter.kt @@ -0,0 +1,7 @@ +package com.example.demo.exchange + +class YahooExchangeAdapter : ExchangeAdapter { + override fun exchange(source: String, sourceAmount: Double, target: String): Double { + TODO("Not yet implemented") + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/demo/rest/ApiController.kt b/src/main/kotlin/com/example/demo/rest/ApiController.kt new file mode 100644 index 0000000..2e65e2d --- /dev/null +++ b/src/main/kotlin/com/example/demo/rest/ApiController.kt @@ -0,0 +1,39 @@ +package com.example.demo.rest + +import org.springframework.web.bind.annotation.DeleteMapping +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.PostMapping +import org.springframework.web.bind.annotation.RequestBody +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RestController +@RequestMapping("/people") +class ApiController { + private val people = mutableListOf() + + @GetMapping + fun index(): List { + return people + } + + @PostMapping + fun create(@RequestBody person: Person): Person { + people.add(person) + + return person + } + + @DeleteMapping("/{id}") + fun delete(@PathVariable id: Long): String { + val person = people.firstOrNull { it.id == id } ?: return "not found" + people.remove(person) + return "deleted" + } +} + +data class Person( + var id: Long? = null, + var name: String? = null +) \ No newline at end of file