From 245ca69bec95ac3ca94ad4324052556ff93d10e8 Mon Sep 17 00:00:00 2001 From: mazhara Date: Thu, 11 Jan 2024 22:00:28 +0100 Subject: [PATCH] updated krop version (#6) * "Updated krop version" * updated krop version * sceleton for books route --------- Co-authored-by: Olga Mazhara --- README.md | 6 ++- backend/resources/book.json | 52 ++++++++++++++++++++ backend/src/main/scala/snorri/Server.scala | 5 +- backend/src/main/scala/snorri/Snorri.scala | 55 +++++++++++++++++++++- build.sbt | 2 +- 5 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 backend/resources/book.json diff --git a/README.md b/README.md index 408c5fa..c1fef07 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # SnorriScalason -ScalaBridge Project - mentored by Noel Welsh +ScalaBridge Project - mentored by Zainab Ali & Noel Welsh +Tasks: + - Update krop to the latest version + - Change the way krop handles JSON + - Read and serve the JSON \ No newline at end of file diff --git a/backend/resources/book.json b/backend/resources/book.json new file mode 100644 index 0000000..62a6e6f --- /dev/null +++ b/backend/resources/book.json @@ -0,0 +1,52 @@ +[ + { + "id" : "978-0641723445", + "cat" : ["book","hardcover"], + "name" : "The Lightning Thief", + "author" : "Rick Riordan", + "series_t" : "Percy Jackson and the Olympians", + "sequence_i" : 1, + "genre_s" : "fantasy", + "inStock" : true, + "price" : 12.50, + "pages_i" : 384 + } + , + { + "id" : "978-1423103349", + "cat" : ["book","paperback"], + "name" : "The Sea of Monsters", + "author" : "Rick Riordan", + "series_t" : "Percy Jackson and the Olympians", + "sequence_i" : 2, + "genre_s" : "fantasy", + "inStock" : true, + "price" : 6.49, + "pages_i" : 304 + } + , + { + "id" : "978-1857995879", + "cat" : ["book","paperback"], + "name" : "Sophie's World : The Greek Philosophers", + "author" : "Jostein Gaarder", + "sequence_i" : 1, + "genre_s" : "fantasy", + "inStock" : true, + "price" : 3.07, + "pages_i" : 64 + } + , + { + "id" : "978-1933988177", + "cat" : ["book","paperback"], + "name" : "Lucene in Action, Second Edition", + "author" : "Michael McCandless", + "sequence_i" : 1, + "genre_s" : "IT", + "inStock" : true, + "price" : 30.50, + "pages_i" : 475 + } +] + \ No newline at end of file diff --git a/backend/src/main/scala/snorri/Server.scala b/backend/src/main/scala/snorri/Server.scala index 8cd4470..afa31b7 100644 --- a/backend/src/main/scala/snorri/Server.scala +++ b/backend/src/main/scala/snorri/Server.scala @@ -1,10 +1,11 @@ package snorri -import krop.all.* +import krop.all.{*, given} object Server { - val app: Application = Snorri.route.otherwise(Application.notFound) + val app: Application = Snorri.route.orElse(Application.notFound) @main def go(): Unit = ServerBuilder.default.withApplication(app).run() } + diff --git a/backend/src/main/scala/snorri/Snorri.scala b/backend/src/main/scala/snorri/Snorri.scala index 43b6167..7e6e9e5 100644 --- a/backend/src/main/scala/snorri/Snorri.scala +++ b/backend/src/main/scala/snorri/Snorri.scala @@ -1,11 +1,62 @@ package snorri import krop.all.* +import krop.route.InvariantEntity +import org.http4s.headers.`Content-Type` +import org.http4s.syntax.all.mediaType +import org.http4s.circe._ +import cats.effect.IO +import io.circe.Json +import io.circe.parser._ object Snorri { - val route = + // Read some json from a file + // - adding circe as a dependency + val testJson = """[{ + "id" : "978-0641723445", + "cat" : ["book","hardcover"], + "name" : "The Lightning Thief", + "author" : "Rick Riordan", + "series_t" : "Percy Jackson and the Olympians", + "sequence_i" : 1, + "genre_s" : "fantasy", + "inStock" : true, + "price" : 12.50, + "pages_i" : 384 + }]""" + + // Read the books.json file as a string + def readBooks: String = testJson + + def decode(jsonString: String): List[Book] = parce(jsonString) + + final case class Book(id: String) + + def encodeAsJson(book: List[Book]): Json = ??? + + val echoRoute = Route( Request.get(Path.root / "echo" / Param.string), - Response.ok[String] + Response.ok(Entity.text) ).handle(param => s"Your message was ${param}") + +// Write a route that serves some json +// entity encoding with krop +// + val json: InvariantEntity[Json] = + Entity( + CirceEntityDecoder.circeEntityDecoder, + CirceEntityEncoder.circeEntityEncoder + ) + val getBookRoute = { + Route( + Request.get(Path.root / "books"), + Response.ok(json/*.withContentType(`Content-Type`(mediaType"application/json"))*/) + ).handle(_ => encodeAsJson(decode(readBooks))) + } + + // TODO: + // 1: Fill in the case class. + // 2. Get a book with a given id. Filter books by fields + // 3. Have the fields as query parameters. } diff --git a/build.sbt b/build.sbt index 602c89f..ff36968 100644 --- a/build.sbt +++ b/build.sbt @@ -5,7 +5,7 @@ ThisBuild / tlBaseVersion := "0.1" ThisBuild / tlCiHeaderCheck := false val commonSettings = Seq( - libraryDependencies += "org.creativescala" %% "krop-core" % "0.4.1" + libraryDependencies += "org.creativescala" %% "krop-core" % "0.6.0" ) lazy val snorriRoot =