From 92d640706876a9e36a897c2dc39b65dc557482c4 Mon Sep 17 00:00:00 2001 From: Alexander Myltsev Date: Fri, 18 Nov 2016 13:03:22 +0300 Subject: [PATCH] ! #102: make pagination result global in output --- .../resolver/api/NamestringsProtocols.scala | 47 ++++++++++--------- .../globalnames/resolver/api/Service.scala | 8 ++-- 2 files changed, 30 insertions(+), 25 deletions(-) diff --git a/api/src/main/scala/org/globalnames/resolver/api/NamestringsProtocols.scala b/api/src/main/scala/org/globalnames/resolver/api/NamestringsProtocols.scala index ed31865..ea44881 100644 --- a/api/src/main/scala/org/globalnames/resolver/api/NamestringsProtocols.scala +++ b/api/src/main/scala/org/globalnames/resolver/api/NamestringsProtocols.scala @@ -24,8 +24,10 @@ trait NamestringsProtocols extends DefaultJsonProtocol { case class VernacularResponse(dataSourceId: Int, values: Seq[VernacularResponseItem]) - case class Response(page: Int, perPage: Int, total: Long, suppliedId: Option[SuppliedId], - suppliedNameString: Option[String], matches: Seq[ResponseItem]) + case class Responses(page: Int, perPage: Int, data: Seq[Response]) + + case class Response(total: Long, suppliedId: Option[SuppliedId], + suppliedNameString: Option[String], results: Seq[ResponseItem]) case class ResponseItem(nameStringUuid: UUID, nameString: String, canonicalNameUuid: Option[UUID], canonicalName: Option[String], @@ -37,32 +39,35 @@ trait NamestringsProtocols extends DefaultJsonProtocol { vernaculars: Seq[VernacularResponse], matchType: MatchType, localId: Option[String]) - def result(matches: Matches, page: Int, perPage: Int): Response = { - val items = matches.matches.map { m => - val vernaculars = m.vernacularStrings.groupBy { _._2.dataSourceId }.map { case (dsi, xs) => - val vris = xs.map { case (vs, vsi) => - VernacularResponseItem(vs.name, vsi.language, vsi.locality, vsi.countryCode) } - VernacularResponse(dsi, vris) - }.toSeq + def result(matchesCollection: Seq[Matches], page: Int, perPage: Int): Responses = { + val responses = matchesCollection.map { matches => + val items = matches.matches.map { m => + val vernaculars = m.vernacularStrings.groupBy { _._2.dataSourceId }.map { case (dsi, xs) => + val vris = xs.map { case (vs, vsi) => + VernacularResponseItem(vs.name, vsi.language, vsi.locality, vsi.countryCode) + } + VernacularResponse(dsi, vris) + }.toSeq - ResponseItem(m.nameString.name.id, m.nameString.name.value, - m.nameString.canonicalName.map { _.id }, m.nameString.canonicalName.map { _.value }, - m.nameString.surrogate, - m.dataSource.id, m.dataSource.title, - m.nameStringIndex.taxonId, m.nameStringIndex.globalId, - m.nameStringIndex.classificationPath, m.nameStringIndex.classificationPathIds, - m.nameStringIndex.classificationPathRanks, - vernaculars, - m.matchType, - m.nameStringIndex.localId) + ResponseItem(m.nameString.name.id, m.nameString.name.value, + m.nameString.canonicalName.map { _.id }, m.nameString.canonicalName.map { _.value }, + m.nameString.surrogate, + m.dataSource.id, m.dataSource.title, + m.nameStringIndex.taxonId, m.nameStringIndex.globalId, + m.nameStringIndex.classificationPath, m.nameStringIndex.classificationPathIds, + m.nameStringIndex.classificationPathRanks, + vernaculars, m.matchType, m.nameStringIndex.localId) + } + Response(matches.total, matches.suppliedId, matches.suppliedNameString, items) } - Response(page, perPage, matches.total, matches.suppliedId, matches.suppliedNameString, items) + Responses(page, perPage, responses) } implicit val vernacularResponseItemFormat = jsonFormat4(VernacularResponseItem.apply) implicit val vernacularResponseFormat = jsonFormat2(VernacularResponse.apply) implicit val responseItemFormat = jsonFormat15(ResponseItem.apply) - implicit val responseFormat = jsonFormat6(Response.apply) + implicit val responseFormat = jsonFormat4(Response.apply) + implicit val responsesFormat = jsonFormat3(Responses.apply) implicit object UuidJsonFormat extends RootJsonFormat[UUID] { def write(x: UUID): JsString = JsString(x.toString) def read(value: JsValue): UUID = value match { diff --git a/api/src/main/scala/org/globalnames/resolver/api/Service.scala b/api/src/main/scala/org/globalnames/resolver/api/Service.scala index a0b36a8..3b3f380 100644 --- a/api/src/main/scala/org/globalnames/resolver/api/Service.scala +++ b/api/src/main/scala/org/globalnames/resolver/api/Service.scala @@ -64,7 +64,7 @@ trait Service extends NamestringsProtocols with CrossMapProtocols { val params = Parameters(page, perPage, withSurrogates, withVernaculars) val matches = resolver.resolveExact(names.take(nameStringsMaxCount), dataSourceIds.orZero, params) - matches.map { ms => ms.map { m => result(m, page, perPage) } } + matches.map { ms => result(ms, page, perPage) } } } } ~ path("name_strings" / JavaUUID) { uuid => @@ -72,13 +72,13 @@ trait Service extends NamestringsProtocols with CrossMapProtocols { (page, perPage, vernaculars) => complete { val params = Parameters(page, perPage, withSurrogates = false, vernaculars) facetedSearcher.findNameStringByUuid(uuid, params).map { m => - result(m, page, perPage) + result(Seq(m), page, perPage) } } } } ~ path("name_strings" / Remaining) { remaining => complete { - result(Matches.empty(remaining), 0, 0) + result(Seq(Matches.empty(remaining)), 0, 0) } } ~ path("name_strings") { (get & parameters('search_term, 'per_page ? nameStringsMaxCount, 'page ? 0, @@ -88,7 +88,7 @@ trait Service extends NamestringsProtocols with CrossMapProtocols { logger.debug(s"$search") val params = Parameters(page, perPage, withSurrogates, withVernaculars, query = searchTerm.some) - resolve(search, params).map { m => result(m, page, perPage) } + resolve(search, params).map { m => result(Seq(m), page, perPage) } } } } ~ path("names_strings" / JavaUUID / "dataSources") { uuid =>