Skip to content

Commit

Permalink
(api): Support delete by ID (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvelimir authored Dec 2, 2022
1 parent 06019d8 commit 1366add
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package zio.elasticsearch

import zio.json.{DeriveJsonDecoder, JsonDecoder}

private[elasticsearch] final case class ElasticDeleteResponse(
result: String
)

private[elasticsearch] object ElasticDeleteResponse {
implicit val decoder: JsonDecoder[ElasticDeleteResponse] = DeriveJsonDecoder.gen[ElasticDeleteResponse]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ sealed trait ElasticRequest[+A] { self =>
case Map(request, mapper) => Map(request.routing(value), mapper)
case r: Create => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case r: CreateOrUpdate => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case r: DeleteById => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case r: Exists => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case r: GetById => r.copy(routing = Some(Routing(value))).asInstanceOf[ElasticRequest[A]]
case _ => self
Expand All @@ -31,6 +32,9 @@ object ElasticRequest {
def create[A: Schema](index: IndexName, doc: A): ElasticRequest[Option[DocumentId]] =
Create(index, None, Document.from(doc))

def deleteById(index: IndexName, id: DocumentId): ElasticRequest[Either[DocumentNotFound.type, Unit]] =
DeleteById(index, id).map(_.toRight(DocumentNotFound))

def exists(index: IndexName, id: DocumentId): ElasticRequest[Boolean] =
Exists(index, id)

Expand Down Expand Up @@ -68,6 +72,12 @@ object ElasticRequest {
routing: Option[Routing] = None
) extends ElasticRequest[Unit]

private[elasticsearch] final case class DeleteById(
index: IndexName,
id: DocumentId,
routing: Option[Routing] = None
) extends ElasticRequest[Option[Unit]]

private[elasticsearch] final case class DeleteIndex(name: IndexName) extends ElasticRequest[Unit]

private[elasticsearch] final case class Exists(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
case r: Create => executeCreate(r)
case r: CreateIndex => executeCreateIndex(r)
case r: CreateOrUpdate => executeCreateOrUpdate(r)
case r: DeleteById => executeDeleteById(r)
case r: DeleteIndex => executeDeleteIndex(r)
case r: Exists => executeExists(r)
case r: GetById => executeGetById(r)
Expand Down Expand Up @@ -75,6 +76,16 @@ private[elasticsearch] final class HttpElasticExecutor private (config: ElasticC
private def executeDeleteIndex(r: DeleteIndex): Task[Unit] =
request.delete(uri"$basePath/${r.name}").send(client).unit

private def executeDeleteById(deleteById: DeleteById): Task[Option[Unit]] = {
val uri =
uri"$basePath/${deleteById.index}/$Doc/${deleteById.id}".withParam("routing", deleteById.routing.map(_.value))
request
.delete(uri)
.response(asJson[ElasticDeleteResponse])
.send(client)
.map(_.body.toOption)
.map(_.filter(_.result == "deleted").map(_ => ()))
}
}

private[elasticsearch] object HttpElasticExecutor {
Expand Down

0 comments on commit 1366add

Please sign in to comment.