From 5a5f6073174ebe83f4fb78ddbe00bc3d725b5cf2 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sat, 16 Feb 2019 23:25:23 -0700 Subject: [PATCH] server: Create the model transformers package The logic to transform a rpc block to a persisted block lives here. --- .../explorer/models/transformers/package.scala | 18 ++++++++++++++++++ .../services/LedgerSynchronizerService.scala | 8 ++------ 2 files changed, 20 insertions(+), 6 deletions(-) create mode 100644 server/app/com/xsn/explorer/models/transformers/package.scala diff --git a/server/app/com/xsn/explorer/models/transformers/package.scala b/server/app/com/xsn/explorer/models/transformers/package.scala new file mode 100644 index 00000000..d960f3fa --- /dev/null +++ b/server/app/com/xsn/explorer/models/transformers/package.scala @@ -0,0 +1,18 @@ +package com.xsn.explorer.models + +import com.xsn.explorer.models.persisted.Block +import io.scalaland.chimney.dsl._ + +/** + * The package is a bridge between model domains, for example, sometimes you will have a rpc.Block but need a + * persisted.Block, this is where the transormation logic lives. + */ +package object transformers { + + def toPersistedBlock(rpcBlock: rpc.Block): persisted.Block = { + rpcBlock + .into[Block] + .withFieldConst(_.extractionMethod, Block.ExtractionMethod.ProofOfWork) // TODO: Get proper method + .transform + } +} diff --git a/server/app/com/xsn/explorer/services/LedgerSynchronizerService.scala b/server/app/com/xsn/explorer/services/LedgerSynchronizerService.scala index 5bb2eaa5..02c13490 100644 --- a/server/app/com/xsn/explorer/services/LedgerSynchronizerService.scala +++ b/server/app/com/xsn/explorer/services/LedgerSynchronizerService.scala @@ -5,9 +5,9 @@ import com.alexitc.playsonify.core.FutureOr.Implicits.{FutureOps, OptionOps} import com.xsn.explorer.data.async.{BlockFutureDataHandler, LedgerFutureDataHandler} import com.xsn.explorer.errors.BlockNotFoundError import com.xsn.explorer.models.persisted.Block +import com.xsn.explorer.models.transformers._ import com.xsn.explorer.models.{Blockhash, Height, Transaction} import com.xsn.explorer.util.Extensions.FutureOrExt -import io.scalaland.chimney.dsl._ import javax.inject.Inject import org.scalactic.Good import org.slf4j.LoggerFactory @@ -162,11 +162,7 @@ class LedgerSynchronizerService @Inject() ( val result = for { rpcBlock <- xsnService.getBlock(blockhash).toFutureOr transactions <- transactionRPCService.getTransactions(rpcBlock.transactions).toFutureOr - block = rpcBlock - .into[Block] - .withFieldConst(_.extractionMethod, Block.ExtractionMethod.ProofOfWork) // TODO: Get proper method - .transform - } yield (block, transactions) + } yield (toPersistedBlock(rpcBlock), transactions) result.toFuture }