Skip to content

Commit

Permalink
server: Create the model transformers package
Browse files Browse the repository at this point in the history
The logic to transform a rpc block to a persisted block lives here.
  • Loading branch information
AlexITC committed Feb 17, 2019
1 parent 21399f3 commit 5a5f607
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
18 changes: 18 additions & 0 deletions server/app/com/xsn/explorer/models/transformers/package.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 5a5f607

Please sign in to comment.