Skip to content

Instantly share code, notes, and snippets.

View codemwnci's full-sized avatar

Wayne Ellis codemwnci

View GitHub Profile
@codemwnci
codemwnci / MainController.kt
Last active August 25, 2017 20:35
Get all todos
get("") { req, res ->
val todos: List<Todo> = using(sessionOf(HikariCP.dataSource())) { session ->
session.run( queryOf("select id, text, done, created_at from todo").map(toTodo).asList )
}
jacksonObjectMapper().writeValueAsString(todos)
}
@codemwnci
codemwnci / MainController.kt
Last active August 25, 2017 20:37
Create a new Todo, plus some helper classes / functions
fun main(args: Array<String>) {
setupDB()
port(9000)
staticFileLocation("/public/")
path("/todo/") {
data class Todo(val id: Long, val text: String, val done: Boolean, val createdAt: LocalDateTime)
val toTodo: (Row) -> Todo = { row -> Todo(row.long(1), row.string(2), row.boolean(3), row.localDateTime(4))}
@codemwnci
codemwnci / MainController.kt
Last active August 25, 2017 20:38
Get a single Todo
get(":id") { req, res ->
jacksonObjectMapper().writeValueAsString( getTodo(req.params("id").toLong()) )
}
@codemwnci
codemwnci / pom.xml
Created August 25, 2017 20:38
POM for the Todo App
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>codemwnci</groupId>
<artifactId>KotlinToDoApp</artifactId>
<version>1.0-SNAPSHOT</version>
@codemwnci
codemwnci / MainController.kt
Created August 25, 2017 20:44
Delete a Todo
delete(":id") { req, res ->
val rowsDeleted = using(sessionOf(HikariCP.dataSource())) { session ->
session.run(queryOf("delete from todo where id=?", req.params("id").toLong()).asUpdate)
}
if (rowsDeleted == 1) "ok"
else serverError("something went wrong")
}
@codemwnci
codemwnci / MainController.kt
Created August 25, 2017 20:54
Update a todo
put(":id") { req, res ->
val update = jacksonObjectMapper().readTree(req.body())
if (!update.has("text") || !update.has("done")) badRequest("text and done required in JSON body string")
val rowsUpdated = using(sessionOf(HikariCP.dataSource())) { session ->
session.run(queryOf("update todo set text=?, done=? where id=?",
update.get("text").asText(), update.get("done").asBoolean(), req.params("id").toLong())
.asUpdate)
}
@codemwnci
codemwnci / MainControllerTest.kt
Created August 25, 2017 21:06
TodoListControllerTest
package codemwnci
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.github.kittinunf.fuel.Fuel
import org.junit.*
import kotlin.test.*
import spark.Spark
class UsageTest {
companion object {
@codemwnci
codemwnci / index.html
Created August 25, 2017 21:25
VueJS Setup
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VueJS / Kotlin ToDo App</title>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://unpkg.com/vue"></script>
</head>
<body>
@codemwnci
codemwnci / index.html
Created August 25, 2017 21:38
Add a Todo / VueJS
<body>
<div id="app">
<input type="text" @keyup.enter="addTodo">
<ul>
<li v-for="todo in todos">
{{todo.text}}
</li>
</ul>
</div>
@codemwnci
codemwnci / index.html
Created August 25, 2017 21:46
Load all todos on startup
mounted() {
axios.get('http://localhost:9000/todo/').then(response => {
this.todos = response.data;
});
}