From 88eb8a0b4017f700ab058eed410e7ed7a3296f5c Mon Sep 17 00:00:00 2001 From: Anna Beddow Date: Thu, 12 Dec 2024 09:57:43 +0000 Subject: [PATCH] Add gallery count to PressedCard --- common/app/model/PressedCard.scala | 15 +++++++++++++++ common/test/common/TrailsToShowcaseTest.scala | 1 + common/test/common/facia/FixtureBuilder.scala | 1 + 3 files changed, 17 insertions(+) diff --git a/common/app/model/PressedCard.scala b/common/app/model/PressedCard.scala index 15a3f214d3a0..8b03acabd63a 100644 --- a/common/app/model/PressedCard.scala +++ b/common/app/model/PressedCard.scala @@ -7,6 +7,7 @@ import com.gu.facia.api.utils.FaciaContentUtils.fold import com.gu.facia.api.{models => fapi, utils => fapiutils} import model.CardStylePicker import org.joda.time.DateTime +import com.gu.contentapi.client.model.v1.{Content, ElementType} final case class PressedCard( id: String, @@ -20,6 +21,7 @@ final case class PressedCard( shortUrl: String, group: String, isLive: Boolean, + galleryCount: Option[Int], ) { def withoutTrailText: PressedCard = copy(trailText = None) } @@ -36,6 +38,18 @@ object PressedCard { ).map(_.toJodaDateTime) } + def extractGalleryCount(fc: FaciaContent): Option[Int] = { + def countImagesInGallery(content: Content): Option[Int] = + content.elements.map(_.count(el => el.`type` == ElementType.Image && el.relation == "gallery")).filter(_ > 0) + + fold(fc)( + curatedContent => countImagesInGallery(curatedContent.content), + supportingCuratedContent => countImagesInGallery(supportingCuratedContent.content), + _ => None, + latestSnap => latestSnap.latestContent.flatMap(countImagesInGallery), + ) + } + PressedCard( id = FaciaContentUtils.id(content), cardStyle = CardStyle.make(CardStylePicker(content)), @@ -48,6 +62,7 @@ object PressedCard { group = FaciaContentUtils.group(content), trailText = FaciaContentUtils.trailText(content), starRating = FaciaContentUtils.starRating(content), + galleryCount = extractGalleryCount(content), ) } } diff --git a/common/test/common/TrailsToShowcaseTest.scala b/common/test/common/TrailsToShowcaseTest.scala index 889115e853a3..48c28867d62a 100644 --- a/common/test/common/TrailsToShowcaseTest.scala +++ b/common/test/common/TrailsToShowcaseTest.scala @@ -1394,6 +1394,7 @@ class TrailsToShowcaseTest extends AnyFlatSpec with Matchers with EitherValues { shortUrlPath = None, isLive = true, group = "", + galleryCount = None, ) val discussionSettings = PressedDiscussionSettings( diff --git a/common/test/common/facia/FixtureBuilder.scala b/common/test/common/facia/FixtureBuilder.scala index 604c7a1ce146..3b5d52cd7693 100644 --- a/common/test/common/facia/FixtureBuilder.scala +++ b/common/test/common/facia/FixtureBuilder.scala @@ -99,6 +99,7 @@ object FixtureBuilder { shortUrl = "", group = "0", isLive = false, + galleryCount = None, ) def mkDiscussion(): PressedDiscussionSettings =