Browse Source

server: Update the endpoint for retrieving latest transactions

prometheus-integration
Alexis Hernandez 6 years ago
parent
commit
1ec1629a19
  1. 13
      server/app/com/xsn/explorer/models/request/GetLatestTransactionRequest.scala
  2. 12
      server/app/controllers/TransactionsController.scala
  3. 8
      server/test/controllers/TransactionsControllerSpec.scala

13
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]
}

12
server/app/controllers/TransactionsController.scala

@ -1,19 +1,17 @@
package controllers package controllers
import javax.inject.Inject import javax.inject.Inject
import com.alexitc.playsonify.models.PublicContextWithModel import com.alexitc.playsonify.models.PublicContextWithModel
import com.xsn.explorer.models.Address import com.xsn.explorer.models.request.{GetLatestTransactionRequest, SendRawTransactionRequest}
import com.xsn.explorer.models.request.SendRawTransactionRequest
import com.xsn.explorer.services.TransactionService import com.xsn.explorer.services.TransactionService
import controllers.common.{Codecs,MyJsonController, MyJsonControllerComponents} import controllers.common.{MyJsonController, MyJsonControllerComponents}
import org.scalactic.Every
class TransactionsController @Inject() ( class TransactionsController @Inject() (
transactionService: TransactionService, transactionService: TransactionService,
cc: MyJsonControllerComponents) cc: MyJsonControllerComponents)
extends MyJsonController(cc) { extends MyJsonController(cc) {
import Codecs._
def getTransaction(txid: String) = publicNoInput { _ => def getTransaction(txid: String) = publicNoInput { _ =>
transactionService.getTransactionDetails(txid) transactionService.getTransactionDetails(txid)
} }
@ -26,7 +24,7 @@ class TransactionsController @Inject() (
transactionService.sendRawTransaction(ctx.model.hex) transactionService.sendRawTransaction(ctx.model.hex)
} }
def getLatestByAddresses() = publicWithInput { ctx: PublicContextWithModel[Every[Address]] => def getLatestByAddresses() = publicWithInput { ctx: PublicContextWithModel[GetLatestTransactionRequest] =>
transactionService.getLatestTransactionBy(ctx.model) transactionService.getLatestTransactionBy(ctx.model.addresses)
} }
} }

8
server/test/controllers/TransactionsControllerSpec.scala

@ -230,20 +230,20 @@ class TransactionsControllerSpec extends MyAPISpec {
"return the latest transactions for the addresses" in { "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" """) .map(x => s""" "$x" """)
.mkString("[", ",", "]") .mkString("[", ",", "]")
val body = s"""{ "addresses": $addresses }"""
val expected = Json.obj(firstAddress.string -> firstTxId, secondAddress.string -> secondTxId) val expected = Json.obj(firstAddress.string -> firstTxId, secondAddress.string -> secondTxId)
val response = POST(url, Some(body)) val response = POST(url, Some(body))
status(response) mustEqual OK status(response) mustEqual OK
val json = contentAsJson(response) val json = contentAsJson(response)
json mustEqual expected json mustEqual expected
} }
"fail while passing no addresses" in { "fail while passing no addresses" in {
val body = """[]""" val body = """{ "addresses": [] }"""
val response = POST(url, Some(body)) val response = POST(url, Some(body))
status(response) mustEqual BAD_REQUEST status(response) mustEqual BAD_REQUEST
@ -255,7 +255,7 @@ class TransactionsControllerSpec extends MyAPISpec {
val error = errorList.head val error = errorList.head
(error \ "type").as[String] mustEqual PublicErrorRenderer.FieldValidationErrorType (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 (error \ "message").as[String].nonEmpty mustEqual true
} }
} }

Loading…
Cancel
Save