From b4ad16dd56bf6584be05b89edd1b59fd9acfc80d Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sat, 16 Jun 2018 21:09:31 -0500 Subject: [PATCH] server: Create TransactionDataHandler trait The trait is implemented by TransactionPostgresDataHandler and TransactionPostgresDataHandler. This is a piece for creating endpoints interacting with transactions from the database instead of the xsn service. --- .../data/TransactionDataHandler.scala | 17 +++++++++++ .../TransactionPostgresDataHandler.scala | 10 ++++--- .../async/TransactionFutureDataHandler.scala | 28 +++++++++++++++++++ .../explorer/modules/DataHandlerModule.scala | 5 ++-- 4 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 server/app/com/xsn/explorer/data/TransactionDataHandler.scala create mode 100644 server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala diff --git a/server/app/com/xsn/explorer/data/TransactionDataHandler.scala b/server/app/com/xsn/explorer/data/TransactionDataHandler.scala new file mode 100644 index 0000000..e9393d8 --- /dev/null +++ b/server/app/com/xsn/explorer/data/TransactionDataHandler.scala @@ -0,0 +1,17 @@ +package com.xsn.explorer.data + +import com.alexitc.playsonify.core.ApplicationResult +import com.xsn.explorer.models.{Blockhash, Transaction, TransactionId} + +import scala.language.higherKinds + +trait TransactionDataHandler[F[_]] { + + def upsert(transaction: Transaction): F[Transaction] + + def delete(transactionId: TransactionId): F[Transaction] + + def deleteBy(blockhash: Blockhash): F[List[Transaction]] +} + +trait TransactionBlockingDataHandler extends TransactionDataHandler[ApplicationResult] diff --git a/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala b/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala index 86459fc..ccb17fa 100644 --- a/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala +++ b/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala @@ -3,6 +3,7 @@ package com.xsn.explorer.data.anorm import javax.inject.Inject import com.alexitc.playsonify.core.ApplicationResult +import com.xsn.explorer.data.TransactionBlockingDataHandler import com.xsn.explorer.data.anorm.dao.TransactionPostgresDAO import com.xsn.explorer.errors.{TransactionNotFoundError, TransactionUnknownError} import com.xsn.explorer.models.{Blockhash, Transaction, TransactionId} @@ -12,19 +13,20 @@ import play.api.db.Database class TransactionPostgresDataHandler @Inject() ( override val database: Database, transactionPostgresDAO: TransactionPostgresDAO) - extends AnormPostgresDataHandler { + extends TransactionBlockingDataHandler + with AnormPostgresDataHandler { - def upsert(transaction: Transaction): ApplicationResult[Transaction] = withTransaction { implicit conn => + override def upsert(transaction: Transaction): ApplicationResult[Transaction] = withTransaction { implicit conn => val maybe = transactionPostgresDAO.upsert(transaction) Or.from(maybe, One(TransactionUnknownError)) } - def delete(transactionId: TransactionId): ApplicationResult[Transaction] = withTransaction { implicit conn => + override def delete(transactionId: TransactionId): ApplicationResult[Transaction] = withTransaction { implicit conn => val maybe = transactionPostgresDAO.delete(transactionId) Or.from(maybe, One(TransactionNotFoundError)) } - def deleteBy(blockhash: Blockhash) = withTransaction { implicit conn => + override def deleteBy(blockhash: Blockhash): ApplicationResult[List[Transaction]] = withTransaction { implicit conn => val transactions = transactionPostgresDAO.deleteBy(blockhash) Good(transactions) } diff --git a/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala b/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala new file mode 100644 index 0000000..c3e9166 --- /dev/null +++ b/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala @@ -0,0 +1,28 @@ +package com.xsn.explorer.data.async + +import javax.inject.Inject + +import com.alexitc.playsonify.core.FutureApplicationResult +import com.xsn.explorer.data.{TransactionBlockingDataHandler, TransactionDataHandler} +import com.xsn.explorer.executors.DatabaseExecutionContext +import com.xsn.explorer.models.{Blockhash, Transaction, TransactionId} + +import scala.concurrent.Future + +class TransactionFutureDataHandler @Inject() ( + blockingDataHandler: TransactionBlockingDataHandler)( + implicit ec: DatabaseExecutionContext) + extends TransactionDataHandler[FutureApplicationResult] { + + override def upsert(transaction: Transaction): FutureApplicationResult[Transaction] = Future { + blockingDataHandler.upsert(transaction) + } + + override def delete(transactionId: TransactionId): FutureApplicationResult[Transaction] = Future { + blockingDataHandler.delete(transactionId) + } + + override def deleteBy(blockhash: Blockhash): FutureApplicationResult[List[Transaction]] = Future { + blockingDataHandler.deleteBy(blockhash) + } +} diff --git a/server/app/com/xsn/explorer/modules/DataHandlerModule.scala b/server/app/com/xsn/explorer/modules/DataHandlerModule.scala index a194537..fe11433 100644 --- a/server/app/com/xsn/explorer/modules/DataHandlerModule.scala +++ b/server/app/com/xsn/explorer/modules/DataHandlerModule.scala @@ -1,8 +1,8 @@ package com.xsn.explorer.modules import com.google.inject.AbstractModule -import com.xsn.explorer.data.anorm.{BalancePostgresDataHandler, BlockPostgresDataHandler, DatabasePostgresSeeder, StatisticsPostgresDataHandler} -import com.xsn.explorer.data.{BalanceBlockingDataHandler, BlockBlockingDataHandler, DatabaseBlockingSeeder, StatisticsBlockingDataHandler} +import com.xsn.explorer.data._ +import com.xsn.explorer.data.anorm._ class DataHandlerModule extends AbstractModule { @@ -11,5 +11,6 @@ class DataHandlerModule extends AbstractModule { bind(classOf[BalanceBlockingDataHandler]).to(classOf[BalancePostgresDataHandler]) bind(classOf[StatisticsBlockingDataHandler]).to(classOf[StatisticsPostgresDataHandler]) bind(classOf[DatabaseBlockingSeeder]).to(classOf[DatabasePostgresSeeder]) + bind(classOf[TransactionBlockingDataHandler]).to(classOf[TransactionPostgresDataHandler]) } }