Skip to content

Commit

Permalink
pass edition
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielCliftonGuardian committed Oct 18, 2023
1 parent 6d2fda0 commit 1dad4cb
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 5 deletions.
21 changes: 21 additions & 0 deletions article/app/controllers/ArticleController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ class ArticleController(
}
}

def renderAppsArticle(path: String, edition: String): Action[AnyContent] = {
Action.async { implicit request =>
mapAppModel(path, ArticleBlocks, edition) { (article, blocks) =>
render(path, article, blocks)
}
}
}

def renderEmail(path: String): Action[AnyContent] = {
Action.async { implicit request =>
mapModel(path, ArticleBlocks) { (article, blocks) =>
Expand Down Expand Up @@ -157,6 +165,19 @@ class ArticleController(
}
}

private def mapAppModel(path: String, range: BlockRange, appEdition: String)(
render: (ArticlePage, Blocks) => Future[Result],
)(implicit request: RequestHeader): Future[Result] = {
capiLookup
.lookupForApps(path, Some(range), appEdition)
.map(responseToModelOrResult)
.recover(convertApiExceptions)
.flatMap {
case Right((model, blocks)) => render(model, blocks)
case Left(other) => Future.successful(RenderOtherStatus(other))
}
}

private def responseToModelOrResult(
response: ItemResponse,
)(implicit request: RequestHeader): Either[Result, (ArticlePage, Blocks)] = {
Expand Down
2 changes: 2 additions & 0 deletions article/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@ GET /*path/email.emailtxt controllers.ArticleController.renderEmail(pa
# e.g. /theguardian/2015/nov/03/mainsection
GET /$publication<(theguardian|theobserver)>/$year<\d\d\d\d>/$month<\w\w\w>/$day<\d\d>/$tail<.+> controllers.PublicationController.publishedOn(publication, year, month, day, tail)

GET /apps/:edition/*path controllers.ArticleController.renderAppsArticle(path, edition: String)

GET /*path controllers.ArticleController.renderArticle(path)
7 changes: 5 additions & 2 deletions common/app/common/ModelOrResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ private object ItemOrRedirect extends ItemResponses with GuLogging {
): Either[Result, T] =
maybeSection match {
case Some(section) => redirectSection(item, request, section)
case None => redirectArticle(item, response, request)
// case None => redirectArticle(item, response, request)
case None => Right(item)
}

private def redirectArticle[T](item: T, response: ItemResponse, request: RequestHeader): Either[Result, T] = {
canonicalPath(response) match {
case Some(canonicalPath) if canonicalPath != request.pathWithoutModifiers && !request.isModified =>
// case Some(canonicalPath) if canonicalPath != request.pathWithoutModifiers && !request.isModified =>
case Some(canonicalPath)
if canonicalPath != request.pathWithAppsAndEdition && canonicalPath != request.pathWithoutModifiers && !request.isModified =>
Left(Found(canonicalPath + paramString(request)))
case _ => Right(item)
}
Expand Down
9 changes: 6 additions & 3 deletions common/app/implicits/Requests.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package implicits

import com.gu.facia.client.models.{
AUNewSouthWalesTerritory,
AUQueenslandTerritory,
AUVictoriaTerritory,
EU27Territory,
NZTerritory,
TargetedTerritory,
USEastCoastTerritory,
USWestCoastTerritory,
AUVictoriaTerritory,
AUQueenslandTerritory,
AUNewSouthWalesTerritory,
}
import conf.Configuration
import play.api.mvc.RequestHeader
Expand Down Expand Up @@ -88,6 +88,9 @@ trait Requests {
if (isEmail) r.path.stripSuffix(EMAIL_SUFFIX)
else r.path.stripSuffix("/all")

lazy val pathWithAppsAndEdition: String =
r.path.stripPrefix("/apps/uk")

lazy val hasParameters: Boolean = r.queryString.nonEmpty

lazy val isHealthcheck: Boolean =
Expand Down
22 changes: 22 additions & 0 deletions common/app/services/CAPILookup.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,29 @@ class CAPILookup(contentApiClient: ContentApiClient) {

def lookup(path: String, range: Option[BlockRange])(implicit request: RequestHeader): Future[ItemResponse] = {
val edition = Edition(request)
val capiItem = contentApiClient
.item(path, edition)
.showTags("all")
.showFields("all")
.showReferences("all")
.showAtoms("all")

val capiItemWithBlocks = range
.map { blockRange =>
val blocksParam = blockRange.query.map(_.mkString(",")).getOrElse("all")
capiItem.showBlocks(blocksParam)
}
.getOrElse(capiItem)

contentApiClient.getResponse(capiItemWithBlocks)

}

def lookupForApps(path: String, range: Option[BlockRange], appEdition: String)(implicit
request: RequestHeader,
): Future[ItemResponse] = {
val edition = Edition.byId(appEdition).getOrElse(Edition.defaultEdition)
println(edition)
val capiItem = contentApiClient
.item(path, edition)
.showTags("all")
Expand Down
1 change: 1 addition & 0 deletions dev-build/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ GET /$path<[^/]+/([^/]+/)?live/.*>

# articles, finished liveblogs

GET /apps/:edition/*path controllers.ArticleController.renderAppsArticle(path, edition: String)
GET /*path.json controllers.ArticleController.renderJson(path)
GET /*path/email controllers.ArticleController.renderEmail(path)
GET /*path/email/headline.txt controllers.ArticleController.renderHeadline(path)
Expand Down

0 comments on commit 1dad4cb

Please sign in to comment.