Skip to content

Commit

Permalink
Add Game to search & count endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed May 10, 2024
1 parent 8cf97f0 commit 16cd755
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 8 deletions.
56 changes: 55 additions & 1 deletion modules/api/src/main/smithy/search.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace lila.search.spec

use alloy#simpleRestJson
use smithy4s.meta#adt
use smithy.api#default

@simpleRestJson
service SearchService {
Expand Down Expand Up @@ -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
}
38 changes: 31 additions & 7 deletions modules/app/src/main/scala/service.search.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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]:

Expand All @@ -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")

0 comments on commit 16cd755

Please sign in to comment.