diff --git a/server/app/com/xsn/explorer/services/BalanceService.scala b/server/app/com/xsn/explorer/services/BalanceService.scala index bdb5446..2592171 100644 --- a/server/app/com/xsn/explorer/services/BalanceService.scala +++ b/server/app/com/xsn/explorer/services/BalanceService.scala @@ -1,14 +1,16 @@ package com.xsn.explorer.services import com.alexitc.playsonify.core.FutureOr.Implicits.{FutureOps, OrOps} -import com.alexitc.playsonify.core.FuturePaginatedResult +import com.alexitc.playsonify.core.{FutureApplicationResult, FuturePaginatedResult} import com.alexitc.playsonify.models.ordering.OrderingQuery -import com.alexitc.playsonify.models.pagination.PaginatedQuery +import com.alexitc.playsonify.models.pagination.{Limit, Offset, PaginatedQuery} import com.alexitc.playsonify.validators.PaginatedQueryValidator import com.xsn.explorer.data.async.BalanceFutureDataHandler -import com.xsn.explorer.models.Balance +import com.xsn.explorer.errors.AddressFormatError +import com.xsn.explorer.models.{Address, Balance, WrappedResult} import com.xsn.explorer.parsers.BalanceOrderingParser import javax.inject.Inject +import org.scalactic.{Good, One, Or} import scala.concurrent.ExecutionContext @@ -27,4 +29,22 @@ class BalanceService @Inject() ( result.toFuture } + + def getHighest(limit: Limit, lastSeenAddressString: Option[String]): FutureApplicationResult[WrappedResult[List[Balance]]] = { + val result = for { + _ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), 100).toFutureOr + + lastSeenAddress <- { + lastSeenAddressString + .map(Address.from) + .map { txid => Or.from(txid, One(AddressFormatError)).map(Option.apply) } + .getOrElse(Good(Option.empty)) + .toFutureOr + } + + data <- balanceFutureDataHandler.getHighestBalances(limit, lastSeenAddress).toFutureOr + } yield WrappedResult(data) + + result.toFuture + } } diff --git a/server/app/controllers/BalancesController.scala b/server/app/controllers/BalancesController.scala index 5fed0b0..4fb056b 100644 --- a/server/app/controllers/BalancesController.scala +++ b/server/app/controllers/BalancesController.scala @@ -19,4 +19,8 @@ class BalancesController @Inject() ( balanceService.get(paginatedQuery, orderingQuery) } + + def getHighest(limit: Int, lastSeenAddress: Option[String]) = public { _ => + balanceService.getHighest(Limit(limit), lastSeenAddress) + } } \ No newline at end of file diff --git a/server/conf/routes b/server/conf/routes index 6e04729..968fdd5 100644 --- a/server/conf/routes +++ b/server/conf/routes @@ -23,6 +23,7 @@ GET /blocks/:blockhash/transactions controllers.BlocksController.getTransac GET /stats controllers.StatisticsController.getStatus() GET /balances controllers.BalancesController.get(offset: Int ?= 0, limit: Int ?= 10, orderBy: String ?= "") +GET /v2/balances controllers.BalancesController.getHighest(limit: Int ?= 10, lastSeenAddress: Option[String]) GET /masternodes controllers.MasternodesController.get(offset: Int ?= 0, limit: Int ?= 10, orderBy: String ?= "") GET /masternodes/:ip controllers.MasternodesController.getBy(ip: String)