From 16cd7558156030ecb64da6278250c73cf9e4684d Mon Sep 17 00:00:00 2001 From: Thanh Le Date: Thu, 9 May 2024 22:31:09 +0700 Subject: [PATCH] Add Game to search & count endpoints --- modules/api/src/main/smithy/search.smithy | 56 ++++++++++++++++++- .../app/src/main/scala/service.search.scala | 38 ++++++++++--- 2 files changed, 86 insertions(+), 8 deletions(-) diff --git a/modules/api/src/main/smithy/search.smithy b/modules/api/src/main/smithy/search.smithy index e43ca54a..1ad0c153 100644 --- a/modules/api/src/main/smithy/search.smithy +++ b/modules/api/src/main/smithy/search.smithy @@ -4,6 +4,7 @@ namespace lila.search.spec use alloy#simpleRestJson use smithy4s.meta#adt +use smithy.api#default @simpleRestJson service SearchService { @@ -64,9 +65,62 @@ structure Study { userId: String } +structure Game { + user1: String + user2: String + winner: String + loser: String + winnerColor: Integer + @default + perf: Perfs + source: Integer + status: Integer + turns: IntRange + averageRating: IntRange + hasAi: Boolean + aiLevel: IntRange + rated: Boolean + date: DateRange + duration: IntRange + clock: Clocking + sorting: Sorting + analysed: Boolean + whiteUser: String + blackUser: String +} + +structure IntRange { + a: Integer + b: Integer +} + +structure DateRange { + a: Timestamp + b: Timestamp +} + +structure Clocking { + initMin: Integer + initMax: Integer + incMin: Integer + incMax: Integer +} + +structure Sorting { + @required + f: String + @required + order: String +} + +list Perfs { + member: Integer +} + @adt union Query { forum: Forum - team: Team + game: Game study: Study + team: Team } diff --git a/modules/app/src/main/scala/service.search.scala b/modules/app/src/main/scala/service.search.scala index ab48c0a9..fbfff4a2 100644 --- a/modules/app/src/main/scala/service.search.scala +++ b/modules/app/src/main/scala/service.search.scala @@ -7,6 +7,8 @@ import org.typelevel.log4cats.Logger import org.typelevel.log4cats.syntax.* import forum.ForumQuery.* import io.github.arainko.ducktape.* +import org.joda.time.DateTime +import smithy4s.Timestamp class SearchServiceImpl(esClient: ESClient[IO])(using Logger[IO]) extends SearchService[IO]: @@ -30,21 +32,43 @@ class SearchServiceImpl(esClient: ESClient[IO])(using Logger[IO]) extends Search object SearchServiceImpl: + given Transformer.Derived[Timestamp, DateTime] = + Transformer.Derived.FromFunction(x => DateTime(x.epochSecond)) + + given intRange: Transformer.Derived[Option[IntRange], Range[Int]] = + Transformer.Derived.FromFunction(_.fold(Range.none)(r => Range(r.a, r.b))) + + given dateRange: Transformer.Derived[Option[DateRange], Range[DateTime]] = + Transformer.Derived.FromFunction( + _.fold(Range.none)(r => Range(r.a.map(_.to[DateTime]), r.b.map(_.to[DateTime]))) + ) + + given Transformer.Derived[Option[Clocking], game.Clocking] = + Transformer.Derived.FromFunction(_.fold(game.Clocking())(_.to[game.Clocking])) + + given Transformer.Derived[Option[Sorting], game.Sorting] = + Transformer.Derived.FromFunction(_.fold(game.Sorting.default)(_.to[game.Sorting])) + + extension (game: Query.Game) def transform: Game = game.to[Game] + given Queryable[Query] with def searchDef(query: Query)(from: From, size: Size) = query match - case q: Query.Forum => forum.ForumQuery.query.searchDef(q.to[lila.search.Forum])(from, size) - case q: Query.Team => team.TeamQuery.query.searchDef(q.to[lila.search.Team])(from, size) - case q: Query.Study => study.StudyQuery.query.searchDef(q.to[lila.search.Study])(from, size) + case q: Query.Forum => forum.ForumQuery.query.searchDef(q.to[Forum])(from, size) + case q: Query.Game => game.GameQuery.query.searchDef(q.transform)(from, size) + case q: Query.Study => study.StudyQuery.query.searchDef(q.to[Study])(from, size) + case q: Query.Team => team.TeamQuery.query.searchDef(q.to[Team])(from, size) def countDef(query: Query) = query match - case q: Query.Forum => forum.ForumQuery.query.countDef(q.to[lila.search.Forum]) - case q: Query.Team => team.TeamQuery.query.countDef(q.to[lila.search.Team]) - case q: Query.Study => study.StudyQuery.query.countDef(q.to[lila.search.Study]) + case q: Query.Forum => forum.ForumQuery.query.countDef(q.to[Forum]) + case q: Query.Game => game.GameQuery.query.countDef(q.transform) + case q: Query.Study => study.StudyQuery.query.countDef(q.to[Study]) + case q: Query.Team => team.TeamQuery.query.countDef(q.to[Team]) extension (query: Query) def index = query match case q: Query.Forum => Index("forum") - case q: Query.Team => Index("team") + case q: Query.Game => Index("game") case q: Query.Study => Index("study") + case q: Query.Team => Index("team")