From c594de3a148f07f9aae66eb5039c0ed08e32d46b Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sun, 1 Jul 2018 20:42:00 -0500 Subject: [PATCH] server: Clean legacy code from TransactionDataHandler (#34) --- .../data/TransactionDataHandler.scala | 6 --- .../TransactionPostgresDataHandler.scala | 18 +------ .../async/TransactionFutureDataHandler.scala | 12 ----- .../TransactionPostgresDataHandlerSpec.scala | 50 ++++++++++++++----- .../helpers/TransactionDummyDataHandler.scala | 6 --- 5 files changed, 39 insertions(+), 53 deletions(-) diff --git a/server/app/com/xsn/explorer/data/TransactionDataHandler.scala b/server/app/com/xsn/explorer/data/TransactionDataHandler.scala index e263ddc..39599dd 100644 --- a/server/app/com/xsn/explorer/data/TransactionDataHandler.scala +++ b/server/app/com/xsn/explorer/data/TransactionDataHandler.scala @@ -9,12 +9,6 @@ import scala.language.higherKinds trait TransactionDataHandler[F[_]] { - def upsert(transaction: Transaction): F[Transaction] - - def delete(transactionId: TransactionId): F[Transaction] - - def deleteBy(blockhash: Blockhash): F[List[Transaction]] - def getBy( address: Address, paginatedQuery: PaginatedQuery, diff --git a/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala b/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala index a649837..4d5d206 100644 --- a/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala +++ b/server/app/com/xsn/explorer/data/anorm/TransactionPostgresDataHandler.scala @@ -6,10 +6,9 @@ import com.alexitc.playsonify.core.ApplicationResult import com.alexitc.playsonify.models.{FieldOrdering, PaginatedQuery, PaginatedResult} import com.xsn.explorer.data.TransactionBlockingDataHandler import com.xsn.explorer.data.anorm.dao.TransactionPostgresDAO -import com.xsn.explorer.errors.{TransactionNotFoundError, TransactionUnknownError} import com.xsn.explorer.models._ import com.xsn.explorer.models.fields.TransactionField -import org.scalactic.{Good, One, Or} +import org.scalactic.Good import play.api.db.Database class TransactionPostgresDataHandler @Inject() ( @@ -18,21 +17,6 @@ class TransactionPostgresDataHandler @Inject() ( extends TransactionBlockingDataHandler with AnormPostgresDataHandler { - override def upsert(transaction: Transaction): ApplicationResult[Transaction] = withTransaction { implicit conn => - val maybe = transactionPostgresDAO.upsert(transaction) - Or.from(maybe, One(TransactionUnknownError)) - } - - override def delete(transactionId: TransactionId): ApplicationResult[Transaction] = withTransaction { implicit conn => - val maybe = transactionPostgresDAO.delete(transactionId) - Or.from(maybe, One(TransactionNotFoundError)) - } - - override def deleteBy(blockhash: Blockhash): ApplicationResult[List[Transaction]] = withTransaction { implicit conn => - val transactions = transactionPostgresDAO.deleteBy(blockhash) - Good(transactions) - } - override def getBy( address: Address, paginatedQuery: PaginatedQuery, diff --git a/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala b/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala index bbf5df7..c3494af 100644 --- a/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala +++ b/server/app/com/xsn/explorer/data/async/TransactionFutureDataHandler.scala @@ -16,18 +16,6 @@ class TransactionFutureDataHandler @Inject() ( implicit ec: DatabaseExecutionContext) extends TransactionDataHandler[FutureApplicationResult] { - override def upsert(transaction: Transaction): FutureApplicationResult[Transaction] = Future { - blockingDataHandler.upsert(transaction) - } - - override def delete(transactionId: TransactionId): FutureApplicationResult[Transaction] = Future { - blockingDataHandler.delete(transactionId) - } - - override def deleteBy(blockhash: Blockhash): FutureApplicationResult[List[Transaction]] = Future { - blockingDataHandler.deleteBy(blockhash) - } - override def getBy( address: Address, paginatedQuery: PaginatedQuery, diff --git a/server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala b/server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala index 2ffa6f2..7ef1fba 100644 --- a/server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala +++ b/server/test/com/xsn/explorer/data/TransactionPostgresDataHandlerSpec.scala @@ -44,7 +44,9 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be 12312312L, Size(1000), List.empty, - List.empty + List( + Transaction.Output(0, 1000, createAddress("Xbh5pJdBNm8J9PxnEmwVcuQKRmZZ7Dkpss"), HexString.from("00").get, None, None) + ) ) val inputs = List( @@ -84,12 +86,36 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be private def prepareTransaction(transaction: Transaction) = { try { - dataHandler.upsert(transaction) + upsertTransaction(transaction) } catch { case _ => () } } + private def upsertTransaction(transaction: Transaction) = { + val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter) + database.withConnection { implicit conn => + val maybe = dao.upsert(transaction) + Or.from(maybe, One(TransactionNotFoundError)) + } + } + + private def delete(txid: TransactionId) = { + val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter) + database.withConnection { implicit conn => + val maybe = dao.delete(txid) + Or.from(maybe, One(TransactionNotFoundError)) + } + } + + private def deleteBy(blockhash: Blockhash) = { + val dao = new TransactionPostgresDAO(new FieldOrderingSQLInterpreter) + database.withConnection { implicit conn => + val result = dao.deleteBy(blockhash) + Good(result) + } + } + before { clearDatabase() prepareBlock(block) @@ -98,7 +124,7 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be "upsert" should { "add a new transaction" in { - val result = dataHandler.upsert(transaction) + val result = upsertTransaction(transaction) result mustEqual Good(transaction) } @@ -107,31 +133,31 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be time = 2313121L, size = Size(2000)) - dataHandler.upsert(transaction).isGood mustEqual true - val result = dataHandler.upsert(newTransaction) + upsertTransaction(transaction).isGood mustEqual true + val result = upsertTransaction(newTransaction) result mustEqual Good(newTransaction) } } "delete" should { "delete a transaction" in { - dataHandler.upsert(transaction).isGood mustEqual true - val result = dataHandler.delete(transaction.id) + upsertTransaction(transaction).isGood mustEqual true + val result = delete(transaction.id) result mustEqual Good(transaction) } "fail to delete a non-existent transaction" in { - dataHandler.delete(transaction.id) - val result = dataHandler.delete(transaction.id) + delete(transaction.id) + val result = delete(transaction.id) result mustEqual Bad(TransactionNotFoundError).accumulating } } "deleteBy blockhash" should { "delete the transactions related to a block" in { - dataHandler.upsert(transaction).isGood mustEqual true + upsertTransaction(transaction).isGood mustEqual true - val result = dataHandler.deleteBy(transaction.blockhash) + val result = deleteBy(transaction.blockhash) result.isGood mustEqual true result.get.contains(transaction) mustEqual true } @@ -178,7 +204,7 @@ class TransactionPostgresDataHandlerSpec extends PostgresDataHandlerSpec with Be received = 50) val expected = PaginatedResult(query.offset, query.limit, Count(1), List(transactionWithValues)) - dataHandler.upsert(transaction).isGood mustEqual true + upsertTransaction(transaction).isGood mustEqual true val result = dataHandler.getBy(address, query, defaultOrdering) result mustEqual Good(expected) diff --git a/server/test/com/xsn/explorer/helpers/TransactionDummyDataHandler.scala b/server/test/com/xsn/explorer/helpers/TransactionDummyDataHandler.scala index cecf4a4..b1e70f6 100644 --- a/server/test/com/xsn/explorer/helpers/TransactionDummyDataHandler.scala +++ b/server/test/com/xsn/explorer/helpers/TransactionDummyDataHandler.scala @@ -8,11 +8,5 @@ import com.xsn.explorer.models.fields.TransactionField class TransactionDummyDataHandler extends TransactionBlockingDataHandler { - override def upsert(transaction: Transaction): ApplicationResult[Transaction] = ??? - - override def delete(transactionId: TransactionId): ApplicationResult[Transaction] = ??? - - override def deleteBy(blockhash: Blockhash): ApplicationResult[List[Transaction]] = ??? - override def getBy(address: Address, paginatedQuery: PaginatedQuery, ordering: FieldOrdering[TransactionField]): ApplicationResult[PaginatedResult[TransactionWithValues]] = ??? }