Browse Source

server: Add the TPoSContractDataHandler

master
Alexis Hernandez 6 years ago
parent
commit
8c3a0cb3e6
  1. 14
      server/app/com/xsn/explorer/data/TPoSContractDataHandler.scala
  2. 22
      server/app/com/xsn/explorer/data/anorm/TPoSContractPostgresDataHandler.scala
  3. 15
      server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala
  4. 20
      server/app/com/xsn/explorer/data/async/TPoSContractFutureDataHandler.scala
  5. 1
      server/app/com/xsn/explorer/modules/DataHandlerModule.scala
  6. 27
      server/test/com/xsn/explorer/data/TPoSContractPostgresDataHandlerSpec.scala

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

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

15
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.*)
}
}

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

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

27
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)
}
}
}
Loading…
Cancel
Save