Skip to content

Commit

Permalink
Merge pull request #423 from guardian/ag/channel-namespace-search
Browse files Browse the repository at this point in the history
Add "withChannel" parameter for search and get
  • Loading branch information
fredex42 authored Jun 13, 2024
2 parents e75598d + 5c27188 commit c35558e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ trait ContentApiQueries {
val editions = EditionsQuery()
val atoms = AtomsQuery()
def atomUsage(atomType: AtomType, atomId: String) = AtomUsageQuery(atomType, atomId)
@deprecated("Recipe atoms no longer exist and should not be relied upon. No data will be returned and this query will be removed in a future iteration of the library")
val recipes = RecipesQuery()
val reviews = ReviewsQuery()
val gameReviews = GameReviewsQuery()
Expand Down
38 changes: 30 additions & 8 deletions client/src/main/scala/com.gu.contentapi.client/model/Queries.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ trait SearchQueryBase[Self <: SearchQueryBase[Self]]
this: Self =>
}

case class ItemQuery(id: String, parameterHolder: Map[String, Parameter] = Map.empty)
case class ItemQuery(id: String, parameterHolder: Map[String, Parameter] = Map.empty, channelId: Option[String]=None)
extends ContentApiQuery[ItemResponse]
with EditionParameters[ItemQuery]
with ShowParameters[ItemQuery]
Expand All @@ -76,23 +76,43 @@ case class ItemQuery(id: String, parameterHolder: Map[String, Parameter] = Map.e
with FilterExtendedParameters[ItemQuery]
with FilterSearchParameters[ItemQuery] {

def withParameters(parameterMap: Map[String, Parameter]) = copy(id, parameterMap)
def withParameters(parameterMap: Map[String, Parameter]) = copy(id, parameterMap, channelId)

def withChannelId(newChannel:String) = copy(id, parameterHolder, Some(newChannel))

def withoutChannelId() = copy(id, parameterHolder, None)

def itemId(contentId: String): ItemQuery =
copy(id = contentId)

override def pathSegment: String = id
override def pathSegment: String = channelId match {
case None => id
case Some(chl) => s"channel/$chl/item/$id"
}
}

case class SearchQuery(parameterHolder: Map[String, Parameter] = Map.empty)
case class SearchQuery(parameterHolder: Map[String, Parameter] = Map.empty, channelId: Option[String] = None)
extends PaginatedApiQuery[SearchResponse, Content] with SearchQueryBase[SearchQuery] {

def setPaginationConsistentWith(response: SearchResponse): PaginatedApiQuery[SearchResponse, Content] =
pageSize.setIfUndefined(response.pageSize).orderBy.setIfUndefined(response.orderBy)

def withParameters(parameterMap: Map[String, Parameter]): SearchQuery = copy(parameterMap)
def withParameters(parameterMap: Map[String, Parameter]): SearchQuery = copy(parameterMap, channelId)

/**
* Make this search on a CAPI channel rather than against web-only content
* For more information about channels, and the reason why your app should only be in one channel,
* contact the Content API team
* @param channelId the channel to search against, or "all" to search across all channels.
*/
def withChannel(channelId:String):SearchQuery = copy(parameterHolder, Some(channelId))

override def pathSegment: String = "search"
def withoutChannel(): SearchQuery = copy(parameterHolder, None)

override def pathSegment: String = channelId match {
case None=>"search"
case Some(chnl)=>s"channel/$chnl/search"
}

protected override def followingQueryGivenFull(response: SearchResponse, direction: Direction) = for {
lastResultInResponse <- response.results.lastOption
Expand Down Expand Up @@ -174,10 +194,11 @@ case class AtomUsageQuery(atomType: AtomType, atomId: String, parameterHolder: M
override def pathSegment: String = s"atom/${atomType.toString.toLowerCase}/$atomId/usage"
}

@deprecated("Recipe atoms no longer exist and should not be relied upon. No data will be returned and this class will be removed in a future iteration of the library")
case class RecipesQuery(parameterHolder: Map[String, Parameter] = Map.empty)
extends ContentApiQuery[AtomsResponse]
with PaginationParameters[RecipesQuery]
with RecipeParameters[RecipesQuery] {
with PaginationParameters[RecipesQuery]
with RecipeParameters[RecipesQuery] {

def withParameters(parameterMap: Map[String, Parameter]) = copy(parameterMap)

Expand Down Expand Up @@ -284,6 +305,7 @@ trait ShowParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { thi
def showStats = BoolParameter("show-stats")
def showAliasPaths = BoolParameter("show-alias-paths")
def showSchemaOrg = BoolParameter("show-schemaorg")
def showChannels = StringParameter("show-channels")
}

trait ShowReferencesParameters[Owner <: Parameters[Owner]] extends Parameters[Owner] { this: Owner =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class ContentApiQueryTest extends AnyFlatSpec with Matchers {
"/profile/justin-pinner?show-alias-paths=true"
}

"ItemQuery" should "accept a channel" in {
ItemQuery("lifeandstyle/thing").withChannelId("recipes").getUrl("") shouldEqual
"/channel/recipes/item/lifeandstyle/thing"
}

"ItemQuery" should "drop the included channel if asked" in {
ItemQuery("lifeandstyle/thing").withChannelId("recipes").withoutChannelId().getUrl("") shouldEqual
"/lifeandstyle/thing"
}

"SearchQuery" should "also be excellent" in {
SearchQuery().tag("profile/robert-berry").showElements("all").contentType("article").queryFields("body").getUrl("") shouldEqual
"/search?tag=profile%2Frobert-berry&show-elements=all&type=article&query-fields=body"
Expand All @@ -29,6 +39,16 @@ class ContentApiQueryTest extends AnyFlatSpec with Matchers {
"/search?paths=path%2Fone%2Cpath%2Ftwo"
}

"SearchQuery" should "include a channel if asked" in {
SearchQuery().withChannel("my-channel").paths("path/one").getUrl("") shouldEqual
"/channel/my-channel/search?paths=path%2Fone"
}

"SearchQuery" should "drop the included channel if asked" in {
SearchQuery().withChannel("my-channel").withoutChannel().paths("path/one").getUrl("") shouldEqual
"/search?paths=path%2Fone"
}

"SectionsQuery" should "be beautiful" in {
SectionsQuery().getUrl("") shouldEqual "/sections"
}
Expand Down

0 comments on commit c35558e

Please sign in to comment.