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
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)
}
}

8
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
}
}

Loading…
Cancel
Save