-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
server/app/com/xsn/explorer/data/anorm/parsers/TPoSContractParsers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |