diff --git a/server/app/com/xsn/explorer/data/async/BalanceFutureDataHandler.scala b/server/app/com/xsn/explorer/data/async/BalanceFutureDataHandler.scala new file mode 100644 index 0000000..3b10bd8 --- /dev/null +++ b/server/app/com/xsn/explorer/data/async/BalanceFutureDataHandler.scala @@ -0,0 +1,25 @@ +package com.xsn.explorer.data.async + +import javax.inject.Inject + +import com.alexitc.playsonify.core.FutureApplicationResult +import com.xsn.explorer.data.{BalanceBlockingDataHandler, BalanceDataHandler} +import com.xsn.explorer.executors.DatabaseExecutionContext +import com.xsn.explorer.models.Balance +import com.xsn.explorer.models.base.{PaginatedQuery, PaginatedResult} + +import scala.concurrent.Future + +class BalanceFutureDataHandler @Inject() ( + blockingDataHandler: BalanceBlockingDataHandler)( + implicit ec: DatabaseExecutionContext) + extends BalanceDataHandler[FutureApplicationResult] { + + override def upsert(balance: Balance): FutureApplicationResult[Balance] = Future { + blockingDataHandler.upsert(balance) + } + + override def getRichest(query: PaginatedQuery): FutureApplicationResult[PaginatedResult[Balance]] = Future { + blockingDataHandler.getRichest(query) + } +} diff --git a/server/app/com/xsn/explorer/services/BalanceService.scala b/server/app/com/xsn/explorer/services/BalanceService.scala new file mode 100644 index 0000000..5773ea5 --- /dev/null +++ b/server/app/com/xsn/explorer/services/BalanceService.scala @@ -0,0 +1,27 @@ +package com.xsn.explorer.services + +import javax.inject.Inject + +import com.alexitc.playsonify.core.FutureApplicationResult +import com.alexitc.playsonify.core.FutureOr.Implicits.{FutureOps, OrOps} +import com.xsn.explorer.data.async.BalanceFutureDataHandler +import com.xsn.explorer.models.Balance +import com.xsn.explorer.models.base.{PaginatedQuery, PaginatedResult} +import com.xsn.explorer.services.validators.PaginatedQueryValidator + +import scala.concurrent.ExecutionContext + +class BalanceService @Inject() ( + paginatedQueryValidator: PaginatedQueryValidator, + balanceFutureDataHandler: BalanceFutureDataHandler)( + implicit ec: ExecutionContext) { + + def getRichest(query: PaginatedQuery): FutureApplicationResult[PaginatedResult[Balance]] = { + val result = for { + validatedQuery <- paginatedQueryValidator.validate(query).toFutureOr + balances <- balanceFutureDataHandler.getRichest(validatedQuery).toFutureOr + } yield balances + + result.toFuture + } +} diff --git a/server/app/controllers/BalancesController.scala b/server/app/controllers/BalancesController.scala new file mode 100644 index 0000000..069e971 --- /dev/null +++ b/server/app/controllers/BalancesController.scala @@ -0,0 +1,18 @@ +package controllers + +import javax.inject.Inject + +import com.xsn.explorer.models.base.{Limit, Offset, PaginatedQuery} +import com.xsn.explorer.services.BalanceService +import controllers.common.{MyJsonController, MyJsonControllerComponents} + +class BalancesController @Inject() ( + balanceService: BalanceService, + cc: MyJsonControllerComponents) + extends MyJsonController(cc) { + + def getRichest(offset: Int, limit: Int) = publicNoInput { _ => + val query = PaginatedQuery(Offset(offset), Limit(limit)) + balanceService.getRichest(query) + } +} \ No newline at end of file diff --git a/server/conf/routes b/server/conf/routes index 31bd55f..15523b0 100644 --- a/server/conf/routes +++ b/server/conf/routes @@ -13,3 +13,5 @@ GET /blocks controllers.BlocksController.getLatestBlocks() GET /blocks/:blockhash controllers.BlocksController.getDetails(blockhash: String) GET /stats controllers.StatisticsController.getStatus() + +GET /balances controllers.BalancesController.getRichest(offset: Int ?= 0, limit: Int ?= 10)