Skip to content

Commit

Permalink
Allow specifying direction to get next search query
Browse files Browse the repository at this point in the history
We're trying to support LightBox functionality that was introduced here: guardian/frontend#20462

Co-authored-by: Roberto Tyley <[email protected]>
  • Loading branch information
ioannakok and rtyley committed Jun 8, 2022
1 parent cbf80c3 commit 0d95dc0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions client/src/main/scala/com.gu.contentapi.client/model/Queries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ case class SearchQuery(parameterHolder: Map[String, Parameter] = Map.empty)

protected override def nextQueryGivenFull(response: SearchResponse) = for {
nextId <- PaginatedApiResponse.searchResponse.getNextId(response)
} yield NextSearchQuery(this, nextId)
} yield NextSearchQuery(this, nextId, Next)

}

Expand Down Expand Up @@ -214,16 +214,27 @@ case class FilmReviewsQuery(parameterHolder: Map[String, Parameter] = Map.empty)
override def pathSegment: String = "atoms/reviews/film"
}

case class NextSearchQuery[Q <: PaginatedApiQuery[SearchResponse, Content]](originalQuery: Q, contentId: String)
extends PaginatedApiQuery[SearchResponse, Content] {
sealed trait Direction {
val pathSegment: String
}
object Next extends Direction {
override val pathSegment: String = "next"
}
object Previous extends Direction {
override val pathSegment: String = "prev"
}

case class NextSearchQuery[Q <: PaginatedApiQuery[SearchResponse, Content]](
originalQuery: Q, contentId: String, direction: Direction
) extends PaginatedApiQuery[SearchResponse, Content] {

def parameters: Map[String, String] = originalQuery.parameters.filterKeys(not(isPaginationParameter)).toMap

override def pathSegment: String = s"""content/${contentId}/next"""
override def pathSegment: String = s"content/$contentId/${direction.pathSegment}"

protected override def nextQueryGivenFull(response: SearchResponse): Option[PaginatedApiQuery[SearchResponse, Content]] = for {
nextId <- PaginatedApiResponse.searchResponse.getNextId(response)
} yield NextSearchQuery(originalQuery, nextId)
} yield copy(contentId = nextId)

override def normalised: PaginatedApiQuery[SearchResponse, Content] = originalQuery.normalised
}
Expand Down

0 comments on commit 0d95dc0

Please sign in to comment.