Browse Source

server: Refactor TransactionService to remove the transaction transformer

The logic was moved to the transformers package.
master
Alexis Hernandez 6 years ago
parent
commit
fc0696a467
  1. 25
      server/app/com/xsn/explorer/models/transformers/package.scala
  2. 22
      server/app/com/xsn/explorer/services/TransactionService.scala

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

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

Loading…
Cancel
Save