aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Odersky <jakob@inpher.io>2019-11-25 21:10:47 -0500
committerJakob Odersky <jakob@inpher.io>2019-11-26 13:40:36 -0500
commitc458a1bd7d173419232ee6e49f3a06ddb4bb7c39 (patch)
tree403f172aa3cf8de1e6e9fa85f82a79d88d684594
parent03dce17329e8a430fbaf7a468a6db1779f62fa19 (diff)
downloadscala-tutorial-c458a1bd7d173419232ee6e49f3a06ddb4bb7c39.tar.gz
scala-tutorial-c458a1bd7d173419232ee6e49f3a06ddb4bb7c39.tar.bz2
scala-tutorial-c458a1bd7d173419232ee6e49f3a06ddb4bb7c39.zip
Add quill and sqlite to back message store
-rw-r--r--.gitignore2
-rw-r--r--build.sc4
-rw-r--r--server/src/Main.scala31
3 files changed, 32 insertions, 5 deletions
diff --git a/.gitignore b/.gitignore
index 89f9ac0..daa0caf 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,3 @@
out/
+
+messages.db
diff --git a/build.sc b/build.sc
index 73216b7..2a544bc 100644
--- a/build.sc
+++ b/build.sc
@@ -5,7 +5,9 @@ object server extends ScalaModule {
def ivyDeps = Agg(
ivy"com.lihaoyi::cask:0.3.6", // web framework, http://www.lihaoyi.com/cask/
- ivy"com.lihaoyi::scalatags:0.7.0" // html rendering DSL http://www.lihaoyi.com/scalatags/
+ ivy"com.lihaoyi::scalatags:0.7.0", // html rendering DSL http://www.lihaoyi.com/scalatags/
+ ivy"io.getquill::quill-jdbc:3.4.10", // language integrated queries https://getquill.io/#docs
+ ivy"org.xerial:sqlite-jdbc:3.28.0" // actual database driver, note the single ':'
)
}
diff --git a/server/src/Main.scala b/server/src/Main.scala
index fdcabb8..9d17fa4 100644
--- a/server/src/Main.scala
+++ b/server/src/Main.scala
@@ -1,16 +1,39 @@
+
+import io.getquill.{SnakeCase, SqliteJdbcContext}
import scala.collection.mutable
+import com.typesafe.config.ConfigFactory
+import java.nio.file.Files
+
object Main extends cask.MainRoutes {
- val messages = mutable.ListBuffer.empty[Message]
+ object ctx extends SqliteJdbcContext(SnakeCase, ConfigFactory.parseString(
+ // This config uses the HOCON syntax https://github.com/lightbend/config/blob/master/HOCON.md
+ // It is usually read from a file.
+ s"""{driverClassName = "org.sqlite.JDBC", jdbcUrl = "jdbc:sqlite:messages.db"}"""
+ ))
+ import ctx._ // this import allows us to construct SQL queries in a nice DSL
- messages += Message("John Smith", 0, "Hello, world!")
+ // tables aren't typically create from within a program, but we do it here'
+ // for demonstration purposes
+ ctx.executeAction(
+ """
+ create table if not exists message(
+ author string not null,
+ date integer not null,
+ content string not null
+ );
+ """
+ )
@cask.get("/")
- def get() = Templates.page(messages.toList).render
+ def get() = {
+ val messages: Seq[Message] = ctx.run(ctx.query[Message])
+ Templates.page(messages).render
+ }
@cask.postJson("/")
def post(message: Message) = {
- messages += message
+ run(query[Message].insert(lift(message)))
}
@cask.staticResources("/assets")