Skip to content

Commit

Permalink
Implement deleteById and deleteByIds endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed May 10, 2024
1 parent 16cd755 commit 83cfbf8
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 8 deletions.
41 changes: 39 additions & 2 deletions modules/api/src/main/smithy/search.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use smithy.api#default

@simpleRestJson
service SearchService {
version: "3.0.0",
operations: [Search, Count]
version: "3.0.0"
operations: [Search, Count, DeleteById, DeleteByIds]
}

@readonly
Expand All @@ -28,6 +28,18 @@ operation Count {
errors: [InternalServerError]
}

@http(method: "POST", uri: "/delete/id/{index}/{id}", code: 200)
operation DeleteById {
input: DeleteByIdInput
errors: [InternalServerError]
}

@http(method: "POST", uri: "/delete/ids/{index}", code: 200)
operation DeleteByIds {
input: DeleteByIdsInput
errors: [InternalServerError]
}

structure SearchInput {

@required
Expand All @@ -47,6 +59,24 @@ structure CountInput {
query: Query
}

structure DeleteByIdInput {
@required
@httpLabel
index: Index
@required
@httpLabel
id: String
}

structure DeleteByIdsInput {
@required
@httpLabel
index: Index

@required
ids: Ids
}

structure Forum {
@required
text: String
Expand Down Expand Up @@ -124,3 +154,10 @@ union Query {
study: Study
team: Team
}

enum Index {
Forum = "forum"
Game = "game"
Study = "study"
Team = "team"
}
26 changes: 21 additions & 5 deletions modules/app/src/main/scala/service.search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@ class SearchServiceImpl(esClient: ESClient[IO])(using Logger[IO]) extends Search

import SearchServiceImpl.{ given, * }

override def deleteById(index: Index, id: String): IO[Unit] =
esClient
.deleteOne(index.transform, Id(id))
.handleErrorWith: e =>
error"Error in deleteById: index=$index, id=$id" *>
IO.raiseError(InternalServerError("Internal server error"))

override def deleteByIds(index: Index, ids: List[String]): IO[Unit] =
esClient
.deleteMany(index.transform, ids.map(Id))
.handleErrorWith: e =>
error"Error in deleteByIds: index=$index, ids=$ids" *>
IO.raiseError(InternalServerError("Internal server error"))

override def count(query: Query): IO[CountResponse] =
esClient
.count(Index("forum"), query)
.count(query.index, query)
.map(_.to[CountResponse])
.handleErrorWith: e =>
error"Error in countForum: query=$query" *>
Expand Down Expand Up @@ -51,6 +65,8 @@ object SearchServiceImpl:

extension (game: Query.Game) def transform: Game = game.to[Game]

extension (index: Index) def transform: lila.search.Index = lila.search.Index(index.value)

given Queryable[Query] with
def searchDef(query: Query)(from: From, size: Size) =
query match
Expand All @@ -68,7 +84,7 @@ object SearchServiceImpl:

extension (query: Query)
def index = query match
case q: Query.Forum => Index("forum")
case q: Query.Game => Index("game")
case q: Query.Study => Index("study")
case q: Query.Team => Index("team")
case q: Query.Forum => lila.search.Index("forum")
case q: Query.Game => lila.search.Index("game")
case q: Query.Study => lila.search.Index("study")
case q: Query.Team => lila.search.Index("team")
12 changes: 12 additions & 0 deletions modules/client/src/main/scala/PlayClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ class PlayClient(client: StandaloneWSClient, baseUrl: String)(using ExecutionCon

import implicits.given

override def deleteById(index: Index, id: String): Future[Unit] =
client
.url(s"$baseUrl/delete/${index.name}/$id")
.execute("POST")
.map(_ => ())

override def deleteByIds(index: Index, ids: List[String]): Future[Unit] =
client
.url(s"$baseUrl/delete/${index.name}")
.post(Ids(ids))
.map(_ => ())

override def count(query: Query): Future[CountResponse] =
request(s"$baseUrl/count", SearchInput(query))

Expand Down
8 changes: 7 additions & 1 deletion modules/e2e/src/test/scala/CompatSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import com.comcast.ip4s.*
import akka.actor.ActorSystem
import play.api.libs.ws.*
import play.api.libs.ws.ahc.*
import lila.search.spec.Query
import lila.search.spec.{ Query, Index as SpecIndex }
import scala.concurrent.ExecutionContext.Implicits.*

object CompatSuite extends weaver.IOSuite:
Expand All @@ -37,6 +37,12 @@ object CompatSuite extends weaver.IOSuite:
val query = Query.Team("foo")
IO.fromFuture(IO(client.count(query))).map(expect.same(_, lila.search.spec.CountResponse(0)))

test("deleteById endpoint"): client =>
IO.fromFuture(IO(client.deleteById(SpecIndex.Game, "iddddd"))).map(expect.same(_, ()))

test("deleteByIds endpoint"): client =>
IO.fromFuture(IO(client.deleteByIds(SpecIndex.Game, List("a", "b", "c")))).map(expect.same(_, ()))

def testAppConfig = AppConfig(
server = HttpServerConfig(ip"0.0.0.0", port"9999", shutdownTimeout = 1),
elastic = ElasticConfig("http://0.0.0.0:9200")
Expand Down

0 comments on commit 83cfbf8

Please sign in to comment.