Skip to content

Commit

Permalink
! #102: make pagination result global in output
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-myltsev committed Nov 18, 2016
1 parent 3f21961 commit 92d6407
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions api/src/main/scala/org/globalnames/resolver/api/Service.scala
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,21 @@ 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 =>
(get & parameters('page ? 0, 'per_page ? 50, 'vernaculars ? false)) {
(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,
Expand All @@ -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 =>
Expand Down

0 comments on commit 92d6407

Please sign in to comment.