Skip to content

Commit

Permalink
route for DCR-JSON crossword data
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-nowak committed Jan 19, 2024
1 parent 0eb925c commit 6f9eed9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
22 changes: 21 additions & 1 deletion applications/app/controllers/CrosswordsController.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package controllers

import com.gu.contentapi.client.model.v1.{Crossword, ItemResponse, Content => ApiContent, Section => ApiSection}
import common.{Edition, ImplicitControllerExecutionContext, GuLogging}
import common.{Edition, GuLogging, ImplicitControllerExecutionContext}
import conf.Static
import contentapi.ContentApiClient
import pages.{CrosswordHtmlPage, IndexHtmlPage, PrintableCrosswordHtmlPage}
import crosswords.{
AccessibleCrosswordPage,
AccessibleCrosswordRows,
CrosswordPageWithContent,
CrosswordPageWithSvg,
CrosswordSearchPageNoResult,
CrosswordSearchPageWithResults,
Expand All @@ -21,6 +22,7 @@ import play.api.data._
import play.api.mvc.{Action, RequestHeader, Result, _}
import services.{IndexPage, IndexPageItem}
import html.HtmlPageHelpers.ContentCSSFile
import model.dotcomrendering.{DotcomRenderingDataModel, PageType}

import scala.concurrent.Future
import scala.concurrent.duration._
Expand Down Expand Up @@ -84,6 +86,24 @@ class CrosswordPageController(val contentApiClient: ContentApiClient, val contro
renderCrosswordPage(crosswordType, id)
}

def renderJson(crosswordType: String, id: Int): Action[AnyContent] = {
Action.async { implicit request =>
withCrossword(crosswordType, id) { (crossword, content) =>
val crosswordContent = CrosswordContent.make(CrosswordData.fromCrossword(crossword, content), content)
val crosswordPage = new CrosswordPageWithContent(crosswordContent)

val pageType = PageType(crosswordPage, request, context)
common.renderJson(getDCRJson(crosswordPage, pageType), crosswordPage).as("application/json")
}
}
}
private def getDCRJson(crosswordPage: CrosswordPageWithContent, pageType: PageType)(implicit
request: RequestHeader,
): String =
DotcomRenderingDataModel.toJson(
DotcomRenderingDataModel.forCrossword(crosswordPage, request, pageType, crosswordPage.crossword),
)

def accessibleCrossword(crosswordType: String, id: Int): Action[AnyContent] =
Action.async { implicit request =>
withCrossword(crosswordType, id) { (crossword, content) =>
Expand Down
1 change: 1 addition & 0 deletions applications/conf/routes
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ GET /survey/thankyou

# NOTE: Leave this as it is, otherwise we don't render /crosswords/series/prize, for example.
GET /crosswords/$crosswordType<cryptic|quick|quiptic|prize|everyman|azed|special|genius|speedy|weekend>/:id.svg controllers.CrosswordPageController.thumbnail(crosswordType: String, id: Int)
GET /crosswords/$crosswordType<cryptic|quick|quiptic|prize|everyman|azed|special|genius|speedy|weekend>/:id.json controllers.CrosswordPageController.renderJson(crosswordType: String, id: Int)
GET /crosswords/$crosswordType<cryptic|quick|quiptic|prize|everyman|azed|special|genius|speedy|weekend>/:id controllers.CrosswordPageController.crossword(crosswordType: String, id: Int)
GET /crosswords/$crosswordType<cryptic|quick|quiptic|prize|everyman|special|genius|speedy|weekend>/:id/print controllers.CrosswordPageController.printableCrossword(crosswordType: String, id: Int)
GET /crosswords/accessible/$crosswordType<cryptic|quick|quiptic|prize|everyman|azed|special|genius|speedy|weekend>/:id controllers.CrosswordPageController.accessibleCrossword(crosswordType: String, id: Int)
Expand Down
28 changes: 28 additions & 0 deletions common/app/model/dotcomrendering/DotcomRenderingDataModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import model.{
CanonicalLiveBlog,
ContentFormat,
ContentPage,
CrosswordData,
GUDateTimeFormatNew,
GalleryPage,
ImageContentPage,
Expand Down Expand Up @@ -108,6 +109,7 @@ case class DotcomRenderingDataModel(
showTableOfContents: Boolean,
lang: Option[String],
isRightToLeftLang: Boolean,
crossword: Option[CrosswordData],
)

object DotcomRenderingDataModel {
Expand Down Expand Up @@ -187,6 +189,7 @@ object DotcomRenderingDataModel {
"showTableOfContents" -> model.showTableOfContents,
"lang" -> model.lang,
"isRightToLeftLang" -> model.isRightToLeftLang,
"crossword" -> model.crossword,
)

ElementsEnhancer.enhanceDcrObject(obj)
Expand Down Expand Up @@ -338,6 +341,29 @@ object DotcomRenderingDataModel {
)
}

def forCrossword(
crosswordPage: ContentPage,
request: RequestHeader,
pageType: PageType,
crossword: CrosswordData, // TODO or move CrosswordPageWithContent to common so usable here
): DotcomRenderingDataModel = {
val linkedData = LinkedData.forArticle(
article = crosswordPage.item,
baseURL = Configuration.dotcom.baseUrl,
fallbackLogo = Configuration.images.fallbackLogo,
)

apply(
page = crosswordPage,
request = request,
pageType = pageType,
linkedData = linkedData,
mainBlock = None,
bodyBlocks = Seq.empty,
crossword = Some(crossword),
)
}

def keyEventsFallback(
blocks: APIBlocks,
): Seq[APIBlock] = {
Expand Down Expand Up @@ -442,6 +468,7 @@ object DotcomRenderingDataModel {
filterKeyEvents: Boolean = false,
mostRecentBlockId: Option[String] = None,
forceLive: Boolean = false,
crossword: Option[CrosswordData] = None,
): DotcomRenderingDataModel = {

val edition = Edition.edition(request)
Expand Down Expand Up @@ -635,6 +662,7 @@ object DotcomRenderingDataModel {
showTableOfContents = content.fields.showTableOfContents.getOrElse(false),
lang = content.fields.lang,
isRightToLeftLang = content.fields.isRightToLeftLang,
crossword = crossword,
)
}
}

0 comments on commit 6f9eed9

Please sign in to comment.