-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
164 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,4 +39,7 @@ bin/ | |
.vscode/ | ||
|
||
### Mac OS ### | ||
.DS_Store | ||
.DS_Store | ||
|
||
### Misc ### | ||
*.db |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,42 @@ | ||
package sample.htmx.config | ||
|
||
import org.slf4j.LoggerFactory | ||
import com.zaxxer.hikari.HikariConfig | ||
import com.zaxxer.hikari.HikariDataSource | ||
import io.github.cdimascio.dotenv.Dotenv | ||
import org.jdbi.v3.core.Jdbi | ||
import javax.sql.DataSource | ||
|
||
class Database { | ||
private val logger by lazy { LoggerFactory.getLogger(Database::class.java) } | ||
|
||
companion object { | ||
|
||
private val dotenv by lazy { | ||
Dotenv.configure().load() | ||
} | ||
|
||
private val config by lazy { | ||
HikariConfig(dotenv.get("DATASOURCE_PROPERTIES")) | ||
} | ||
|
||
private val dataSource: DataSource by lazy { | ||
HikariDataSource(config) | ||
} | ||
|
||
val jdbi: Jdbi by lazy { | ||
Jdbi.create(dataSource) | ||
} | ||
|
||
fun init() { | ||
jdbi.withHandle<Any, Exception> { handle -> | ||
handle.execute(""" | ||
create table if not exists todos( | ||
id integer primary key auto_increment, | ||
description text not null, | ||
done boolean default false, | ||
created timestamp default now() | ||
); | ||
""".trimIndent()) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,70 @@ | ||
package sample.htmx.service | ||
|
||
import org.jdbi.v3.core.Jdbi | ||
import org.slf4j.LoggerFactory | ||
import sample.htmx.config.Database | ||
import sample.htmx.model.TodoItem | ||
|
||
class TodoService(private val db: Database = Database()) { | ||
class TodoService(private val db: Jdbi = Database.jdbi) { | ||
|
||
private val logger by lazy { LoggerFactory.getLogger(TodoService::class.java) } | ||
fun list(q: String? = ""): List<TodoItem> { | ||
logger.info("list todos") | ||
return listOf(TodoItem(1, "test"), TodoItem(2, "test")) | ||
return db.withHandle<List<TodoItem>, Exception> { handle -> | ||
handle.createQuery( | ||
""" | ||
select * from todos | ||
where lower(description) like lower(:q) | ||
""".trimIndent() | ||
).bind("q", "%$q%").mapToBean(TodoItem::class.java).list() | ||
} | ||
} | ||
|
||
fun find(id: Long): TodoItem { | ||
logger.info("find todos") | ||
return TodoItem(id, "test") | ||
return db.withHandle<TodoItem, Exception> { handle -> | ||
handle.createQuery( | ||
""" | ||
select * from todos | ||
where id = :id | ||
""".trimIndent() | ||
).bind("id", id).mapToBean(TodoItem::class.java).one() | ||
} | ||
} | ||
|
||
fun insert(item: TodoItem): String { | ||
fun insert(item: TodoItem): Int { | ||
logger.info("insert todo") | ||
return "OK" | ||
return db.withHandle<Int, Exception> { handle -> | ||
handle.createUpdate( | ||
""" | ||
insert into todos(description, done) | ||
values (:description, :done) | ||
""".trimIndent() | ||
).bindBean(item).execute() | ||
} | ||
} | ||
|
||
fun update(item: TodoItem): String { | ||
fun update(item: TodoItem): Int { | ||
logger.info("update todo") | ||
return "OK" | ||
return db.withHandle<Int, Exception> { handle -> | ||
handle.createUpdate( | ||
""" | ||
update todos | ||
set description = :description, | ||
done = :done where id = :id | ||
""".trimIndent() | ||
).bindBean(item).execute() | ||
} | ||
} | ||
|
||
fun delete(id: Long): String { | ||
fun delete(id: Long): Int { | ||
logger.info("delete todo") | ||
return "OK" | ||
return db.withHandle<Int, Exception> { handle -> | ||
handle.createUpdate( | ||
""" | ||
delete from todos where id = :id | ||
""".trimIndent() | ||
).bind("id", id).execute() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
# the app is customizable via environment variables | ||
DB_URL= | ||
DB_USER= | ||
DB_PASS= | ||
DATASOURCE_PROPERTIES=/todo-datasource-dev.properties | ||
# either development, test or production | ||
MODE=development |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<form id="form" hx-post="/todos" | ||
hx-on::after-request="this.reset()" hx-swap="outerHTML" hx-target="#table"> | ||
<label>Description | ||
<input name="description" placeholder="New todo here"/> | ||
</label> | ||
<button type="submit">Save</button> | ||
</form> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# src/main/resources/todo-datasource-dev.properties | ||
|
||
driverClassName=org.h2.Driver | ||
jdbcUrl=jdbc:h2:./todo | ||
maximumPoolSize=10 | ||
minimumIdle=2 | ||
username=sa | ||
password=sa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
# the app is customizable via environment variables | ||
DB_URL= | ||
DB_USER= | ||
DB_PASS= | ||
DATASOURCE_PROPERTIES=/todo-datasource-test.properties | ||
# either development, test or production | ||
MODE=test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# src/test/resources/todo-datasource-test.properties | ||
|
||
driverClassName=org.h2.Driver | ||
jdbcUrl=jdbc:h2:mem:todo | ||
maximumPoolSize=10 | ||
minimumIdle=2 | ||
username=sa | ||
password=sa |