From a8fb6930185b98e24165439bcb1549cdd30fd446 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sun, 21 Oct 2018 17:12:21 -0600 Subject: [PATCH] server: Reuse the paginated query on the BlockPostgresDAO The getFirstBlock and getLatestBlock methods use the paginated query to get the results. --- .../data/anorm/dao/BlockPostgresDAO.scala | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) 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 11e4bc4..9633af0 100644 --- a/server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala +++ b/server/app/com/xsn/explorer/data/anorm/dao/BlockPostgresDAO.scala @@ -4,7 +4,7 @@ import java.sql.Connection import javax.inject.Inject import anorm._ -import com.alexitc.playsonify.models.{Count, FieldOrdering, PaginatedQuery} +import com.alexitc.playsonify.models._ import com.xsn.explorer.data.anorm.interpreters.FieldOrderingSQLInterpreter import com.xsn.explorer.data.anorm.parsers.BlockParsers._ import com.xsn.explorer.models.fields.BlockField @@ -138,26 +138,14 @@ class BlockPostgresDAO @Inject() (fieldOrderingSQLInterpreter: FieldOrderingSQLI } def getLatestBlock(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 - |ORDER BY height DESC - |LIMIT 1 - """.stripMargin - ).as(parseBlock.singleOpt).flatten + val query = PaginatedQuery(Offset(0), Limit(1)) + val ordering = FieldOrdering(BlockField.Height, OrderingCondition.DescendingOrder) + getBy(query, ordering).headOption } def getFirstBlock(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 - |ORDER BY height - |LIMIT 1 - """.stripMargin - ).as(parseBlock.singleOpt).flatten + val query = PaginatedQuery(Offset(0), Limit(1)) + val ordering = FieldOrdering(BlockField.Height, OrderingCondition.AscendingOrder) + getBy(query, ordering).headOption } }