Skip to content

Commit

Permalink
Second day's work
Browse files Browse the repository at this point in the history
  • Loading branch information
flippingbits committed Apr 2, 2019
1 parent 35e47bf commit 526e76c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 52 deletions.
18 changes: 17 additions & 1 deletion app/controllers/DocumentController.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package controllers

import akka.actor.Status.Success
import javax.inject.Inject
import models.DocumentRepository
import play.api.libs.json.Json
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._

import scala.concurrent.ExecutionContext
Expand All @@ -11,4 +12,19 @@ class DocumentController @Inject()(cc: ControllerComponents, documentRepository:
def index = Action.async { implicit request =>
documentRepository.list().map(documents => Ok(Json.toJson(documents)))
}

def getById(id: Long) = Action.async { implicit request =>
documentRepository.getById(id).map(document => Ok(Json.toJson(document)))
}

def createDocument = Action.async(parse.json) { implicit request: Request[JsValue] =>
documentRepository.create(
(request.body \ "title").toString,
(request.body \ "body").toString
).map(document => Created(Json.toJson(document)))
}

def testz = Action.async { implicit request =>
documentRepository.list().map(documents => Ok(Json.toJson(documents)))
}
}
17 changes: 15 additions & 2 deletions app/models/Document.scala
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package models

import play.api.libs.json._
import play.api.libs.json.Writes._
import play.api.libs.functional.syntax._

case class Document(id: Long, title: String, body: String)
case class Document(id: Long, title: String, body: String) {
def hashtags = {
"""#(\w+)""".r.findAllIn(body).matchData.map(_.group(1).toLowerCase).toList
}
}

object Document {
implicit val documentFormat = Json.format[Document]
implicit val documentWrites: Writes[Document] = (
(JsPath \ "id").write[Long] and
(JsPath \ "title").write[String] and
(JsPath \ "body").write[String] and
(JsPath \ "hashtags").write[List[String]]
)((d: Document) => (d.id, d.title, d.body, d.hashtags))

//implicit val documentFormat = Json.format[Document]
}
11 changes: 11 additions & 0 deletions app/models/DocumentRepository.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class DocumentRepository @Inject() (dbConfigProvider: DatabaseConfigProvider)(im
documents.result
}

def getById(id: Long) = db.run {
documents.filter(_.id === id).result.head
}

def create(title: String, body: String): Future[Document] = db.run {
(
documents.map(p => (p.title, p.body))
Expand All @@ -34,4 +38,11 @@ class DocumentRepository @Inject() (dbConfigProvider: DatabaseConfigProvider)(im
) += (title, body)
}

def deleteAll() = db.run{
documents.delete
}

def resetAutoInc() = db.run {
sqlu"ALTER TABLE documents AUTO_INCREMENT=1;"
}
}
4 changes: 3 additions & 1 deletion conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@
# This file defines all application routes (Higher priority routes first)
# https://www.playframework.com/documentation/latest/ScalaRouting
#
GET /documents controllers.DocumentController.index
GET /documents controllers.DocumentController.index
GET /documents/:id controllers.DocumentController.getById(id: Long)
POST /documents controllers.DocumentController.createDocument
71 changes: 23 additions & 48 deletions test/controllers/DocumentControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import play.api.inject.guice.GuiceApplicationBuilder
import play.api.test._
import play.api.test.Helpers._

import scala.concurrent.Await
import slick.jdbc.MySQLProfile.api._

import scala.concurrent.{Await, ExecutionContext}
import scala.concurrent.duration._

/*
Expand All @@ -32,27 +34,19 @@ class DocumentControllerSpec extends PlaySpec
.build()
.injector
val documentRepository = injector.instanceOf[DocumentRepository]
implicit val ec = ExecutionContext.global

override def beforeEach() {
Await.ready(
documentRepository.create(
Await.ready(documentRepository.deleteAll, 10 seconds)
Await.ready(documentRepository.resetAutoInc(), 5 seconds)
Await.ready(documentRepository.create(
"Quarkus, a Kubernetes Native Java Framework",
"Red Hat has released #Quarkus, a #Kubernetes native #Java framework tailored for GraalVM and OpenJDK HotSpot."
),
10 seconds
)
Await.ready(
documentRepository.create(
"Java 11 Released",
"#Java 11 has arrived. The new release is the first planned appearance of #Oracle's #LTS releases, although #Oracle has also grandfathered in Java 8 as an LTS release to help bridge the gap between the old release model and the new approach."
),
10 seconds
)
/*
* Note that you typically do not need to provide an id when creating database records through an ORM or any other access layer.
* create Document(id = 1, title = "Quarkus, a Kubernetes Native Java Framework", body = "Red Hat has released #Quarkus, a #Kubernetes native #Java framework tailored for GraalVM and OpenJDK HotSpot.")
* create Document(id = 2, title = "Java 11 Released", body = "#Java 11 has arrived. The new release is the first planned appearance of #Oracle's #LTS releases, although #Oracle has also grandfathered in Java 8 as an LTS release to help bridge the gap between the old release model and the new approach.")
*/
"Red Hat has released #Quarkus, a #Kubernetes native #Java framework tailored for GraalVM and OpenJDK HotSpot."
), 5 seconds)
Await.ready(documentRepository.create(
"Java 11 Released",
"#Java 11 has arrived. The new release is the first planned appearance of #Oracle's #LTS releases, although #Oracle has also grandfathered in Java 8 as an LTS release to help bridge the gap between the old release model and the new approach."
), 5 seconds)
}

