Browse Source

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.
prometheus-integration
Alexis Hernandez 7 years ago
parent
commit
b4ad16dd56
  1. 17
      server/app/com/xsn/explorer/data/TransactionDataHandler.scala
  2. 10
      server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala
  3. 28
      server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala
  4. 5
      server/app/com/xsn/explorer/modules/DataHandlerModule.scala

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

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

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

5
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])
}
}

Loading…
Cancel
Save