def backfill()

in fapi-client/src/main/scala/com/gu/facia/api/utils/Backfill.scala [30:61]


  def backfill(resolver: BackfillResolver, adjustSearchQuery: AdjustSearchQuery = identity, adjustItemQuery: AdjustItemQuery = identity)
              (implicit capiClient: ContentApiClient, faciaClient: ApiClient, ec: ExecutionContext): Response[List[FaciaContent]] = {
    resolver match {
      case CapiBackfill(query, collectionConfig) =>
        val capiQuery = ContentApi.buildBackfillQuery(query)
          .map(adjustSearchQuery)
          .left.map(adjustItemQuery)
        val backfillResponse = ContentApi.getBackfillResponse(capiClient, capiQuery)
        for {
          backfillContent <- ContentApi.backfillContentFromResponse(backfillResponse)
        } yield {
          backfillContent.map(CuratedContent.fromTrailAndContent(_, TrailMetaData.empty, None, collectionConfig))
        }
      case CollectionBackfill(parentCollectionId) =>
        val collectionBackfillResult =
          for {
            parentCollection <- FAPI.getCollection(parentCollectionId)
            curatedCollection <- FAPI.liveCollectionContentWithSnaps(parentCollection, adjustSearchQuery, adjustItemQuery)
            nestedBackfill <- parentCollection.collectionConfig.backfill match {
              case Some(Backfill("capi", query)) =>
                backfill(CapiBackfill(query, parentCollection.collectionConfig), adjustSearchQuery, adjustItemQuery)
              case _ => backfill(EmptyBackfill)
            }
          } yield {
            (curatedCollection ++ nestedBackfill).distinct
          }

          collectionBackfillResult recover {
            err => List()
          }

      case EmptyBackfill => Response.Right(Nil)}}