From 444b7d37b38e5bf24a68e5f821ae1a3d6581e02b Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sat, 14 Apr 2018 00:13:01 -0500 Subject: [PATCH] server: Allow total and circulating supply to be missing --- .../anorm/parsers/StatisticsParsers.scala | 2 +- .../com/xsn/explorer/models/Statistics.scala | 4 ++-- .../explorer/models/StatisticsDetails.scala | 22 ++++++++++--------- 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/server/app/com/xsn/explorer/data/anorm/parsers/StatisticsParsers.scala b/server/app/com/xsn/explorer/data/anorm/parsers/StatisticsParsers.scala index e00de5e..007cda4 100644 --- a/server/app/com/xsn/explorer/data/anorm/parsers/StatisticsParsers.scala +++ b/server/app/com/xsn/explorer/data/anorm/parsers/StatisticsParsers.scala @@ -11,7 +11,7 @@ object StatisticsParsers { val parseTotalSupply = get[BigDecimal]("total_supply") val parseCirculatingSupply = get[BigDecimal]("circulating_supply") - val parseStatistics = (parseBlocks ~ parseTransactions ~ parseTotalSupply ~ parseCirculatingSupply).map { + val parseStatistics = (parseBlocks ~ parseTransactions ~ parseTotalSupply.? ~ parseCirculatingSupply.?).map { case blocks ~ transactions ~ totalSupply ~ circulatingSupply => Statistics(blocks, transactions, totalSupply, circulatingSupply) } diff --git a/server/app/com/xsn/explorer/models/Statistics.scala b/server/app/com/xsn/explorer/models/Statistics.scala index 346a7d4..6647351 100644 --- a/server/app/com/xsn/explorer/models/Statistics.scala +++ b/server/app/com/xsn/explorer/models/Statistics.scala @@ -5,8 +5,8 @@ import play.api.libs.json.{Json, Writes} case class Statistics( blocks: Int, transactions: Int, - totalSupply: BigDecimal, - circulatingSupply: BigDecimal) + totalSupply: Option[BigDecimal], + circulatingSupply: Option[BigDecimal]) object Statistics { diff --git a/server/app/com/xsn/explorer/models/StatisticsDetails.scala b/server/app/com/xsn/explorer/models/StatisticsDetails.scala index b3a8f44..d69b085 100644 --- a/server/app/com/xsn/explorer/models/StatisticsDetails.scala +++ b/server/app/com/xsn/explorer/models/StatisticsDetails.scala @@ -9,17 +9,19 @@ object StatisticsDetails { implicit val writes: Writes[StatisticsDetails] = Writes { obj => val values = Map( "blocks" -> JsNumber(obj.statistics.blocks), - "transactions" -> JsNumber(obj.statistics.transactions), - "totalSupply" -> JsNumber(obj.statistics.totalSupply), - "circulatingSupply" -> JsNumber(obj.statistics.circulatingSupply)) + "transactions" -> JsNumber(obj.statistics.transactions)) - val result = obj.masternodes - .map { count => - values + ("masternodes" -> JsNumber(count)) - } - .getOrElse { - values - } + val extras = List( + "totalSupply" -> obj.statistics.totalSupply.map(JsNumber.apply), + "circulatingSupply" -> obj.statistics.circulatingSupply.map(JsNumber.apply), + "masternodes" -> obj.masternodes.map(c => JsNumber.apply(c)) + ).flatMap { case (key, maybe) => + maybe.map(key -> _) + } + + val result = extras.foldLeft(values) { case (acc, value) => + acc + value + } JsObject.apply(result) }