From 87793c7bcbce8bc74d4c126d6b01277c6d60aa1d Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sat, 30 Mar 2019 21:19:45 -0700 Subject: [PATCH] server: Add the TPoSContractDAO --- .../data/anorm/dao/TPoSContractDAO.scala | 78 +++++++++++++++++++ .../anorm/parsers/TPoSContractParsers.scala | 34 ++++++++ 2 files changed, 112 insertions(+) create mode 100644 server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala create mode 100644 server/app/com/xsn/explorer/data/anorm/parsers/TPoSContractParsers.scala diff --git a/server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala b/server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala new file mode 100644 index 0000000..245151f --- /dev/null +++ b/server/app/com/xsn/explorer/data/anorm/dao/TPoSContractDAO.scala @@ -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() + } +} diff --git a/server/app/com/xsn/explorer/data/anorm/parsers/TPoSContractParsers.scala b/server/app/com/xsn/explorer/data/anorm/parsers/TPoSContractParsers.scala new file mode 100644 index 0000000..36c04cd --- /dev/null +++ b/server/app/com/xsn/explorer/data/anorm/parsers/TPoSContractParsers.scala @@ -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) + } +}