From 8a9ce8179bc3c860ee5d63b8d5049bc0ec39ebfe Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sat, 6 Apr 2019 17:22:28 -0700 Subject: [PATCH] server: Use the TransactionIdValidator on the TransactionService --- .../services/TransactionService.scala | 36 ++++++++----------- .../validators/TransactionIdValidator.scala | 12 +++++++ .../LedgerSynchronizerServiceSpec.scala | 3 +- 3 files changed, 28 insertions(+), 23 deletions(-) create mode 100644 server/app/com/xsn/explorer/services/validators/TransactionIdValidator.scala diff --git a/server/app/com/xsn/explorer/services/TransactionService.scala b/server/app/com/xsn/explorer/services/TransactionService.scala index 6bc659e..e2e93aa 100644 --- a/server/app/com/xsn/explorer/services/TransactionService.scala +++ b/server/app/com/xsn/explorer/services/TransactionService.scala @@ -11,7 +11,7 @@ import com.xsn.explorer.models._ import com.xsn.explorer.models.transformers._ import com.xsn.explorer.models.values._ import com.xsn.explorer.parsers.{OrderingConditionParser, TransactionOrderingParser} -import com.xsn.explorer.services.validators.AddressValidator +import com.xsn.explorer.services.validators.{AddressValidator, TransactionIdValidator} import javax.inject.Inject import org.scalactic._ import org.slf4j.LoggerFactory @@ -23,6 +23,7 @@ class TransactionService @Inject() ( orderingConditionParser: OrderingConditionParser, transactionOrderingParser: TransactionOrderingParser, addressValidator: AddressValidator, + transactionIdValidator: TransactionIdValidator, transactionFutureDataHandler: TransactionFutureDataHandler)( implicit ec: ExecutionContext) { @@ -57,13 +58,10 @@ class TransactionService @Inject() ( _ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr orderingCondition <- orderingConditionParser.parseReuslt(orderingConditionString).toFutureOr - lastSeenTxid <- { - lastSeenTxidString - .map(TransactionId.from) - .map { txid => Or.from(txid, One(TransactionFormatError)).map(Option.apply) } - .getOrElse(Good(Option.empty)) - .toFutureOr - } + lastSeenTxid <- lastSeenTxidString + .map { string => transactionIdValidator.validate(string).map(Option.apply) } + .getOrElse(Good(None)) + .toFutureOr transactions <- transactionFutureDataHandler.getBy(address, limit, lastSeenTxid, orderingCondition).toFutureOr } yield { @@ -91,13 +89,10 @@ class TransactionService @Inject() ( blockhash <- Or.from(Blockhash.from(blockhashString), One(BlockhashFormatError)).toFutureOr _ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr - lastSeenTxid <- { - lastSeenTxidString - .map(TransactionId.from) - .map { txid => Or.from(txid, One(TransactionFormatError)).map(Option.apply) } - .getOrElse(Good(Option.empty)) - .toFutureOr - } + lastSeenTxid <- lastSeenTxidString + .map { string => transactionIdValidator.validate(string).map(Option.apply) } + .getOrElse(Good(None)) + .toFutureOr r <- transactionFutureDataHandler.getByBlockhash(blockhash, limit, lastSeenTxid).toFutureOr } yield WrappedResult(r) @@ -114,13 +109,10 @@ class TransactionService @Inject() ( blockhash <- Or.from(Blockhash.from(blockhashString), One(BlockhashFormatError)).toFutureOr _ <- paginatedQueryValidator.validate(PaginatedQuery(Offset(0), limit), maxTransactionsPerQuery).toFutureOr - lastSeenTxid <- { - lastSeenTxidString - .map(TransactionId.from) - .map { txid => Or.from(txid, One(TransactionFormatError)).map(Option.apply) } - .getOrElse(Good(Option.empty)) - .toFutureOr - } + lastSeenTxid <- lastSeenTxidString + .map { string => transactionIdValidator.validate(string).map(Option.apply) } + .getOrElse(Good(None)) + .toFutureOr transactions <- transactionFutureDataHandler.getTransactionsWithIOBy(blockhash, limit, lastSeenTxid).toFutureOr } yield { diff --git a/server/app/com/xsn/explorer/services/validators/TransactionIdValidator.scala b/server/app/com/xsn/explorer/services/validators/TransactionIdValidator.scala new file mode 100644 index 0000000..56fcc5d --- /dev/null +++ b/server/app/com/xsn/explorer/services/validators/TransactionIdValidator.scala @@ -0,0 +1,12 @@ +package com.xsn.explorer.services.validators + +import com.alexitc.playsonify.core.ApplicationResult +import com.xsn.explorer.errors.TransactionFormatError +import com.xsn.explorer.models.values.TransactionId + +class TransactionIdValidator { + + def validate(string: String): ApplicationResult[TransactionId] = { + optional(string, TransactionFormatError)(TransactionId.from) + } +} diff --git a/server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala b/server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala index 8f54dc8..3f93ee6 100644 --- a/server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala +++ b/server/test/com/xsn/explorer/services/LedgerSynchronizerServiceSpec.scala @@ -13,7 +13,7 @@ import com.xsn.explorer.models.rpc.Block import com.xsn.explorer.models.values.{Blockhash, Height} import com.xsn.explorer.parsers.{OrderingConditionParser, TransactionOrderingParser} import com.xsn.explorer.services.logic.{BlockLogic, TransactionLogic} -import com.xsn.explorer.services.validators.{AddressValidator, BlockhashValidator} +import com.xsn.explorer.services.validators.{AddressValidator, BlockhashValidator, TransactionIdValidator} import org.scalactic.{Bad, Good, One, Or} import org.scalatest.BeforeAndAfter import org.scalatest.concurrent.ScalaFutures @@ -217,6 +217,7 @@ class LedgerSynchronizerServiceSpec extends PostgresDataHandlerSpec with BeforeA new OrderingConditionParser, new TransactionOrderingParser, new AddressValidator, + new TransactionIdValidator, new TransactionFutureDataHandler(transactionDataHandler)(Executors.databaseEC)) val blockService = new BlockService(