Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor core #82

Merged
merged 9 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import zio.prelude.Newtype.unsafeWrap
final case class RepositoriesElasticsearch(executor: ElasticExecutor) {

def findAll(): Task[List[GitHubRepo]] =
executor.execute(ElasticRequest.search[GitHubRepo](Index, matchAll()))
executor.execute(ElasticRequest.search[GitHubRepo](Index, matchAll))

def findById(organization: String, id: String): Task[Option[GitHubRepo]] =
for {
Expand Down
45 changes: 26 additions & 19 deletions modules/example/src/main/scala/example/api/Repositories.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@ package example.api
import example.{GitHubRepo, RepositoriesElasticsearch}
import zio.ZIO
import zio.elasticsearch.ElasticQuery.boolQuery
import zio.elasticsearch.{CreationOutcome, DeletionOutcome, ElasticQuery}
import zio.elasticsearch._
import zio.http._
import zio.http.model.Method
import zio.http.model.Status._
import zio.http.model.Status.{
BadRequest => HttpBadRequest,
Created => HttpCreated,
NoContent => HttpNoContent,
NotFound => HttpNotFound
}
import zio.json.EncoderOps
import zio.schema.codec.JsonCodec

Expand All @@ -45,7 +50,7 @@ object Repositories {
case Some(r) =>
Response.json(r.toJson)
case None =>
Response.json(ErrorResponse.fromReasons(s"Repository $id does not exist.").toJson).setStatus(NotFound)
Response.json(ErrorResponse.fromReasons(s"Repository $id does not exist.").toJson).setStatus(HttpNotFound)
}
.orDie

Expand All @@ -54,13 +59,13 @@ object Repositories {
.map(JsonCodec.JsonDecoder.decode[GitHubRepo](GitHubRepo.schema, _))
.flatMap {
case Left(e) =>
ZIO.succeed(Response.json(ErrorResponse.fromReasons(e.message).toJson).setStatus(BadRequest))
ZIO.succeed(Response.json(ErrorResponse.fromReasons(e.message).toJson).setStatus(HttpBadRequest))
case Right(repo) =>
RepositoriesElasticsearch.create(repo).map {
case CreationOutcome.Created =>
Response.json(repo.toJson).setStatus(Created)
case CreationOutcome.AlreadyExists =>
Response.json("A repository with a given ID already exists.").setStatus(BadRequest)
case Created =>
Response.json(repo.toJson).setStatus(HttpCreated)
case AlreadyExists =>
Response.json("A repository with a given ID already exists.").setStatus(HttpBadRequest)
}
}
.orDie
Expand All @@ -70,7 +75,7 @@ object Repositories {
.map(JsonCodec.JsonDecoder.decode[Criteria](Criteria.schema, _))
.flatMap {
case Left(e) =>
ZIO.succeed(Response.json(ErrorResponse.fromReasons(e.message).toJson).setStatus(BadRequest))
ZIO.succeed(Response.json(ErrorResponse.fromReasons(e.message).toJson).setStatus(HttpBadRequest))
case Right(queryBody) =>
RepositoriesElasticsearch
.search(createElasticQuery(queryBody))
Expand All @@ -83,20 +88,22 @@ object Repositories {
.map(JsonCodec.JsonDecoder.decode[GitHubRepo](GitHubRepo.schema, _))
.flatMap {
case Left(e) =>
ZIO.succeed(Response.json(ErrorResponse.fromReasons(e.message).toJson).setStatus(BadRequest))
ZIO.succeed(Response.json(ErrorResponse.fromReasons(e.message).toJson).setStatus(HttpBadRequest))
case Right(repo) if repo.id == id =>
ZIO.succeed(
Response
.json(
ErrorResponse.fromReasons("The ID provided in the path does not match the ID from the body.").toJson
)
.setStatus(BadRequest)
.setStatus(HttpBadRequest)
)
case Right(repo) =>
(RepositoriesElasticsearch
.upsert(id, repo.copy(id = id)) *> RepositoriesElasticsearch.findById(repo.organization, id)).map {
case Some(updated) => Response.json(updated.toJson)
case None => Response.json(ErrorResponse.fromReasons("Operation failed.").toJson).setStatus(BadRequest)
case Some(updated) =>
Response.json(updated.toJson)
case None =>
Response.json(ErrorResponse.fromReasons("Operation failed.").toJson).setStatus(HttpBadRequest)
}
}
.orDie
Expand All @@ -105,10 +112,10 @@ object Repositories {
RepositoriesElasticsearch
.remove(organization, id)
.map {
case DeletionOutcome.Deleted =>
Response.status(NoContent)
case DeletionOutcome.NotFound =>
Response.json(ErrorResponse.fromReasons(s"Repository $id does not exist.").toJson).setStatus(NotFound)
case Deleted =>
Response.status(HttpNoContent)
case NotFound =>
Response.json(ErrorResponse.fromReasons(s"Repository $id does not exist.").toJson).setStatus(HttpNotFound)
}
.orDie
}
Expand All @@ -117,8 +124,8 @@ object Repositories {
query match {
case CompoundCriteria(operator, filters) =>
operator match {
case And => boolQuery().must(filters.map(createElasticQuery): _*)
case Or => boolQuery().should(filters.map(createElasticQuery): _*)
case And => boolQuery.must(filters.map(createElasticQuery): _*)
case Or => boolQuery.should(filters.map(createElasticQuery): _*)
}
case DateCriteria(field, operator, value) =>
operator match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@

package zio.elasticsearch

import zio.elasticsearch.CreationOutcome.{AlreadyExists, Created}
import zio.elasticsearch.DeletionOutcome.{Deleted, NotFound}
import zio.elasticsearch.ElasticQuery._
import zio.test.Assertion._
import zio.test.TestAspect._
import zio.test._

object HttpExecutorSpec extends IntegrationSpec {

override def spec: Spec[TestEnvironment, Any] = {
def spec: Spec[TestEnvironment, Any] = {
suite("Executor")(
suite("HTTP Executor")(
suite("creating document")(
Expand Down Expand Up @@ -158,7 +156,7 @@ object HttpExecutorSpec extends IntegrationSpec {
checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer) {
(firstDocumentId, firstCustomer, secondDocumentId, secondCustomer) =>
for {
_ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute
_ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll).execute
_ <-
ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute
_ <-
Expand All @@ -179,7 +177,7 @@ object HttpExecutorSpec extends IntegrationSpec {
(employeeDocumentId, customerDocumentId, employee, customer) =>
val result =
for {
_ <- ElasticRequest.deleteByQuery(secondSearchIndex, matchAll()).execute
_ <- ElasticRequest.deleteByQuery(secondSearchIndex, matchAll).execute
_ <-
ElasticRequest.upsert[CustomerDocument](secondSearchIndex, customerDocumentId, customer).execute
_ <- ElasticRequest
Expand All @@ -206,7 +204,7 @@ object HttpExecutorSpec extends IntegrationSpec {
checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer) {
(firstDocumentId, firstCustomer, secondDocumentId, secondCustomer) =>
for {
_ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute
_ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll).execute
_ <-
ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute
_ <-
Expand All @@ -226,7 +224,7 @@ object HttpExecutorSpec extends IntegrationSpec {
checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer) {
(firstDocumentId, firstCustomer, secondDocumentId, secondCustomer) =>
for {
_ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute
_ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll).execute
_ <-
ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute
_ <-
Expand All @@ -246,7 +244,7 @@ object HttpExecutorSpec extends IntegrationSpec {
checkOnce(genDocumentId, genCustomer, genDocumentId, genCustomer) {
(firstDocumentId, firstCustomer, secondDocumentId, secondCustomer) =>
for {
_ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll()).execute
_ <- ElasticRequest.deleteByQuery(firstSearchIndex, matchAll).execute
_ <-
ElasticRequest.upsert[CustomerDocument](firstSearchIndex, firstDocumentId, firstCustomer).execute
_ <-
Expand Down Expand Up @@ -295,7 +293,7 @@ object HttpExecutorSpec extends IntegrationSpec {
.execute
deleteQuery = range("balance").gte(300)
_ <- ElasticRequest.deleteByQuery(deleteByQueryIndex, deleteQuery).refreshTrue.execute
res <- ElasticRequest.search[CustomerDocument](deleteByQueryIndex, matchAll()).execute
res <- ElasticRequest.search[CustomerDocument](deleteByQueryIndex, matchAll).execute
} yield assert(res)(hasSameElements(List(firstCustomer.copy(balance = 150))))
}
} @@ around(
Expand All @@ -304,7 +302,7 @@ object HttpExecutorSpec extends IntegrationSpec {
),
test("returns NotFound when provided index is missing") {
checkOnce(genIndexName) { missingIndex =>
assertZIO(ElasticRequest.deleteByQuery(missingIndex, matchAll()).execute)(equalTo(NotFound))
assertZIO(ElasticRequest.deleteByQuery(missingIndex, matchAll).execute)(equalTo(NotFound))
}
}
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ trait IntegrationSpec extends ZIOSpecDefault {

val prepareElasticsearchIndexForTests: TestAspect[Nothing, Any, Throwable, Any] = beforeAll((for {
_ <- ElasticRequest.createIndex(index, None).execute
_ <- ElasticRequest.deleteByQuery(index, matchAll()).refreshTrue.execute
_ <- ElasticRequest.deleteByQuery(index, matchAll).refreshTrue.execute
} yield ()).provide(elasticsearchLayer))

def genIndexName: Gen[Any, IndexName] =
Expand Down
Loading