Skip to content

Commit

Permalink
server: Add the TPoSContractDataHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Mar 31, 2019
1 parent f926b44 commit 8c3a0cb
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 1 deletion.
14 changes: 14 additions & 0 deletions server/app/com/xsn/explorer/data/TPoSContractDataHandler.scala
Original file line number Diff line number Diff line change
@@ -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]
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 1 deletion server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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.*)
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 8c3a0cb

Please sign in to comment.