From 8c3a0cb3e690c3d714b8d883059ad7489888c779 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sun, 31 Mar 2019 13:07:58 -0700 Subject: [PATCH] server: Add the TPoSContractDataHandler --- .../data/TPoSContractDataHandler.scala | 14 ++++++++++ .../TPoSContractPostgresDataHandler.scala | 22 +++++++++++++++ .../data/anorm/dao/TPoSContractDAO.scala | 15 ++++++++++- .../async/TPoSContractFutureDataHandler.scala | 20 ++++++++++++++ .../explorer/modules/DataHandlerModule.scala | 1 + .../TPoSContractPostgresDataHandlerSpec.scala | 27 +++++++++++++++++++ 6 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 server/app/com/xsn/explorer/data/TPoSContractDataHandler.scala create mode 100644 server/app/com/xsn/explorer/data/anorm/TPoSContractPostgresDataHandler.scala create mode 100644 server/app/com/xsn/explorer/data/async/TPoSContractFutureDataHandler.scala create mode 100644 server/test/com/xsn/explorer/data/TPoSContractPostgresDataHandlerSpec.scala diff --git a/server/app/com/xsn/explorer/data/TPoSContractDataHandler.scala b/server/app/com/xsn/explorer/data/TPoSContractDataHandler.scala new file mode 100644 index 00000000..f9b8a1a5 --- /dev/null +++ b/server/app/com/xsn/explorer/data/TPoSContractDataHandler.scala @@ -0,0 +1,14 @@ +package com.xsn.explorer.data + +import com.alexitc.playsonify.core.ApplicationResult +import com.xsn.explorer.models.TPoSContract +import com.xsn.explorer.models.values.Address + +import scala.language.higherKinds + +trait TPoSContractDataHandler[F[_]] { + + def getBy(address: Address): F[List[TPoSContract]] +} + +trait TPoSContractBlockingDataHandler extends TPoSContractDataHandler[ApplicationResult] diff --git a/server/app/com/xsn/explorer/data/anorm/TPoSContractPostgresDataHandler.scala b/server/app/com/xsn/explorer/data/anorm/TPoSContractPostgresDataHandler.scala new file mode 100644 index 00000000..2bb64227 --- /dev/null +++ b/server/app/com/xsn/explorer/data/anorm/TPoSContractPostgresDataHandler.scala @@ -0,0 +1,22 @@ +package com.xsn.explorer.data.anorm + +import com.alexitc.playsonify.core.ApplicationResult +import com.xsn.explorer.data.TPoSContractBlockingDataHandler +import com.xsn.explorer.data.anorm.dao.TPoSContractDAO +import com.xsn.explorer.models.TPoSContract +import com.xsn.explorer.models.values.Address +import javax.inject.Inject +import org.scalactic.Good +import play.api.db.Database + +class TPoSContractPostgresDataHandler @Inject() ( + override val database: Database, + tposContractDAO: TPoSContractDAO) + extends TPoSContractBlockingDataHandler + with AnormPostgresDataHandler { + + def getBy(address: Address): ApplicationResult[List[TPoSContract]] = withConnection { implicit conn => + val result = tposContractDAO.getBy(address) + Good(result) + } +} diff --git a/server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala b/server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala index 245151fa..3ec981e7 100644 --- a/server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala +++ b/server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala @@ -5,7 +5,7 @@ import java.sql.Connection import anorm._ import com.xsn.explorer.data.anorm.parsers.TPoSContractParsers import com.xsn.explorer.models.TPoSContract -import com.xsn.explorer.models.values.TransactionId +import com.xsn.explorer.models.values.{Address, TransactionId} class TPoSContractDAO { @@ -75,4 +75,17 @@ class TPoSContractDAO { 'state -> TPoSContract.State.Active.entryName, ).executeUpdate() } + + def getBy(address: Address)(implicit conn: Connection): List[TPoSContract] = { + SQL( + """ + |SELECT txid, index, owner, merchant, merchant_commission, state, time + |FROM tpos_contracts + |WHERE owner = {address} OR merchant = {address} + |ORDER BY time DESC + """.stripMargin + ).on( + 'address -> address.string + ).as(parseTPoSContract.*) + } } diff --git a/server/app/com/xsn/explorer/data/async/TPoSContractFutureDataHandler.scala b/server/app/com/xsn/explorer/data/async/TPoSContractFutureDataHandler.scala new file mode 100644 index 00000000..a1126a8e --- /dev/null +++ b/server/app/com/xsn/explorer/data/async/TPoSContractFutureDataHandler.scala @@ -0,0 +1,20 @@ +package com.xsn.explorer.data.async + +import com.alexitc.playsonify.core.FutureApplicationResult +import com.xsn.explorer.data.{TPoSContractBlockingDataHandler, TPoSContractDataHandler} +import com.xsn.explorer.executors.DatabaseExecutionContext +import com.xsn.explorer.models.TPoSContract +import com.xsn.explorer.models.values.Address +import javax.inject.Inject + +import scala.concurrent.Future + +class TPoSContractFutureDataHandler @Inject() ( + blockingDataHandler: TPoSContractBlockingDataHandler)( + implicit ec: DatabaseExecutionContext) + extends TPoSContractDataHandler[FutureApplicationResult] { + + override def getBy(address: Address): FutureApplicationResult[List[TPoSContract]] = Future { + blockingDataHandler.getBy(address) + } +} diff --git a/server/app/com/xsn/explorer/modules/DataHandlerModule.scala b/server/app/com/xsn/explorer/modules/DataHandlerModule.scala index 1c00e5e6..51c69af7 100644 --- a/server/app/com/xsn/explorer/modules/DataHandlerModule.scala +++ b/server/app/com/xsn/explorer/modules/DataHandlerModule.scala @@ -12,5 +12,6 @@ class DataHandlerModule extends AbstractModule { bind(classOf[StatisticsBlockingDataHandler]).to(classOf[StatisticsPostgresDataHandler]) bind(classOf[TransactionBlockingDataHandler]).to(classOf[TransactionPostgresDataHandler]) bind(classOf[LedgerBlockingDataHandler]).to(classOf[LedgerPostgresDataHandler]) + bind(classOf[TPoSContractBlockingDataHandler]).to(classOf[TPoSContractPostgresDataHandler]) } } diff --git a/server/test/com/xsn/explorer/data/TPoSContractPostgresDataHandlerSpec.scala b/server/test/com/xsn/explorer/data/TPoSContractPostgresDataHandlerSpec.scala new file mode 100644 index 00000000..32d2d186 --- /dev/null +++ b/server/test/com/xsn/explorer/data/TPoSContractPostgresDataHandlerSpec.scala @@ -0,0 +1,27 @@ +package com.xsn.explorer.data + +import com.xsn.explorer.data.anorm.TPoSContractPostgresDataHandler +import com.xsn.explorer.data.anorm.dao.TPoSContractDAO +import com.xsn.explorer.data.common.PostgresDataHandlerSpec +import com.xsn.explorer.helpers.DataGenerator +import com.xsn.explorer.models.TPoSContract + +class TPoSContractPostgresDataHandlerSpec extends PostgresDataHandlerSpec { + + val dao = new TPoSContractDAO + lazy val dataHandler = new TPoSContractPostgresDataHandler(database, dao) + + "getBy" should { + "return the contracts matching the owner or the merchant address" in { + val owner = DataGenerator.randomAddress + val merchant = DataGenerator.randomAddress + pending + } + } + + private def create(contract: TPoSContract): Unit = { + val _ = database.withConnection { implicit conn => + dao.create(contract) + } + } +}