Skip to content

Instantly share code, notes, and snippets.

View renaudmathieu's full-sized avatar

Renaud Mathieu renaudmathieu

View GitHub Profile
@renaudmathieu
renaudmathieu / Application.kt
Created July 5, 2021 15:18
ktor_part3_application_routing
get<Routes.TODO> { // this: PipelineContext<Unit, ApplicationCall>
TodoRepositoryImpl.getAll() // it is launched here 🚀
}
@renaudmathieu
renaudmathieu / TodoRepositoryImpl.kt
Created July 5, 2021 15:18
ktor_part3_TodoRepositoryImpl
class TodoRepositoryImpl: TodoRepository {
override suspend fun create(todo: Todo): Todo = newSuspendedTransaction{
TodoModel.new{
subject = todo.subject
done = todo.done
}.toEntity()
}
override suspend fun getAll(): List<Todo> = newSuspendedTransaction{
TodoModel.all().map(TodoModel::toEntity)
}
@renaudmathieu
renaudmathieu / TodoModel.kt
Created July 5, 2021 15:17
ktor_part3_TodoModel
// Define
class TodoModel(uid: EntityID<UUID>): UUIDEntity(uid) {
companion object: UUIDEntityClass<TodoModel>(TodoTable)
var subject by TodoTable.subject
var done by TodoTable.done
}
@renaudmathieu
renaudmathieu / TodoTable.kt
Created July 5, 2021 15:17
ktor_part3_TodoTable
// Define a table
object TodoTable: UUIDTable(name = "todo", columnName = "uid") {
val subject: Column<String> = text(name = "subject")
val done: Column<Boolean> = bool(name = "done")
}
@renaudmathieu
renaudmathieu / Application.kt
Created July 5, 2021 15:16
ktor_part3_application
fun Application.module(testing: Boolean = false) {
// ...
val database: AppDatabase by inject()
database.init()
}
@renaudmathieu
renaudmathieu / AppDatabase.kt
Created July 5, 2021 15:15
ktor_part3_AppDatabase
@KtorExperimentalAPI
class AppDatabase(
private val config: ApplicationConfig
) {
lateinit var dataSource: DataSource
fun init() {
connectionPool()
orm()
}
private fun connectionPool() {
@renaudmathieu
renaudmathieu / application.conf
Created July 5, 2021 15:15
ktor_part3_application_conf
ktor {
...
database {
connection {
jdbc = "jdbc:postgresql://localhost:5432/postgres"
jdbc = ${?DATABASE_JDBC}
user = admin
user = ${?DATABASE_USER}
password = admin
password = ${?DATABASE_PASSWORD}
@renaudmathieu
renaudmathieu / docker-compose.yml
Created July 5, 2021 15:13
ktor_part3_dockercompose
# For development purpose only
version: "3.7"
services:
postgres:
image: postgres:12-alpine
restart: always
environment:
POSTGRES_DB: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: admin
@renaudmathieu
renaudmathieu / build.gradle.kts
Created June 14, 2021 10:33
openapi-code-generator
plugins {
id("org.openapi.generator") version "5.1.1"
}
openApiGenerate {
generatorName.set("kotlin")
inputSpec.set("./api-client.yml")
outputDir.set("$buildDir")
apiPackage.set("com.innovorder.data.api")
modelPackage.set("com.innovorder.data.models")
object Kotlin {
const val version = "1.5.0"
}
object Ktor {
const val version = "1.5.4"
}
object Moshi {
const val version = "1.12.0"