private def publishMoreTopics()

in src/main/scala/managehelpcontentpublisher/PathAndContent.scala [36:59]


  private def publishMoreTopics(
      publishingOps: PublishingOps
  )(oldArticle: Option[Article], newTopics: Seq[Topic]): Either[Failure, Option[PathAndContent]] = {

    def storeMoreTopics(prevMoreTopics: Option[MoreTopics], newContent: Option[String]) = for {
      moreTopics <- prevMoreTopics
      content <- newContent
    } yield publishingOps.storeTopic(PathAndContent(moreTopics.path, content))

    // No need to do anything if the new topics and the topics of the old article are all core topics
    def isCore(path: String) = config.topic.corePaths.contains(path)
    if (
      newTopics.forall(topic => isCore(topic.path)) &&
      oldArticle.forall(_.topics.forall(topic => isCore(topic.path)))
    ) Right(None)
    else
      for {
        jsonString <- publishingOps.fetchTopicByPath(config.topic.moreTopics.path)
        oldMoreTopics <- jsonString.map(readMoreTopics).sequence
        newMoreTopics = MoreTopics.withNewTopics(oldMoreTopics, oldArticle, newTopics)
        content <- newMoreTopics.map(writeMoreTopics).sequence
        result <- storeMoreTopics(newMoreTopics, content).sequence
      } yield result
  }