Initial commit
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
plugins {
|
||||
id("kotlin-application")
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation("org.slf4j:slf4j-api:2.0.0-alpha1")
|
||||
runtimeOnly("org.slf4j:slf4j-simple:2.0.0-alpha1")
|
||||
implementation("org.ktorm:ktorm-core:3.3.0")
|
||||
implementation("com.h2database:h2:1.4.200")
|
||||
implementation("org.jetbrains.kotlinx:kotlinx-html:0.7.3")
|
||||
implementation("io.javalin:javalin:3.13.6")
|
||||
implementation("org.ocpsoft.prettytime:prettytime:5.0.1.Final")
|
||||
}
|
||||
|
||||
application {
|
||||
mainClass.set("be.vandewalleh.issueslog.MainKt")
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
org.slf4j.simpleLogger.logFile=System.out
|
||||
org.slf4j.simpleLogger.showDateTime=true
|
||||
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss:SSS Z
|
||||
org.slf4j.simpleLogger.defaultLogLevel=info
|
||||
|
||||
# Logging detail level for a SimpleLogger instance named "xxxxx".
|
||||
# Must be one of ("trace", "debug", "info", "warn", or "error").
|
||||
# If not specified, the default logging detail level is used.
|
||||
#org.slf4j.simpleLogger.log.xxxxx=
|
||||
@@ -0,0 +1,51 @@
|
||||
package be.vandewalleh.issueslog
|
||||
|
||||
import kotlinx.html.*
|
||||
|
||||
@HtmlTagMarker
|
||||
fun TagConsumer<*>.head() {
|
||||
script(src = "https://unpkg.com/htmx.org@1.3.3") {}
|
||||
script(src = "https://unpkg.com/hyperscript.org@0.0.9") {}
|
||||
link(
|
||||
href = "https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css",
|
||||
rel = "stylesheet"
|
||||
)
|
||||
title("Issues log")
|
||||
}
|
||||
|
||||
@HtmlTagMarker
|
||||
fun TagConsumer<*>.issues(issues: List<IssueEntity>) {
|
||||
issues.forEach {
|
||||
div("card") {
|
||||
style = "margin: 6px"
|
||||
div("card-header") { +it.created.pretty() }
|
||||
div("card-body") {
|
||||
h5("card-title text-center") { +it.name }
|
||||
p("card-text") { +it.cause }
|
||||
div {
|
||||
button(classes = "btn btn-outline-danger btn-sm") {
|
||||
hxDelete = "/issues?id=${it.id}"
|
||||
hxTarget = "#issues"
|
||||
+"Delete"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@HtmlTagMarker
|
||||
fun TagConsumer<*>.requiredInput(label: String, name: String) {
|
||||
div("mb-3") {
|
||||
label("form-label") {
|
||||
`for` = name
|
||||
+label
|
||||
}
|
||||
input(classes = "form-control") {
|
||||
id = name
|
||||
this.name = name
|
||||
required = true
|
||||
maxLength = "255"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package be.vandewalleh.issueslog
|
||||
|
||||
import org.ktorm.database.Database
|
||||
import org.ktorm.dsl.delete
|
||||
import org.ktorm.dsl.eq
|
||||
import org.ktorm.entity.add
|
||||
import org.ktorm.entity.sequenceOf
|
||||
import org.ktorm.entity.sortedByDescending
|
||||
import org.ktorm.entity.toList
|
||||
|
||||
class IssuesRepository(private val database: Database) {
|
||||
fun create(issueEntity: IssueEntity) {
|
||||
database.sequenceOf(IssuesTable).add(issueEntity)
|
||||
}
|
||||
|
||||
fun all() = database.sequenceOf(IssuesTable).sortedByDescending { it.created }.toList()
|
||||
|
||||
fun delete(id: Int) {
|
||||
database.delete(IssuesTable) {
|
||||
it.id eq id
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package be.vandewalleh.issueslog
|
||||
|
||||
import io.javalin.Javalin
|
||||
import kotlinx.html.*
|
||||
import org.h2.jdbcx.JdbcDataSource
|
||||
import org.ktorm.database.Database
|
||||
|
||||
fun main() {
|
||||
val dataSource = JdbcDataSource()
|
||||
.apply { setURL("jdbc:h2:./issues;DB_CLOSE_DELAY=-1;CASE_INSENSITIVE_IDENTIFIERS=TRUE") }
|
||||
val database = Database.connect(dataSource).apply { createTables() }
|
||||
val repo = IssuesRepository(database)
|
||||
val app = Javalin.create()
|
||||
|
||||
app.get("/") { ctx ->
|
||||
ctx.document {
|
||||
head()
|
||||
body {
|
||||
style = "padding:1.5rem"
|
||||
form {
|
||||
hxPost = "/new"
|
||||
hxTarget = "#issues"
|
||||
hs = "on submit set #name.value to '' set #cause.value to '' call #name.focus()"
|
||||
requiredInput(label = "Name", name = "name")
|
||||
requiredInput(label = "Cause", name = "cause")
|
||||
button(classes = "btn btn-outline-primary") { +"Add" }
|
||||
}
|
||||
h1 { +"Issues Log" }
|
||||
div {
|
||||
id = "issues"
|
||||
style = "display:flex;flex-wrap:wrap"
|
||||
issues(repo.all())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
app.post("/new") { ctx ->
|
||||
repo.create(IssueEntity {
|
||||
name = ctx.formParam("name")!!
|
||||
cause = ctx.formParam("cause")!!
|
||||
})
|
||||
ctx.fragment { issues(repo.all()) }
|
||||
}
|
||||
|
||||
app.delete("/issues") { ctx ->
|
||||
repo.delete(ctx.queryParam("id")!!.toInt())
|
||||
ctx.fragment { issues(repo.all()) }
|
||||
}
|
||||
|
||||
app.start(System.getenv("PORT")?.toIntOrNull() ?: 9000)
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package be.vandewalleh.issueslog
|
||||
|
||||
import org.ktorm.database.Database
|
||||
import org.ktorm.entity.Entity
|
||||
import org.ktorm.entity.EntitySequence
|
||||
import org.ktorm.entity.sequenceOf
|
||||
import org.ktorm.schema.Table
|
||||
import org.ktorm.schema.int
|
||||
import org.ktorm.schema.timestamp
|
||||
import org.ktorm.schema.varchar
|
||||
import java.time.Instant
|
||||
|
||||
object IssuesTable : Table<IssueEntity>("Issues") {
|
||||
val id = int("id").primaryKey().bindTo { it.id }
|
||||
val name = varchar("name").bindTo { it.name }
|
||||
val cause = varchar("cause").bindTo { it.cause }
|
||||
val created = timestamp("created").bindTo { it.created }
|
||||
}
|
||||
|
||||
interface IssueEntity : Entity<IssueEntity> {
|
||||
companion object : Entity.Factory<IssueEntity>()
|
||||
|
||||
var id: Int
|
||||
var name: String
|
||||
var cause: String
|
||||
var created: Instant
|
||||
}
|
||||
|
||||
fun Database.createTables() {
|
||||
useConnection { connection ->
|
||||
connection.prepareStatement(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS Issues (
|
||||
id int auto_increment primary key,
|
||||
name varchar not null,
|
||||
cause varchar not null,
|
||||
created timestamp not null default CURRENT_TIMESTAMP()
|
||||
);
|
||||
""".trimIndent()
|
||||
).execute()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package be.vandewalleh.issueslog
|
||||
|
||||
import io.javalin.http.Context
|
||||
import kotlinx.html.*
|
||||
import kotlinx.html.stream.*
|
||||
import org.ocpsoft.prettytime.PrettyTime
|
||||
import java.time.Instant
|
||||
import kotlin.properties.ReadWriteProperty
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class AttributeDelegate(private val name: String? = null) : ReadWriteProperty<Tag, String> {
|
||||
override fun getValue(thisRef: Tag, property: KProperty<*>): String {
|
||||
throw UnsupportedOperationException()
|
||||
}
|
||||
|
||||
override fun setValue(thisRef: Tag, property: KProperty<*>, value: String) {
|
||||
val name = this.name ?: property.name.camelToKebabCase()
|
||||
thisRef.attributes[name] = value
|
||||
}
|
||||
|
||||
companion object {
|
||||
private val camelRegex = "(?<=[a-zA-Z])[A-Z]".toRegex()
|
||||
private fun String.camelToKebabCase() = camelRegex.replace(this) { "-${it.value}" }.lowercase()
|
||||
}
|
||||
}
|
||||
|
||||
var Tag.hxTarget by AttributeDelegate()
|
||||
var Tag.hxGet by AttributeDelegate()
|
||||
var Tag.hxPost by AttributeDelegate()
|
||||
var Tag.hxDelete by AttributeDelegate()
|
||||
|
||||
var Tag.hs by AttributeDelegate("_")
|
||||
|
||||
var LABEL.`for` by AttributeDelegate("for")
|
||||
|
||||
fun Context.document(block: TagConsumer<StringBuilder>.() -> Unit) {
|
||||
header("Content-Type", "text/html; charset=utf-8")
|
||||
result(buildString {
|
||||
append("<!DOCTYPE html>\n")
|
||||
appendHTML().apply(block)
|
||||
})
|
||||
}
|
||||
|
||||
fun Context.fragment(block: TagConsumer<StringBuilder>.() -> Unit) {
|
||||
header("Content-Type", "text/html; charset=utf-8")
|
||||
result(buildString {
|
||||
appendHTML().apply(block)
|
||||
})
|
||||
}
|
||||
|
||||
private val prettyTime by lazy(LazyThreadSafetyMode.NONE) { PrettyTime() }
|
||||
fun Instant.pretty(): String = prettyTime.format(this)
|
||||
Reference in New Issue
Block a user