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

(api): Support delete by ID #19

Merged
merged 2 commits into from
Dec 2, 2022
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
@@ -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