diff --git a/server/app/com/xsn/explorer/services/BalanceService.scala b/server/app/com/xsn/explorer/services/BalanceService.scala index 33177eb..2f34d57 100644 --- a/server/app/com/xsn/explorer/services/BalanceService.scala +++ b/server/app/com/xsn/explorer/services/BalanceService.scala @@ -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 @@ -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) diff --git a/server/app/com/xsn/explorer/services/BlockService.scala b/server/app/com/xsn/explorer/services/BlockService.scala index 3748a1d..95c20ff 100644 --- a/server/app/com/xsn/explorer/services/BlockService.scala +++ b/server/app/com/xsn/explorer/services/BlockService.scala @@ -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} @@ -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 diff --git a/server/app/com/xsn/explorer/services/TransactionService.scala b/server/app/com/xsn/explorer/services/TransactionService.scala index 862b20f..b4f0282 100644 --- a/server/app/com/xsn/explorer/services/TransactionService.scala +++ b/server/app/com/xsn/explorer/services/TransactionService.scala @@ -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 @@ -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) @@ -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) @@ -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) diff --git a/server/app/com/xsn/explorer/services/validators/package.scala b/server/app/com/xsn/explorer/services/validators/package.scala index 1af28ee..4151008 100644 --- a/server/app/com/xsn/explorer/services/validators/package.scala +++ b/server/app/com/xsn/explorer/services/validators/package.scala @@ -2,7 +2,7 @@ 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 { @@ -10,4 +10,10 @@ package object validators { 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)) + } }