Browse Source

server: Create the model transformers package

The logic to transform a rpc block to a persisted block lives here.
master
Alexis Hernandez 6 years ago
parent
commit
5a5f607317
  1. 18
      server/app/com/xsn/explorer/models/transformers/package.scala
  2. 8
      server/app/com/xsn/explorer/services/LedgerSynchronizerService.scala

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

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

Loading…
Cancel
Save