Browse Source

server: Add getBy height on BlockDataHandler

scalafmt-draft
Alexis Hernandez 7 years ago
parent
commit
7b8d12e4ed
  1. 4
      server/app/com/xsn/explorer/data/BlockDataHandler.scala
  2. 7
      server/app/com/xsn/explorer/data/anorm/BlockPostgresDataHandler.scala
  3. 15
      server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala
  4. 16
      server/app/com/xsn/explorer/data/async/BlockFutureDataHandler.scala

4
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]

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

15
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(
"""

16
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()
}
}

Loading…
Cancel
Save