"GET /documents" should {
Expand Down Expand Up @@ -99,7 +93,7 @@ class DocumentControllerSpec extends PlaySpec
val result = route(app, FakeRequest(GET, "/documents/1")).get
val responseBody = contentAsString(result)

responseBody must include(""""hashtags": ["quarkus", "kubernetes", "java"]""")
responseBody must include(""""hashtags":["quarkus","kubernetes","java"]""")
}
}

Expand All @@ -111,25 +105,19 @@ class DocumentControllerSpec extends PlaySpec
POST,
"/documents",
FakeHeaders(List("HOST"->"localhost", "Content-type"->"application/json")),
"""{
| "title": "Mashreq Bank’s Lean Agile Journey",
| "body": "After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."
|}"""
"""{"title":"Mashreq Bank’s Lean Agile Journey","body":"After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."}"""
)).get
status(result) mustBe CREATED
}

"return JSON" in {
n "return JSON" in {
val result = route(
app,
FakeRequest(
POST,
"/documents",
FakeHeaders(List("HOST"->"localhost", "Content-type"->"application/json")),
"""{
| "title": "Mashreq Bank’s Lean Agile Journey",
| "body": "After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."
|}"""
"""{"title":"Mashreq Bank’s Lean Agile Journey","body":"After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."}"""
)).get
contentType(result) mustBe Some("application/json")
}
Expand All @@ -141,10 +129,7 @@ class DocumentControllerSpec extends PlaySpec
POST,
"/documents",
FakeHeaders(List("HOST"->"localhost", "Content-type"->"application/json")),
"""{
| "title": "Mashreq Bank’s Lean Agile Journey",
| "body": "After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."
|}"""
"""{"title":"Mashreq Bank’s Lean Agile Journey","body":"After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."}"""
)).get
contentAsString(result) must include("Mashreq Bank’s Lean Agile Journey")
}
Expand All @@ -156,12 +141,9 @@ class DocumentControllerSpec extends PlaySpec
POST,
"/documents",
FakeHeaders(List("HOST"->"localhost", "Content-type"->"application/json")),
"""{
| "title": "Mashreq Bank’s Lean Agile Journey",
| "body": "After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."
|}"""
"""{"title":"Mashreq Bank’s Lean Agile Journey","body":"After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."}"""
)).get
contentAsString(result) must include(""""hashtags": ["lean", "agile"]""")
contentAsString(result) must include(""""hashtags":["lean","agile"]""")
}

"add the document to the database" in {
Expand All @@ -171,10 +153,7 @@ class DocumentControllerSpec extends PlaySpec
POST,
"/documents",
FakeHeaders(List("HOST"->"localhost", "Content-type"->"application/json")),
"""{
| "title": "Mashreq Bank’s Lean Agile Journey",
| "body": "After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."
|}"""
"""{"title":"Mashreq Bank’s Lean Agile Journey","body":"After having seen and evidenced the tangible benefit of #lean at Mashreq Bank, #agile was seen as a natural progression, an evolutionary step."}"""
)).get
status(result) mustBe CREATED
val indexResult = route(app, FakeRequest(GET, "/documents")).get
Expand All @@ -190,9 +169,7 @@ class DocumentControllerSpec extends PlaySpec
POST,
"/documents",
FakeHeaders(List("HOST"->"localhost", "Content-type"->"application/json")),
"""{
| "title": "Mashreq Bank’s Lean Agile Journey"
|}"""
"""{"title":"Mashreq Bank’s Lean Agile Journey"}"""
)).get
status(result) mustBe BAD_REQUEST
}
Expand All @@ -204,9 +181,7 @@ class DocumentControllerSpec extends PlaySpec
POST,
"/documents",
FakeHeaders(List("HOST"->"localhost", "Content-type"->"application/json")),
"""{
| "title": "Mashreq Bank’s Lean Agile Journey"
|}"""
"""{"title":"Mashreq Bank’s Lean Agile Journey"}"""
)).get
status(result) mustBe BAD_REQUEST
val indexResult = route(app, FakeRequest(GET, "/documents")).get
Expand Down

0 comments on commit 526e76c

Please sign in to comment.