Skip to content

Commit

Permalink
Address code remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
drmarjanovic committed Dec 21, 2022
1 parent 049aad9 commit 17363fa
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 29 deletions.
20 changes: 11 additions & 9 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ lazy val root =
lazy val library =
project
.in(file("modules/library"))
.disablePlugins(RevolverPlugin)
.settings(stdSettings("zio-elasticsearch"))
.settings(scalacOptions += "-language:higherKinds")
.configs(IntegrationTest)
.settings(
Defaults.itSettings,
libraryDependencies ++= List(
"com.softwaremill.sttp.client3" %% "zio" % "3.8.3",
"com.softwaremill.sttp.client3" %% "zio-json" % "3.8.3",
"com.softwaremill.sttp.client3" %% "zio" % "3.8.5",
"com.softwaremill.sttp.client3" %% "zio-json" % "3.8.5",
"dev.zio" %% "zio-json" % "0.3.0",
"dev.zio" %% "zio-prelude" % "1.0.0-RC16",
"dev.zio" %% "zio-schema" % "0.3.1",
"dev.zio" %% "zio-schema-json" % "0.3.1",
"dev.zio" %% "zio-schema" % "0.4.1",
"dev.zio" %% "zio-schema-json" % "0.4.1",
"org.apache.commons" % "commons-lang3" % "3.12.0",
"dev.zio" %% "zio-test" % "2.0.5" % Tests,
"dev.zio" %% "zio-test-sbt" % "2.0.5" % Tests
Expand All @@ -55,13 +56,13 @@ lazy val example =
.settings(
libraryDependencies ++= List(
"dev.zio" %% "zio" % "2.0.5",
"dev.zio" %% "zio-config" % "3.0.2",
"dev.zio" %% "zio-config-magnolia" % "3.0.2",
"dev.zio" %% "zio-config-typesafe" % "3.0.2",
"dev.zio" %% "zio-config" % "3.0.6",
"dev.zio" %% "zio-config-magnolia" % "3.0.6",
"dev.zio" %% "zio-config-typesafe" % "3.0.6",
"dev.zio" %% "zio-http" % "0.0.3",
"dev.zio" %% "zio-json" % "0.3.0",
"dev.zio" %% "zio-schema" % "0.3.1",
"dev.zio" %% "zio-schema-json" % "0.3.1"
"dev.zio" %% "zio-schema" % "0.4.1",
"dev.zio" %% "zio-schema-json" % "0.4.1"
)
)
.dependsOn(library)
Expand All @@ -73,6 +74,7 @@ lazy val docs =
project
.in(file("modules/docs"))
.enablePlugins(MdocPlugin, DocusaurusPlugin, ScalaUnidocPlugin)
.disablePlugins(RevolverPlugin)
.dependsOn(library)
.settings(
publish / skip := true,
Expand Down
2 changes: 1 addition & 1 deletion modules/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This application represents an example of usage `zio-elasticsearch` library for
repository)
- Start the application by running the following command:
```shell
./sbt example/run
./sbt "~example/reStart"
```
- Check whether the application is running [here](http://localhost:9000/health)
- Explore endpoints using Postman collection (`zio-elasticsearch-example.postman_collection.json`)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import zio.schema.{DeriveSchema, Schema}

import java.time.LocalDateTime

final case class Repository(
final case class GitHubRepo(
id: Option[String],
organization: String,
name: String,
Expand All @@ -16,8 +16,8 @@ final case class Repository(
forks: Int
)

object Repository {
implicit val schema: Schema[Repository] = DeriveSchema.gen[Repository]
object GitHubRepo {
implicit val schema: Schema[GitHubRepo] = DeriveSchema.gen[GitHubRepo]

implicit val encoder: JsonEncoder[Repository] = DeriveJsonEncoder.gen[Repository]
implicit val encoder: JsonEncoder[GitHubRepo] = DeriveJsonEncoder.gen[GitHubRepo]
}
10 changes: 7 additions & 3 deletions modules/example/src/main/scala/example/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@ import scala.io.Source
object Main extends ZIOAppDefault {

override def run: Task[ExitCode] = {
val elasticConfigLive = ZLayer(getConfig[ElasticsearchConfig].map(es => ElasticConfig(es.host, es.port)))
val elasticExecutorLive = elasticConfigLive >>> ElasticExecutor.live
val elasticConfigLive = ZLayer(getConfig[ElasticsearchConfig].map(es => ElasticConfig(es.host, es.port)))

(prepare *> runServer).provide(AppConfig.live, elasticExecutorLive, HttpClientZioBackend.layer())
(prepare *> runServer).provide(
AppConfig.live,
elasticConfigLive,
ElasticExecutor.live,
HttpClientZioBackend.layer()
)
}

private[this] def prepare: RIO[ElasticExecutor, Unit] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ import zio.{RIO, ULayer, ZIO, ZLayer}

final class RepositoriesElasticsearch {

def findById(organization: String, id: String): RIO[ElasticExecutor, Option[Repository]] =
def findById(organization: String, id: String): RIO[ElasticExecutor, Option[GitHubRepo]] =
ElasticRequest
.getById[Repository](Index, DocumentId(id))
.getById[GitHubRepo](Index, DocumentId(id))
.routing(unsafeWrap(Routing)(organization))
.execute
.map(_.toOption)

def create(repository: Repository): RIO[ElasticExecutor, Option[DocumentId]] =
def create(repository: GitHubRepo): RIO[ElasticExecutor, Option[DocumentId]] =
ElasticRequest
.create(Index, repository)
.routing(unsafeWrap(Routing)(repository.organization))
.execute

def upsert(id: String, repository: Repository): RIO[ElasticExecutor, Unit] =
def upsert(id: String, repository: GitHubRepo): RIO[ElasticExecutor, Unit] =
ElasticRequest
.upsert(Index, DocumentId(id), repository)
.routing(unsafeWrap(Routing)(repository.organization))
Expand All @@ -40,13 +40,13 @@ object RepositoriesElasticsearch {
def findById(
organization: String,
id: String
): RIO[ElasticExecutor with RepositoriesElasticsearch, Option[Repository]] =
): RIO[ElasticExecutor with RepositoriesElasticsearch, Option[GitHubRepo]] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.findById(organization, id))

def create(repository: Repository): RIO[ElasticExecutor with RepositoriesElasticsearch, Option[DocumentId]] =
def create(repository: GitHubRepo): RIO[ElasticExecutor with RepositoriesElasticsearch, Option[DocumentId]] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.create(repository))

def upsert(id: String, repository: Repository): RIO[ElasticExecutor with RepositoriesElasticsearch, Unit] =
def upsert(id: String, repository: GitHubRepo): RIO[ElasticExecutor with RepositoriesElasticsearch, Unit] =
ZIO.serviceWithZIO[RepositoriesElasticsearch](_.upsert(id, repository))

def remove(
Expand Down
4 changes: 2 additions & 2 deletions modules/example/src/main/scala/example/api/Application.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package example.api

import zio.http._
import zio.http.model.Method
import zio.json.ast.Json
import zio.json.ast.Json._

object Application {

final val Routes: Http[Any, Nothing, Any, Response] = Http.collect { case Method.GET -> !! / "health" =>
Response.json(Json.Obj("name" -> Json.Str("zio-elasticsearch-example"), "status" -> Json.Str("up")).toJson)
Response.json(Obj("name" -> Str("zio-elasticsearch-example"), "status" -> Str("up")).toJson)
}

}
6 changes: 3 additions & 3 deletions modules/example/src/main/scala/example/api/Repositories.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package example.api

import example.{RepositoriesElasticsearch, Repository}
import example.{GitHubRepo, RepositoriesElasticsearch}
import zio.ZIO
import zio.elasticsearch.{DocumentId, ElasticExecutor}
import zio.http._
Expand Down Expand Up @@ -31,7 +31,7 @@ object Repositories {

case req @ Method.POST -> BasePath =>
req.body.asString
.map(JsonCodec.JsonDecoder.decode[Repository](Repository.schema, _))
.map(JsonCodec.JsonDecoder.decode[GitHubRepo](GitHubRepo.schema, _))
.flatMap {
case Left(e) =>
ZIO.succeed(Response.json(ErrorResponse.fromReasons(e.message).toJson).setStatus(BadRequest))
Expand All @@ -47,7 +47,7 @@ object Repositories {

case req @ Method.PUT -> BasePath / id =>
req.body.asString
.map(JsonCodec.JsonDecoder.decode[Repository](Repository.schema, _))
.map(JsonCodec.JsonDecoder.decode[GitHubRepo](GitHubRepo.schema, _))
.flatMap {
case Left(e) =>
ZIO.succeed(Response.json(ErrorResponse.fromReasons(e.message).toJson).setStatus(BadRequest))
Expand Down

0 comments on commit 17363fa

Please sign in to comment.