Updated swagger ui and apis

This commit is contained in:
Sambo Chea 2020-09-01 19:09:24 +07:00
parent 614092fb7d
commit 49a3e35a06
10 changed files with 152 additions and 9 deletions

View File

@ -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") {

View File

@ -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)
}
}

View File

@ -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()
}
}

View File

@ -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/")
}
}

View File

@ -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
}

View File

@ -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")
}
}

View File

@ -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")
}
}

View File

@ -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))
}
}

View File

@ -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")
}
}

View File

@ -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<Person>()
@GetMapping
fun index(): List<Person> {
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
)