diff --git a/article-api/src/main/scala/no/ndla/articleapi/service/ReadService.scala b/article-api/src/main/scala/no/ndla/articleapi/service/ReadService.scala index 851266dbdc..9b8330bc43 100644 --- a/article-api/src/main/scala/no/ndla/articleapi/service/ReadService.scala +++ b/article-api/src/main/scala/no/ndla/articleapi/service/ReadService.scala @@ -38,15 +38,8 @@ import scala.math.max import scala.util.{Failure, Success, Try} trait ReadService { - this: ArticleRepository - with FeideApiClient - with ConverterService - with ArticleSearchService - with SearchConverterService - with MemoizeHelpers - with Props - with ErrorHelpers - with FrontpageApiClient => + this: ArticleRepository & FeideApiClient & ConverterService & ArticleSearchService & SearchConverterService & + MemoizeHelpers & Props & ErrorHelpers & FrontpageApiClient => val readService: ReadService class ReadService extends StrictLogging { @@ -153,9 +146,8 @@ trait ReadService { private def addUrlOnEmbedTag(embedTag: Element): Unit = { val typeAndPathOption = embedTag.attr(TagAttribute.DataResource.toString) match { case resourceType - if resourceType == ResourceType.File.toString - || resourceType == ResourceType.H5P.toString - && embedTag.hasAttr(TagAttribute.DataPath.toString) => + if resourceType == ResourceType.File.toString || resourceType == ResourceType.H5P.toString && embedTag + .hasAttr(TagAttribute.DataPath.toString) => val path = embedTag.attr(TagAttribute.DataPath.toString) Some((resourceType, path)) @@ -278,9 +270,10 @@ trait ReadService { ): Try[Seq[api.ArticleV2]] = { if (articleIds.isEmpty) Failure(ValidationException("ids", "Query parameter 'ids' is missing")) else { - val offset = (page - 1) * pageSize - val domainArticles = articleRepository.withIds(articleIds, offset, pageSize).toArticles - val isFeideNeeded = domainArticles.exists(article => article.availability == Availability.teacher) + val offset = (page - 1) * pageSize + val domainArticles = + articleRepository.withIds(articleIds, offset, pageSize).toArticles.map(addUrlsOnEmbedResources) + val isFeideNeeded = domainArticles.exists(article => article.availability == Availability.teacher) val filtered = if (isFeideNeeded) applyAvailabilityFilter(feideAccessToken, domainArticles) else domainArticles filtered.traverse(article => converterService.toApiArticleV2(article, language, fallback)) } @@ -307,7 +300,7 @@ trait ReadService { articles.map(Cachable.merge) } - def toArticleItem(article: api.ArticleV2): String = { + private def toArticleItem(article: api.ArticleV2): String = { s""" | ${article.title.title} | ${article.metaDescription.metaDescription} @@ -322,7 +315,7 @@ trait ReadService { .getOrElse(s"${props.ndlaFrontendUrl}/article/$id") private val allBlankLinesRegex = """(?m)^\s*$[\r\n]*""".r - def toRSSXML(parentArticle: api.ArticleV2, articles: List[api.ArticleV2]): String = { + private def toRSSXML(parentArticle: api.ArticleV2, articles: List[api.ArticleV2]): String = { val rss = s""" | | diff --git a/draft-api/src/main/scala/no/ndla/draftapi/service/ReadService.scala b/draft-api/src/main/scala/no/ndla/draftapi/service/ReadService.scala index 1190328a80..a3f2f90a6d 100644 --- a/draft-api/src/main/scala/no/ndla/draftapi/service/ReadService.scala +++ b/draft-api/src/main/scala/no/ndla/draftapi/service/ReadService.scala @@ -7,7 +7,7 @@ package no.ndla.draftapi.service -import cats.implicits._ +import cats.implicits.* import io.lemonlabs.uri.{Path, Url} import no.ndla.common.configuration.Constants.EmbedTagName import no.ndla.common.errors.ValidationException @@ -24,28 +24,20 @@ import no.ndla.draftapi.service.search.{ SearchConverterService, TagSearchService } -import no.ndla.validation._ +import no.ndla.validation.* import org.jsoup.nodes.Element import scalikejdbc.ReadOnlyAutoSession -import scala.jdk.CollectionConverters._ +import scala.jdk.CollectionConverters.* import scala.math.max import scala.util.{Failure, Success, Try} trait ReadService { - this: DraftRepository - with ConverterService - with ArticleSearchService - with TagSearchService - with GrepCodesSearchService - with SearchConverterService - with UserDataRepository - with WriteService - with Props - with MemoizeHelpers => + this: DraftRepository & ConverterService & ArticleSearchService & TagSearchService & GrepCodesSearchService & + SearchConverterService & UserDataRepository & WriteService & Props & MemoizeHelpers => val readService: ReadService - import props._ + import props.* class ReadService { @@ -60,7 +52,7 @@ trait ReadService { } def getArticleBySlug(slug: String, language: String, fallback: Boolean = false): Try[api.Article] = { - draftRepository.withSlug(slug)(ReadOnlyAutoSession) match { + draftRepository.withSlug(slug)(ReadOnlyAutoSession).map(addUrlsOnEmbedResources) match { case None => Failure(NotFoundException(s"The article with slug '$slug' was not found")) case Some(article) => converterService.toApiArticle(article, language, fallback) } @@ -131,9 +123,8 @@ trait ReadService { private def addUrlOnEmbedTag(embedTag: Element): Unit = { val typeAndPathOption = embedTag.attr(TagAttribute.DataResource.toString) match { case resourceType - if resourceType == ResourceType.File.toString - || resourceType == ResourceType.H5P.toString - && embedTag.hasAttr(TagAttribute.DataPath.toString) => + if resourceType == ResourceType.File.toString || resourceType == ResourceType.H5P.toString && embedTag + .hasAttr(TagAttribute.DataPath.toString) => val path = embedTag.attr(TagAttribute.DataPath.toString) Some((resourceType, path)) @@ -185,7 +176,9 @@ trait ReadService { if (articleIds.isEmpty) Failure(ValidationException("ids", "Query parameter 'ids' is missing")) else Success(articleIds) domainArticles <- draftRepository.withIds(ids, offset, pageSize) - api <- domainArticles.traverse(article => converterService.toApiArticle(article, language, fallback)) + api <- domainArticles.traverse(article => + converterService.toApiArticle(addUrlsOnEmbedResources(article), language, fallback) + ) } yield api } }