Browse Source

server: Add endpoint GET /balances to get richest balances

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
b43bf44737
  1. 25
      server/app/com/xsn/explorer/data/async/BalanceFutureDataHandler.scala
  2. 27
      server/app/com/xsn/explorer/services/BalanceService.scala
  3. 18
      server/app/controllers/BalancesController.scala
  4. 2
      server/conf/routes

25
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)
}
}

27
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
}
}

18
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)
}
}

2
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)

Loading…
Cancel
Save