diff --git a/server/app/com/xsn/explorer/data/BlockDataHandler.scala b/server/app/com/xsn/explorer/data/BlockDataHandler.scala index ecaa087..1547bdb 100644 --- a/server/app/com/xsn/explorer/data/BlockDataHandler.scala +++ b/server/app/com/xsn/explorer/data/BlockDataHandler.scala @@ -1,8 +1,8 @@ package com.xsn.explorer.data import com.alexitc.playsonify.core.ApplicationResult -import com.xsn.explorer.models.Blockhash import com.xsn.explorer.models.rpc.Block +import com.xsn.explorer.models.{Blockhash, Height} import scala.language.higherKinds @@ -12,6 +12,8 @@ trait BlockDataHandler[F[_]] { def getBy(blockhash: Blockhash): F[Block] + def getBy(height: Height): F[Block] + def delete(blockhash: Blockhash): F[Block] def getLatestBlock(): F[Block] diff --git a/server/app/com/xsn/explorer/data/anorm/BlockPostgresDataHandler.scala b/server/app/com/xsn/explorer/data/anorm/BlockPostgresDataHandler.scala index 780ebd6..59ff045 100644 --- a/server/app/com/xsn/explorer/data/anorm/BlockPostgresDataHandler.scala +++ b/server/app/com/xsn/explorer/data/anorm/BlockPostgresDataHandler.scala @@ -6,8 +6,8 @@ import com.alexitc.playsonify.core.ApplicationResult import com.xsn.explorer.data.BlockBlockingDataHandler import com.xsn.explorer.data.anorm.dao.BlockPostgresDAO import com.xsn.explorer.errors._ -import com.xsn.explorer.models.Blockhash import com.xsn.explorer.models.rpc.Block +import com.xsn.explorer.models.{Blockhash, Height} import org.scalactic.{One, Or} import play.api.db.Database @@ -37,6 +37,11 @@ class BlockPostgresDataHandler @Inject() ( Or.from(maybe, One(BlockNotFoundError)) } + override def getBy(height: Height): ApplicationResult[Block] = withConnection { implicit conn => + val maybe = blockPostgresDAO.getBy(height) + Or.from(maybe, One(BlockNotFoundError)) + } + override def delete(blockhash: Blockhash): ApplicationResult[Block] = database.withConnection { implicit conn => val maybe = blockPostgresDAO.delete(blockhash) Or.from(maybe, One(BlockNotFoundError)) diff --git a/server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala b/server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala index 1b5f6ba..dc3ac78 100644 --- a/server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala +++ b/server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala @@ -4,8 +4,8 @@ import java.sql.Connection import anorm._ import com.xsn.explorer.data.anorm.parsers.BlockParsers._ -import com.xsn.explorer.models.Blockhash import com.xsn.explorer.models.rpc.Block +import com.xsn.explorer.models.{Blockhash, Height} class BlockPostgresDAO { @@ -75,6 +75,19 @@ class BlockPostgresDAO { ).as(parseBlock.singleOpt).flatten } + def getBy(height: Height)(implicit conn: Connection): Option[Block] = { + SQL( + """ + |SELECT blockhash, previous_blockhash, next_blockhash, tpos_contract, merkle_root, size, + | height, version, time, median_time, nonce, bits, chainwork, difficulty + |FROM blocks + |WHERE height = {height} + """.stripMargin + ).on( + "height" -> height.int + ).as(parseBlock.singleOpt).flatten + } + def delete(blockhash: Blockhash)(implicit conn: Connection): Option[Block] = { SQL( """ diff --git a/server/app/com/xsn/explorer/data/async/BlockFutureDataHandler.scala b/server/app/com/xsn/explorer/data/async/BlockFutureDataHandler.scala index 15e931d..e7d4071 100644 --- a/server/app/com/xsn/explorer/data/async/BlockFutureDataHandler.scala +++ b/server/app/com/xsn/explorer/data/async/BlockFutureDataHandler.scala @@ -5,8 +5,8 @@ import javax.inject.Inject import com.alexitc.playsonify.core.FutureApplicationResult import com.xsn.explorer.data.{BlockBlockingDataHandler, BlockDataHandler} import com.xsn.explorer.executors.DatabaseExecutionContext -import com.xsn.explorer.models.Blockhash import com.xsn.explorer.models.rpc.Block +import com.xsn.explorer.models.{Blockhash, Height} import scala.concurrent.Future @@ -15,23 +15,27 @@ class BlockFutureDataHandler @Inject() ( implicit ec: DatabaseExecutionContext) extends BlockDataHandler[FutureApplicationResult] { - def insert(block: Block): FutureApplicationResult[Block] = Future { + override def insert(block: Block): FutureApplicationResult[Block] = Future { blockBlockingDataHandler.insert(block) } - def getBy(blockhash: Blockhash): FutureApplicationResult[Block] = Future { + override def getBy(blockhash: Blockhash): FutureApplicationResult[Block] = Future { blockBlockingDataHandler.getBy(blockhash) } - def delete(blockhash: Blockhash): FutureApplicationResult[Block] = Future { + override def getBy(height: Height): FutureApplicationResult[Block] = Future { + blockBlockingDataHandler.getBy(height) + } + + override def delete(blockhash: Blockhash): FutureApplicationResult[Block] = Future { blockBlockingDataHandler.delete(blockhash) } - def getLatestBlock(): FutureApplicationResult[Block] = Future { + override def getLatestBlock(): FutureApplicationResult[Block] = Future { blockBlockingDataHandler.getLatestBlock() } - def getFirstBlock(): FutureApplicationResult[Block] = Future { + override def getFirstBlock(): FutureApplicationResult[Block] = Future { blockBlockingDataHandler.getFirstBlock() } }