From 1ec1629a1970352ef9a7f7af004aab45c443b90d Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Tue, 4 Dec 2018 16:16:54 -0700 Subject: [PATCH] server: Update the endpoint for retrieving latest transactions --- .../request/GetLatestTransactionRequest.scala | 13 +++++++++++++ server/app/controllers/TransactionsController.scala | 12 +++++------- .../controllers/TransactionsControllerSpec.scala | 8 ++++---- 3 files changed, 22 insertions(+), 11 deletions(-) create mode 100644 server/app/com/xsn/explorer/models/request/GetLatestTransactionRequest.scala diff --git a/server/app/com/xsn/explorer/models/request/GetLatestTransactionRequest.scala b/server/app/com/xsn/explorer/models/request/GetLatestTransactionRequest.scala new file mode 100644 index 0000000..06e9560 --- /dev/null +++ b/server/app/com/xsn/explorer/models/request/GetLatestTransactionRequest.scala @@ -0,0 +1,13 @@ +package com.xsn.explorer.models.request + +import com.xsn.explorer.models.Address +import org.scalactic.Every +import play.api.libs.json.{Json, Reads} +import controllers.common.Codecs.everyReads + +case class GetLatestTransactionRequest(addresses: Every[Address]) + +object GetLatestTransactionRequest { + + implicit val reads: Reads[GetLatestTransactionRequest] = Json.reads[GetLatestTransactionRequest] +} diff --git a/server/app/controllers/TransactionsController.scala b/server/app/controllers/TransactionsController.scala index 1dc886f..acd7f54 100644 --- a/server/app/controllers/TransactionsController.scala +++ b/server/app/controllers/TransactionsController.scala @@ -1,19 +1,17 @@ package controllers import javax.inject.Inject + import com.alexitc.playsonify.models.PublicContextWithModel -import com.xsn.explorer.models.Address -import com.xsn.explorer.models.request.SendRawTransactionRequest +import com.xsn.explorer.models.request.{GetLatestTransactionRequest, SendRawTransactionRequest} import com.xsn.explorer.services.TransactionService -import controllers.common.{Codecs,MyJsonController, MyJsonControllerComponents} -import org.scalactic.Every +import controllers.common.{MyJsonController, MyJsonControllerComponents} class TransactionsController @Inject() ( transactionService: TransactionService, cc: MyJsonControllerComponents) extends MyJsonController(cc) { - import Codecs._ def getTransaction(txid: String) = publicNoInput { _ => transactionService.getTransactionDetails(txid) } @@ -26,7 +24,7 @@ class TransactionsController @Inject() ( transactionService.sendRawTransaction(ctx.model.hex) } - def getLatestByAddresses() = publicWithInput { ctx: PublicContextWithModel[Every[Address]] => - transactionService.getLatestTransactionBy(ctx.model) + def getLatestByAddresses() = publicWithInput { ctx: PublicContextWithModel[GetLatestTransactionRequest] => + transactionService.getLatestTransactionBy(ctx.model.addresses) } } diff --git a/server/test/controllers/TransactionsControllerSpec.scala b/server/test/controllers/TransactionsControllerSpec.scala index ae9821d..a90a3cc 100644 --- a/server/test/controllers/TransactionsControllerSpec.scala +++ b/server/test/controllers/TransactionsControllerSpec.scala @@ -230,20 +230,20 @@ class TransactionsControllerSpec extends MyAPISpec { "return the latest transactions for the addresses" in { - val body = List(firstAddress.string, secondAddress.string, "3rdaddress") + val addresses = List(firstAddress.string, secondAddress.string, "3rdaddress") .map(x => s""" "$x" """) .mkString("[", ",", "]") + val body = s"""{ "addresses": $addresses }""" val expected = Json.obj(firstAddress.string -> firstTxId, secondAddress.string -> secondTxId) val response = POST(url, Some(body)) - status(response) mustEqual OK val json = contentAsJson(response) json mustEqual expected } "fail while passing no addresses" in { - val body = """[]""" + val body = """{ "addresses": [] }""" val response = POST(url, Some(body)) status(response) mustEqual BAD_REQUEST @@ -255,7 +255,7 @@ class TransactionsControllerSpec extends MyAPISpec { val error = errorList.head (error \ "type").as[String] mustEqual PublicErrorRenderer.FieldValidationErrorType - (error \ "field").as[String].nonEmpty mustEqual false + (error \ "field").as[String] mustEqual "addresses" (error \ "message").as[String].nonEmpty mustEqual true } }