Skip to content

Commit

Permalink
server: Simplify the validators
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Apr 7, 2019
1 parent 277abf9 commit 21fcb82
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 31 deletions.
9 changes: 2 additions & 7 deletions server/app/com/xsn/explorer/services/BalanceService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import com.xsn.explorer.data.async.BalanceFutureDataHandler
import com.xsn.explorer.models.WrappedResult
import com.xsn.explorer.models.persisted.Balance
import com.xsn.explorer.parsers.BalanceOrderingParser
import com.xsn.explorer.services.validators.AddressValidator
import com.xsn.explorer.services.validators._
import javax.inject.Inject
import org.scalactic.Good

import scala.concurrent.ExecutionContext

Expand All @@ -35,11 +34,7 @@ class BalanceService @Inject() (
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 { string => addressValidator.validate(string).map(Option.apply) }
.getOrElse(Good(None))
.toFutureOr
lastSeenAddress <- validate(lastSeenAddressString, addressValidator.validate).toFutureOr

data <- balanceFutureDataHandler.getHighestBalances(limit, lastSeenAddress).toFutureOr
} yield WrappedResult(data)
Expand Down
8 changes: 2 additions & 6 deletions server/app/com/xsn/explorer/services/BlockService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.xsn.explorer.models.rpc.{Block, TransactionVIN}
import com.xsn.explorer.models.values.{Blockhash, Height}
import com.xsn.explorer.parsers.OrderingConditionParser
import com.xsn.explorer.services.logic.{BlockLogic, TransactionLogic}
import com.xsn.explorer.services.validators.BlockhashValidator
import com.xsn.explorer.services.validators._
import com.xsn.explorer.util.Extensions.FutureOrExt
import javax.inject.Inject
import org.scalactic.{Bad, Good}
Expand Down Expand Up @@ -42,11 +42,7 @@ class BlockService @Inject() (
implicit writes: Writes[BlockHeader]): FutureApplicationResult[JsValue] = {

val result = for {
lastSeenHash <- lastSeenHashString
.map { string => blockhashValidator.validate(string).map(Option.apply) }
.getOrElse(Good(None))
.toFutureOr

lastSeenHash <- validate(lastSeenHashString, blockhashValidator.validate).toFutureOr
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxHeadersPerQuery).toFutureOr
orderingCondition <- orderingConditionParser.parseReuslt(orderingConditionString).toFutureOr

Expand Down
21 changes: 4 additions & 17 deletions server/app/com/xsn/explorer/services/TransactionService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import com.xsn.explorer.data.async.TransactionFutureDataHandler
import com.xsn.explorer.models._
import com.xsn.explorer.models.transformers._
import com.xsn.explorer.parsers.{OrderingConditionParser, TransactionOrderingParser}
import com.xsn.explorer.services.validators.{AddressValidator, BlockhashValidator, TransactionIdValidator}
import com.xsn.explorer.services.validators._
import javax.inject.Inject
import org.scalactic._
import org.slf4j.LoggerFactory

import scala.concurrent.ExecutionContext
Expand Down Expand Up @@ -57,11 +56,7 @@ class TransactionService @Inject() (
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr
orderingCondition <- orderingConditionParser.parseReuslt(orderingConditionString).toFutureOr

lastSeenTxid <- lastSeenTxidString
.map { string => transactionIdValidator.validate(string).map(Option.apply) }
.getOrElse(Good(None))
.toFutureOr

lastSeenTxid <- validate(lastSeenTxidString, transactionIdValidator.validate).toFutureOr
transactions <- transactionFutureDataHandler.getBy(address, limit, lastSeenTxid, orderingCondition).toFutureOr
} yield {
val lightTxs = transactions.map(toLightWalletTransaction)
Expand All @@ -88,11 +83,7 @@ class TransactionService @Inject() (
blockhash <- blockhashValidator.validate(blockhashString).toFutureOr
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr

lastSeenTxid <- lastSeenTxidString
.map { string => transactionIdValidator.validate(string).map(Option.apply) }
.getOrElse(Good(None))
.toFutureOr

lastSeenTxid <- validate(lastSeenTxidString, transactionIdValidator.validate).toFutureOr
r <- transactionFutureDataHandler.getByBlockhash(blockhash, limit, lastSeenTxid).toFutureOr
} yield WrappedResult(r)

Expand All @@ -108,11 +99,7 @@ class TransactionService @Inject() (
blockhash <- blockhashValidator.validate(blockhashString).toFutureOr
_ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr

lastSeenTxid <- lastSeenTxidString
.map { string => transactionIdValidator.validate(string).map(Option.apply) }
.getOrElse(Good(None))
.toFutureOr

lastSeenTxid <- validate(lastSeenTxidString, transactionIdValidator.validate).toFutureOr
transactions <- transactionFutureDataHandler.getTransactionsWithIOBy(blockhash, limit, lastSeenTxid).toFutureOr
} yield {
val lightTxs = transactions.map(toLightWalletTransaction)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ package com.xsn.explorer.services

import com.alexitc.playsonify.core.ApplicationResult
import com.alexitc.playsonify.models.ApplicationError
import org.scalactic.{One, Or}
import org.scalactic.{Good, One, Or}

package object validators {

def optional[T](string: String, error: ApplicationError)(builder: String => Option[T]): ApplicationResult[T] = {
val maybe = builder(string)
Or.from(maybe, One(error))
}

def validate[T](maybe: Option[String], validator: String => ApplicationResult[T]): ApplicationResult[Option[T]] = {
maybe
.map { string => validator(string).map(Option.apply) }
.getOrElse(Good(None))
}
}

0 comments on commit 21fcb82

Please sign in to comment.