Skip to content

Commit

Permalink
server: Add the TPoSContractDAO
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexITC committed Mar 31, 2019
1 parent 35f1b3b commit 87793c7
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
78 changes: 78 additions & 0 deletions server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.xsn.explorer.data.anorm.dao

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

class TPoSContractDAO {

import TPoSContractParsers._

def create(contract: TPoSContract)(implicit conn: Connection): TPoSContract = {
SQL(
"""
|INSERT INTO tpos_contracts
| (txid, index, owner, merchant, merchant_commission, state, time)
|VALUES
| ({txid}, {index}, {owner}, {merchant}, {merchant_commission}, {state}::TPOS_CONTRACT_STATE, {time})
|RETURNING txid, index, owner, merchant, merchant_commission, state, time
""".stripMargin
).on(
'txid -> contract.id.txid.string,
'index -> contract.id.index,
'owner -> contract.details.owner.string,
'merchant -> contract.details.merchant.string,
'merchant_commission -> contract.details.merchantCommission.int,
'state -> contract.state.entryName,
'time -> contract.time
).as(parseTPoSContract.single)
}

def deleteBy(txid: TransactionId)(implicit conn: Connection): Option[TPoSContract] = {
SQL(
"""
|DELETE FROM tpos_contracts
|WHERE txid = {txid}
|RETURNING txid, index, owner, merchant, merchant_commission, state, time
""".stripMargin
).on(
'txid -> txid.string
).as(parseTPoSContract.singleOpt)
}

def close(id: TPoSContract.Id, closedOn: TransactionId)(implicit conn: Connection): Unit = {
val _ = SQL(
"""
|UPDATE tpos_contracts
|SET state = {state}::TPOS_CONTRACT_STATE,
| closed_on = {closed_on}
|WHERE txid = {txid} AND
| index = {index}
""".stripMargin
).on(
'txid -> id.txid.string,
'index -> id.index,
'state -> TPoSContract.State.Closed.entryName,
'closed_on -> closedOn.string
).executeUpdate()
}

def open(id: TPoSContract.Id)(implicit conn: Connection): Unit = {
val _ = SQL(
"""
|UPDATE tpos_contracts
|SET state = {state}::TPOS_CONTRACT_STATE,
| closed_on = null
|WHERE txid = {txid} AND
| index = {index}
""".stripMargin
).on(
'txid -> id.txid.string,
'index -> id.index,
'state -> TPoSContract.State.Active.entryName,
).executeUpdate()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.xsn.explorer.data.anorm.parsers

import anorm.SqlParser._
import anorm._
import com.xsn.explorer.models.TPoSContract

object TPoSContractParsers {

import CommonParsers._

val parseOwner = parseAddress("owner")
val parseMerchant = parseAddress("merchant")
val parseMerchantCommission = int("merchant_commission")
.map(TPoSContract.Commission.from)
.map { _.getOrElse(throw new RuntimeException("corrupted merchant_commission")) }

val parseTPoSContractState = str("state")
.map(TPoSContract.State.withNameInsensitiveOption)
.map { _.getOrElse(throw new RuntimeException("corrupted state")) }

val parseTPoSContract = (
parseTransactionId() ~
parseIndex ~
parseOwner ~
parseMerchant ~
parseMerchantCommission ~
parseTime ~
parseTPoSContractState).map {

case txid ~ index ~ owner ~ merchant ~ merchantCommission ~ time ~ state =>
val details = TPoSContract.Details(owner = owner, merchant = merchant, merchantCommission = merchantCommission)
TPoSContract(TPoSContract.Id(txid, index), details, time, state)
}
}

0 comments on commit 87793c7

Please sign in to comment.