diff --git a/article/app/controllers/ArticleController.scala b/article/app/controllers/ArticleController.scala index 0784e7fa8ec..01d09fb095f 100644 --- a/article/app/controllers/ArticleController.scala +++ b/article/app/controllers/ArticleController.scala @@ -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) => @@ -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)] = { diff --git a/article/conf/routes b/article/conf/routes index 72258ac6799..32b8559595b 100644 --- a/article/conf/routes +++ b/article/conf/routes @@ -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) diff --git a/common/app/common/ModelOrResult.scala b/common/app/common/ModelOrResult.scala index ba52d7f18f8..42ff823f115 100644 --- a/common/app/common/ModelOrResult.scala +++ b/common/app/common/ModelOrResult.scala @@ -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) } diff --git a/common/app/implicits/Requests.scala b/common/app/implicits/Requests.scala index 897a450da5e..b21ca1e9c27 100644 --- a/common/app/implicits/Requests.scala +++ b/common/app/implicits/Requests.scala @@ -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 @@ -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 = diff --git a/common/app/services/CAPILookup.scala b/common/app/services/CAPILookup.scala index 54c11c9f0a5..728b90d3ea3 100644 --- a/common/app/services/CAPILookup.scala +++ b/common/app/services/CAPILookup.scala @@ -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") diff --git a/dev-build/conf/routes b/dev-build/conf/routes index eaedbc8234e..9a0b8976ec3 100644 --- a/dev-build/conf/routes +++ b/dev-build/conf/routes @@ -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)