Commit ffbba587 authored by Christophe Henry's avatar Christophe Henry

Add Postgres DB

parent c923e60a
......@@ -17,9 +17,17 @@ repositories {
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
runtimeOnly("org.postgresql:postgresql")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
testImplementation("org.springframework.boot:spring-boot-starter-test") {
exclude(group = "org.junit.vintage", module = "junit-vintage-engine")
}
......
package fr.chenry.demo
import org.springframework.boot.CommandLineRunner
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
@SpringBootApplication
class DemoApplication
class DemoApplication {
@Bean
fun run(taskRepository: TaskRepository): CommandLineRunner = CommandLineRunner {
runCatching {
taskRepository.saveAll(listOf(
Task(name = "Task 1", description = "This is task 1"),
Task(name = "Task 2", description = "This is task 2"),
Task(name = "Task 3", description = "This is task 3")
))
}
}
}
fun main(args: Array<String>) {
runApplication<DemoApplication>(*args)
......
package fr.chenry.demo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.repository.CrudRepository
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import java.lang.StringBuilder
import javax.persistence.*
@RestController
class TaskController {
@Autowired
lateinit var repository: TaskRepository
@RequestMapping("/tasks")
fun index(): String = runCatching {
StringBuilder().apply {
val tasks = repository.findAll()
append("Tasks count: ${tasks.count()}<br/>")
tasks.forEach {
append("<br/>")
append(it.toString())
}
}.toString()
}.getOrDefault("No db connection available")
}
@Entity
data class Task @JvmOverloads constructor(
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long = 0,
var name: String = "",
var description: String = ""
)
interface TaskRepository : CrudRepository<Task, Long>
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://${POSTGRES_DOMAIN}:5432/${POSTGRES_DB}
spring.datasource.username=${POSTGRES_USER}
spring.datasource.password=${POSTGRES_PASSWORD}
spring.datasource.continue-on-error=true
spring.jpa.database=POSTGRESQL
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
server.port=8080
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment