From fc0696a46759ae9e2442a08826c8a3b189ba8c80 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sun, 17 Feb 2019 21:37:24 -0700 Subject: [PATCH] server: Refactor TransactionService to remove the transaction transformer The logic was moved to the transformers package. --- .../models/transformers/package.scala | 25 ++++++++++++++++++- .../services/TransactionService.scala | 22 ++-------------- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/server/app/com/xsn/explorer/models/transformers/package.scala b/server/app/com/xsn/explorer/models/transformers/package.scala index d960f3fa..903f02db 100644 --- a/server/app/com/xsn/explorer/models/transformers/package.scala +++ b/server/app/com/xsn/explorer/models/transformers/package.scala @@ -1,6 +1,6 @@ package com.xsn.explorer.models -import com.xsn.explorer.models.persisted.Block +import com.xsn.explorer.models.persisted.{Block, Transaction} import io.scalaland.chimney.dsl._ /** @@ -15,4 +15,27 @@ package object transformers { .withFieldConst(_.extractionMethod, Block.ExtractionMethod.ProofOfWork) // TODO: Get proper method .transform } + + def toLightWalletTransactionInput(input: Transaction.Input): LightWalletTransaction.Input = { + input + .into[LightWalletTransaction.Input] + .withFieldRenamed(_.fromOutputIndex, _.index) + .withFieldRenamed(_.fromTxid, _.txid) + .transform + } + + def toLightWalletTransactionOutput(output: Transaction.Output): LightWalletTransaction.Output = { + output.into[LightWalletTransaction.Output].transform + } + + def toLightWalletTransaction(tx: Transaction.HasIO): LightWalletTransaction = { + val inputs = tx.inputs.map(toLightWalletTransactionInput) + val outputs = tx.outputs.map(toLightWalletTransactionOutput) + + tx.transaction + .into[LightWalletTransaction] + .withFieldConst(_.inputs, inputs) + .withFieldConst(_.outputs, outputs) + .transform + } } diff --git a/server/app/com/xsn/explorer/services/TransactionService.scala b/server/app/com/xsn/explorer/services/TransactionService.scala index 35270c30..75c4f708 100644 --- a/server/app/com/xsn/explorer/services/TransactionService.scala +++ b/server/app/com/xsn/explorer/services/TransactionService.scala @@ -8,9 +8,9 @@ import com.alexitc.playsonify.validators.PaginatedQueryValidator import com.xsn.explorer.data.async.TransactionFutureDataHandler import com.xsn.explorer.errors._ import com.xsn.explorer.models._ +import com.xsn.explorer.models.transformers._ import com.xsn.explorer.models.values._ import com.xsn.explorer.parsers.TransactionOrderingParser -import io.scalaland.chimney.dsl._ import javax.inject.Inject import org.scalactic._ import org.slf4j.LoggerFactory @@ -71,25 +71,7 @@ class TransactionService @Inject() ( transactions <- transactionFutureDataHandler.getBy(address, limit, lastSeenTxid, orderingCondition).toFutureOr } yield { - val lightTxs = transactions.map { tx => - val inputs = tx.inputs.map { input => - input - .into[LightWalletTransaction.Input] - .withFieldRenamed(_.fromOutputIndex, _.index) - .withFieldRenamed(_.fromTxid, _.txid) - .transform - } - - val outputs = tx.outputs.map { output => - output.into[LightWalletTransaction.Output].transform - } - - tx.transaction - .into[LightWalletTransaction] - .withFieldConst(_.inputs, inputs) - .withFieldConst(_.outputs, outputs) - .transform - } + val lightTxs = transactions.map(toLightWalletTransaction) WrappedResult(lightTxs) }