Skip to content

Commit

Permalink
Add total_records for federations api
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Feb 22, 2025
1 parent 0a5b60b commit 2959718
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
4 changes: 4 additions & 0 deletions modules/api/src/main/smithy/federations.smithy
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ operation GetFederationsSummary {
output:= {
@required
items: FederationsSummary
@required
totalResults: Long
nextPage: PageNumber
}

Expand Down Expand Up @@ -69,6 +71,8 @@ operation GetFederationPlayersById {
output := {
@required
items: Players
@required
totalResults: Long
nextPage: PageNumber
}

Expand Down
24 changes: 9 additions & 15 deletions modules/backend/src/main/scala/service.federation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,12 @@ class FederationServiceImpl(db: Db)(using Logger[IO]) extends FederationService[
hasWomenTitle,
hasOtherTitle
)
db.allPlayers(sorting, paging, filter)
(db.allPlayers(sorting, paging, filter).map(_.map(_.transform)), db.countPlayers(filter))
.parMapN: (xs, total) =>
GetFederationPlayersByIdOutput(xs, total, Option.when(xs.size == pageSize)(page.succ))
.handleErrorWith: e =>
error"Error in getPlayers with $filter, $e" *>
IO.raiseError(InternalServerError("Internal server error"))
.map(_.map(_.transform))
.map: xs =>
GetFederationPlayersByIdOutput(
xs,
Option.when(xs.size == pageSize)(page.succ)
)

override def getFederationSummaryById(id: FederationId): IO[GetFederationSummaryByIdOutput] =
db.federationSummaryById(id)
Expand All @@ -89,16 +85,14 @@ class FederationServiceImpl(db: Db)(using Logger[IO]) extends FederationService[
page: PageNumber,
pageSize: PageSize
): IO[GetFederationsSummaryOutput] =
db.allFederationsSummary(Pagination(page, pageSize))
.handleErrorWith: e =>
(
db.allFederationsSummary(Pagination(page, pageSize)).map(_.map(_.transform)),
db.countFederationsSummary
).parMapN: (xs, total) =>
GetFederationsSummaryOutput(xs, total, Option.when(xs.size == pageSize)(page.succ))
.handleErrorWith: e =>
error"Error in getFederationsSummary: $e" *>
IO.raiseError(InternalServerError("Internal server error"))
.map(_.map(_.transform))
.map: xs =>
GetFederationsSummaryOutput(
xs,
Option.when(xs.size == pageSize)(page.succ)
)

object FederationTransformers:
extension (p: FederationSummary)
Expand Down
14 changes: 14 additions & 0 deletions modules/db/src/main/scala/Db.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ trait Db:
def playersByIds(ids: Set[PlayerId]): IO[List[PlayerInfo]]
def playersByFederationId(id: FederationId): IO[List[PlayerInfo]]
def allFederationsSummary(paging: Pagination): IO[List[FederationSummary]]
def countFederationsSummary: IO[Long]
def countFederations: IO[Long]
def federationSummaryById(id: FederationId): IO[Option[FederationSummary]]

object Db:
Expand Down Expand Up @@ -74,6 +76,12 @@ object Db:
val q = f.fragment.query(Codecs.federationSummary)
postgres.use(_.execute(q)(f.argument))

def countFederationsSummary: IO[Long] =
postgres.use(_.unique(Sql.countFederationsSummary))

def countFederations: IO[Long] =
postgres.use(_.unique(Sql.countFederations))

def federationSummaryById(id: FederationId): IO[Option[FederationSummary]] =
postgres.use(_.option(Sql.federationSummaryById)(id))

Expand Down Expand Up @@ -192,6 +200,12 @@ private object Sql:
""".apply(Void)
select |+| filterFragment(filter).fold(void)(where |+| _)

val countFederations: Query[Void, Long] =
sql"SELECT count(*) FROM federations".query(codec.all.int8)

val countFederationsSummary: Query[Void, Long] =
sql"SELECT count(*) FROM federations".query(codec.all.int8)

def playersByIds(n: Int): Fragment[List[Int]] =
val ids = int4.values.list(n)
sql"$allPlayersFragment WHERE p.id IN ($ids)"
Expand Down

0 comments on commit 2959718

Please sign in to comment.