KTLN-18: Kotlin Microservice With Spring Boot (#8913)

This commit is contained in:
maryarm
2020-03-22 07:00:43 +04:30
committed by GitHub
parent d898b9eb3e
commit 401590034f
12 changed files with 277 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
package com.baeldung.bootmicroservice
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class HealthTrackerApplication
fun main(args: Array<String>) {
runApplication<HealthTrackerApplication>(*args)
}

View File

@@ -0,0 +1,28 @@
package com.baeldung.bootmicroservice.config;
import org.springframework.context.annotation.Configuration
import org.springframework.data.r2dbc.core.DatabaseClient
@Configuration
class DBConfiguration(db: DatabaseClient) {
init {
val initDb = db.execute {
""" CREATE TABLE IF NOT EXISTS profile (
id SERIAL PRIMARY KEY,
first_name VARCHAR(20) NOT NULL,
last_name VARCHAR(20) NOT NULL,
birth_date DATE NOT NULL
);
CREATE TABLE IF NOT EXISTS health_record(
id SERIAL PRIMARY KEY,
profile_id LONG NOT NULL,
temperature DECIMAL NOT NULL,
blood_pressure DECIMAL NOT NULL,
heart_rate DECIMAL,
date DATE NOT NULL
);
"""
}
initDb.then().subscribe()
}
}

View File

@@ -0,0 +1,44 @@
package com.baeldung.bootmicroservice.controller
import com.baeldung.bootmicroservice.model.AverageHealthStatus
import com.baeldung.bootmicroservice.model.HealthRecord
import com.baeldung.bootmicroservice.repository.HealthRecordRepository
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.RestController
import reactor.core.publisher.Mono
@RestController
class HealthRecordController(val repository: HealthRecordRepository) {
@PostMapping("/health/{profileId}/record")
fun storeHealthRecord(@PathVariable("profileId") profileId: Long, @RequestBody record: HealthRecord): Mono<HealthRecord> =
repository.save(HealthRecord(null
, profileId
, record.temperature
, record.bloodPressure
, record.heartRate
, record.date))
@GetMapping("/health/{profileId}/avg")
fun fetchHealthRecordAverage(@PathVariable("profileId") profileId: Long): Mono<AverageHealthStatus> =
repository.findByProfileId(profileId)
.reduce(
AverageHealthStatus(0, 0.0, 0.0, 0.0)
, { s, r ->
AverageHealthStatus(s.cnt + 1
, s.temperature + r.temperature
, s.bloodPressure + r.bloodPressure
, s.heartRate + r.heartRate
)
}
).map { s ->
AverageHealthStatus(s.cnt
, if (s.cnt != 0) s.temperature / s.cnt else 0.0
, if (s.cnt != 0) s.bloodPressure / s.cnt else 0.0
, if (s.cnt != 0) s.heartRate / s.cnt else 0.0)
}
}

View File

@@ -0,0 +1,15 @@
package com.baeldung.bootmicroservice.controller
import com.baeldung.bootmicroservice.model.Profile
import com.baeldung.bootmicroservice.repository.ProfileRepository
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Mono
@RestController
class ProfileController(val repository: ProfileRepository) {
@PostMapping("/profile")
fun save(@RequestBody profile: Profile): Mono<Profile> = repository.save(profile)
}

View File

@@ -0,0 +1,3 @@
package com.baeldung.bootmicroservice.model;
class AverageHealthStatus(var cnt: Int, var temperature: Double, var bloodPressure: Double, var heartRate: Double)

View File

@@ -0,0 +1,8 @@
package com.baeldung.bootmicroservice.model
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
import java.time.LocalDate
@Table
data class HealthRecord(@Id var id: Long?, var profileId: Long?, var temperature: Double, var bloodPressure: Double, var heartRate: Double, var date: LocalDate)

View File

@@ -0,0 +1,8 @@
package com.baeldung.bootmicroservice.model
import org.springframework.data.annotation.Id
import org.springframework.data.relational.core.mapping.Table
import java.time.LocalDateTime
@Table
data class Profile(@Id var id:Long?, var firstName : String, var lastName : String, var birthDate: LocalDateTime)

View File

@@ -0,0 +1,13 @@
package com.baeldung.bootmicroservice.repository
import com.baeldung.bootmicroservice.model.HealthRecord
import org.springframework.data.r2dbc.repository.Query
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository
import reactor.core.publisher.Flux
@Repository
interface HealthRecordRepository: ReactiveCrudRepository<HealthRecord, Long> {
@Query("select p.* from health_record p where p.profile_id = :profileId ")
fun findByProfileId(profileId: Long): Flux<HealthRecord>
}

View File

@@ -0,0 +1,8 @@
package com.baeldung.bootmicroservice.repository
import com.baeldung.bootmicroservice.model.Profile
import org.springframework.data.repository.reactive.ReactiveCrudRepository
import org.springframework.stereotype.Repository
@Repository
interface ProfileRepository: ReactiveCrudRepository<Profile, Long>

View File

@@ -0,0 +1 @@
management.endpoints.web.exposure.include: health,metrics

View File

@@ -0,0 +1,51 @@
package com.baeldung.bootmicroservice.controller;
import com.baeldung.bootmicroservice.model.Profile
import com.fasterxml.jackson.databind.ObjectMapper
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
import org.springframework.test.web.reactive.server.WebTestClient
import java.time.LocalDateTime
@SpringBootTest
class ProfileControllerTest {
@Autowired
lateinit var controller: ProfileController
@Autowired
lateinit var mapper: ObjectMapper ;
lateinit var client: WebTestClient
lateinit var profile: String
@BeforeEach
fun setup() {
client = WebTestClient.bindToController(controller).build()
profile = mapper.writeValueAsString(Profile(null, "kotlin", "reactive", LocalDateTime.now()))
}
@Test
fun whenRequestProfile_thenStatusShouldBeOk() {
client.post()
.uri("/profile")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(profile)
.exchange()
.expectStatus().isOk
}
@Test
fun whenRequestProfile_thenIdShouldBeNotNull() {
client.post()
.uri("/profile")
.contentType(MediaType.APPLICATION_JSON)
.bodyValue(profile)
.exchange()
.expectBody()
.jsonPath("$.id")
.isNotEmpty
}
}