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 23, 2022
1 parent 6e6489d commit 3ecefd6
Showing 1 changed file with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
package example

import zio._
import zio.elasticsearch.ElasticError.DocumentRetrievingError.DocumentNotFound
import zio.elasticsearch.{DocumentId, ElasticExecutor, ElasticRequest, Routing}
import zio.{RIO, Task, URLayer, ZIO, ZLayer}

final case class RepositoriesElasticsearch(executor: ElasticExecutor) {

def findById(organization: String, id: String): Task[Option[GitHubRepo]] =
for {
routing <- Routing.make(organization).toZIO.mapError(e => new IllegalArgumentException(e))
routing <- routingOf(organization)
req = ElasticRequest.getById[GitHubRepo](Index, DocumentId(id)).routing(routing)
res <- executor.execute(req)
} yield res.toOption

def create(repository: GitHubRepo): Task[Option[DocumentId]] =
for {
routing <- Routing.make(repository.organization).toZIO.mapError(e => new IllegalArgumentException(e))
routing <- routingOf(repository.organization)
req = ElasticRequest.create(Index, repository).routing(routing)
res <- executor.execute(req)
} yield res

def upsert(id: String, repository: GitHubRepo): Task[Unit] =
for {
routing <- Routing.make(repository.organization).toZIO.mapError(e => new IllegalArgumentException(e))
routing <- routingOf(repository.organization)
req = ElasticRequest.upsert(Index, DocumentId(id), repository).routing(routing)
_ <- executor.execute(req)
} yield ()

def remove(organization: String, id: String): Task[Either[DocumentNotFound.type, Unit]] =
for {
routing <- Routing.make(organization).toZIO.mapError(e => new IllegalArgumentException(e))
routing <- routingOf(organization)
req = ElasticRequest.deleteById(Index, DocumentId(id)).routing(routing)
res <- executor.execute(req)
} yield res

private def routingOf(value: String): IO[IllegalArgumentException, Routing.Type] =
Routing.make(value).toZIO.mapError(e => new IllegalArgumentException(e))

}

object RepositoriesElasticsearch {
Expand Down

0 comments on commit 3ecefd6

Please sign in to comment.