Skip to content

Commit

Permalink
Merge pull request #551 from NDLANO/add-introduction-for-learningstep
Browse files Browse the repository at this point in the history
Adds introduction to learningstep.
  • Loading branch information
gunnarvelle authored Dec 4, 2024
2 parents 3f70da3 + 6aea015 commit dba51e3
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Part of NDLA common
* Copyright (C) 2024 NDLA
*
* See LICENSE
*
*/

package no.ndla.common.model.domain.learningpath

import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder}
import io.circe.{Decoder, Encoder}
import no.ndla.language.model.LanguageField

case class Introduction(introduction: String, language: String) extends LanguageField[String] {
override def value: String = introduction
override def isEmpty: Boolean = introduction.isEmpty
}

object Introduction {
implicit val encoder: Encoder[Introduction] = deriveEncoder
implicit val decoder: Decoder[Introduction] = deriveDecoder
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ case class LearningStep(
learningPathId: Option[Long],
seqNo: Int,
title: Seq[Title],
introduction: Seq[Introduction],
description: Seq[Description],
embedUrl: Seq[EmbedUrl],
`type`: StepType,
Expand Down
2 changes: 1 addition & 1 deletion language/src/main/scala/no/ndla/language/Language.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object Language {
"und"
)

def mergeLanguageFields[A <: LanguageField[_]](existing: Seq[A], updated: Seq[A]): Seq[A] = {
def mergeLanguageFields[A <: LanguageField[?]](existing: Seq[A], updated: Seq[A]): Seq[A] = {
val toKeep = existing.filterNot(item => updated.map(_.language).contains(item.language))
(toKeep ++ updated).filterNot(_.isEmpty)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ case class LearningStepV2(
@description("The revision number for this learningstep") revision: Int,
@description("The sequence number for the step. The first step has seqNo 0.") seqNo: Int,
@description("The title of the learningstep") title: Title,
@description("The introduction of the learningstep") introduction: Option[Introduction],
@description("The description of the learningstep") description: Option[Description],
@description("The embed content for the learningstep") embedUrl: Option[EmbedUrlV2],
@description("Determines if the title of the step should be displayed in viewmode") showTitle: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import sttp.tapir.Schema.annotations.description
@description("Information about a new learningstep")
case class NewLearningStepV2(
@description("The titles of the learningstep") title: String,
@description("The introduction of the learningstep") introduction: Option[String],
@description("The descriptions of the learningstep") description: Option[String],
@description("The chosen language") language: String,
@description("The embed content for the learningstep") embedUrl: Option[EmbedUrlV2],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import sttp.tapir.Schema.annotations.description
case class UpdatedLearningStepV2(
@description("The revision number for this learningstep") revision: Int,
@description("The title of the learningstep") title: Option[String],
@description("The introduction of the learningstep") introduction: Option[String],
@description("The chosen language") language: String,
@description("The description of the learningstep") description: Option[String],
@description("The embed content for the learningstep") embedUrl: Option[EmbedUrlV2],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ object DBLearningStep extends SQLSyntaxSupport[LearningStep] {
Some(rs.long(ls.c("learning_path_id"))),
meta.seqNo,
meta.title,
meta.introduction,
meta.description,
meta.embedUrl,
meta.`type`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import no.ndla.common.implicits.OptionImplicit
import no.ndla.common.model.domain.learningpath
import no.ndla.common.model.domain.learningpath.{
Description,
Introduction,
EmbedType,
EmbedUrl,
LearningPath,
Expand Down Expand Up @@ -241,6 +242,10 @@ trait ConverterService {
}

def asDomainLearningStep(newLearningStep: NewLearningStepV2, learningPath: LearningPath): Try[LearningStep] = {
val introduction = newLearningStep.introduction
.map(Introduction(_, newLearningStep.language))
.toSeq

val description = newLearningStep.description
.map(Description(_, newLearningStep.language))
.toSeq
Expand All @@ -264,6 +269,7 @@ trait ConverterService {
learningPath.id,
newSeqNo,
Seq(common.Title(newLearningStep.title, newLearningStep.language)),
introduction,
description,
embedUrl,
StepType.valueOfOrError(newLearningStep.`type`),
Expand Down Expand Up @@ -303,6 +309,12 @@ trait ConverterService {
mergeLanguageFields(existing.title, Seq(common.Title(value, updated.language)))
}

val introductions = updated.introduction match {
case None => existing.introduction
case Some(value) =>
mergeLanguageFields(existing.introduction, Seq(Introduction(value, updated.language)))
}

val descriptions = updated.description match {
case None => existing.description
case Some(value) =>
Expand All @@ -321,6 +333,7 @@ trait ConverterService {
existing.copy(
revision = Some(updated.revision),
title = titles,
introduction = introductions,
description = descriptions,
embedUrl = embedUrls,
showTitle = updated.showTitle.getOrElse(existing.showTitle),
Expand Down Expand Up @@ -493,6 +506,9 @@ trait ConverterService {
val title = findByLanguageOrBestEffort(ls.title, language)
.map(asApiTitle)
.getOrElse(api.Title("", DefaultLanguage))
val introduction =
findByLanguageOrBestEffort(ls.introduction, language)
.map(asApiIntroduction)
val description =
findByLanguageOrBestEffort(ls.description, language)
.map(asApiDescription)
Expand All @@ -506,6 +522,7 @@ trait ConverterService {
ls.revision.get,
ls.seqNo,
title,
introduction,
description,
embedUrl,
ls.showTitle,
Expand Down Expand Up @@ -606,6 +623,10 @@ trait ConverterService {
api.Title(title.title, title.language)
}

private def asApiIntroduction(introduction: Introduction): api.Introduction = {
api.Introduction(introduction.introduction, introduction.language)
}

private def asApiDescription(description: Description): api.Description = {
api.Description(description.description, description.language)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
package no.ndla.learningpathapi.validation

import no.ndla.common.errors.{ValidationException, ValidationMessage}
import no.ndla.common.model.domain.learningpath.{Description, EmbedUrl, LearningStep}
import no.ndla.common.model.domain.learningpath.{Description, EmbedUrl, Introduction, LearningStep}

import scala.util.{Failure, Success, Try}

Expand All @@ -34,12 +34,31 @@ trait LearningStepValidator {

def validateLearningStep(newLearningStep: LearningStep, allowUnknownLanguage: Boolean): Seq[ValidationMessage] = {
titleValidator.validate(newLearningStep.title, allowUnknownLanguage) ++
validateIntroduction(newLearningStep.introduction, allowUnknownLanguage) ++
validateDescription(newLearningStep.description, allowUnknownLanguage) ++
validateEmbedUrl(newLearningStep.embedUrl, allowUnknownLanguage) ++
validateLicense(newLearningStep.license).toList ++
validateThatDescriptionOrEmbedUrlOrBothIsDefined(newLearningStep).toList
}

def validateIntroduction(
introductions: Seq[Introduction],
allowUnknownLanguage: Boolean
): Seq[ValidationMessage] = {
if (introductions.isEmpty) {
List()
} else {
introductions.flatMap(introduction => {
basicHtmlTextValidator
.validate("introduction", introduction.introduction)
.toList :::
languageValidator
.validate("language", introduction.language, allowUnknownLanguage)
.toList
})
}
}

def validateDescription(descriptions: Seq[Description], allowUnknownLanguage: Boolean): Seq[ValidationMessage] = {
if (descriptions.isEmpty) {
List()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ object TestData {
None,
1,
List(common.Title("Step1Title", "nb")),
List.empty,
List(Description("Step1Description", "nb")),
List(),
StepType.INTRODUCTION,
Expand All @@ -62,6 +63,7 @@ object TestData {
None,
2,
List(common.Title("Step2Title", "nb")),
List.empty,
List(Description("Step2Description", "nb")),
List(),
learningpath.StepType.TEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import no.ndla.common.model.domain.learningpath.{
Description,
EmbedType,
EmbedUrl,
Introduction,
LearningPath,
LearningPathStatus,
LearningPathVerificationStatus,
Expand Down Expand Up @@ -71,6 +72,7 @@ class LearningPathRepositoryComponentIntegrationTest
None,
0,
List(Title("UNIT-TEST", "unknown")),
List(Introduction("UNIT-TEST", "unknown")),
List(Description("UNIT-TEST", "unknown")),
List(EmbedUrl("http://www.vg.no", "unknown", EmbedType.OEmbed)),
StepType.TEXT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class ConverterServiceTest extends UnitSuite with UnitTestEnvironment {
None
)
val domainLearningStep: LearningStep =
LearningStep(None, None, None, None, 1, List(), List(), List(), StepType.INTRODUCTION, None)
LearningStep(None, None, None, None, 1, List(), List(), List(), List(), StepType.INTRODUCTION, None)

val domainLearningStep2: LearningStep = LearningStep(
Some(1),
Expand All @@ -76,6 +76,7 @@ class ConverterServiceTest extends UnitSuite with UnitTestEnvironment {
None,
1,
List(Title("tittel", "nb")),
List(),
List(Description("deskripsjon", "nb")),
List(),
StepType.INTRODUCTION,
Expand Down Expand Up @@ -243,6 +244,7 @@ class ConverterServiceTest extends UnitSuite with UnitTestEnvironment {
1,
1,
api.Title("tittel", DefaultLanguage),
None,
Some(api.Description("deskripsjon", DefaultLanguage)),
None,
showTitle = false,
Expand Down Expand Up @@ -288,6 +290,7 @@ class ConverterServiceTest extends UnitSuite with UnitTestEnvironment {
1,
1,
api.Title("tittel", DefaultLanguage),
None,
Some(api.Description("deskripsjon", DefaultLanguage)),
None,
showTitle = false,
Expand Down Expand Up @@ -509,7 +512,7 @@ class ConverterServiceTest extends UnitSuite with UnitTestEnvironment {

test("asDomainLearningStep should work with learningpaths no matter the amount of steps") {
val newLs =
NewLearningStepV2("Tittel", Some("Beskrivelse"), "nb", Some(api.EmbedUrlV2("", "oembed")), true, "TEXT", None)
NewLearningStepV2("Tittel", Some("Beskrivelse"), None, "nb", Some(api.EmbedUrlV2("", "oembed")), true, "TEXT", None)
val lpId = 5591L
val lp1 = TestData.sampleDomainLearningPath.copy(id = Some(lpId), learningsteps = None)
val lp2 = TestData.sampleDomainLearningPath.copy(id = Some(lpId), learningsteps = Some(Seq.empty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class ReadServiceTest extends UnitSuite with UnitTestEnvironment {
List(Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = true,
Expand All @@ -101,6 +102,7 @@ class ReadServiceTest extends UnitSuite with UnitTestEnvironment {
List(Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = false,
Expand All @@ -116,6 +118,7 @@ class ReadServiceTest extends UnitSuite with UnitTestEnvironment {
List(Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
List(common.Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = true,
Expand All @@ -75,9 +76,9 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
List(common.Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = false,
status = StepStatus.ACTIVE
)

Expand All @@ -90,6 +91,7 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
List(common.Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = true,
Expand All @@ -105,9 +107,9 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
List(common.Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = false,
status = StepStatus.ACTIVE
)

Expand All @@ -120,6 +122,7 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
List(common.Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = true,
Expand All @@ -135,17 +138,17 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
List(common.Title("Tittel", "nb")),
List(),
List(),
List(),
StepType.TEXT,
None,
showTitle = false,
status = StepStatus.ACTIVE
)

val NEW_STEPV2: NewLearningStepV2 =
NewLearningStepV2("Tittel", Some("Beskrivelse"), "nb", Some(api.EmbedUrlV2("", "oembed")), true, "TEXT", None)
NewLearningStepV2("Tittel", Some("Beskrivelse"), None, "nb", Some(api.EmbedUrlV2("", "oembed")), true, "TEXT", None)

val UPDATED_STEPV2: UpdatedLearningStepV2 =
UpdatedLearningStepV2(1, Option("Tittel"), "nb", Some("Beskrivelse"), None, Some(false), None, None)
UpdatedLearningStepV2(1, Option("Tittel"), None, "nb", Some("Beskrivelse"), None, Some(false), None, None)

val rubio: Author = Author("author", "Little Marco")
val license = "publicdomain"
Expand Down Expand Up @@ -1175,7 +1178,7 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
when(clock.now()).thenReturn(newDate)
when(learningPathRepository.learningPathsWithIsBasedOn(any[Long])).thenReturn(List.empty)

val updatedLs = UpdatedLearningStepV2(1, Some("Dårlig tittel"), "nb", None, None, None, None, None)
val updatedLs = UpdatedLearningStepV2(1, Some("Dårlig tittel"), None, "nb", None, None, None, None, None)
service.updateLearningStepV2(PUBLISHED_ID, STEP1.id.get, updatedLs, PUBLISHED_OWNER.toCombined)
val updatedPath = PUBLISHED_LEARNINGPATH.copy(
status = LearningPathStatus.UNLISTED,
Expand Down Expand Up @@ -1230,7 +1233,7 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
when(clock.now()).thenReturn(newDate)
when(learningPathRepository.learningPathsWithIsBasedOn(any[Long])).thenReturn(List.empty)

val updatedLs = UpdatedLearningStepV2(1, Some("Dårlig tittel"), "nb", None, None, None, None, None)
val updatedLs = UpdatedLearningStepV2(1, Some("Dårlig tittel"), None, "nb", None, None, None, None, None)
service.updateLearningStepV2(PRIVATE_ID, STEP1.id.get, updatedLs, PRIVATE_OWNER.toCombined)
val updatedPath = PRIVATE_LEARNINGPATH.copy(
lastUpdated = newDate,
Expand All @@ -1257,7 +1260,7 @@ class UpdateServiceTest extends UnitSuite with UnitTestEnvironment {
)
when(clock.now()).thenReturn(newDate)

val updatedLs = UpdatedLearningStepV2(1, Some("Dårlig tittel"), "nb", None, None, None, None, None)
val updatedLs = UpdatedLearningStepV2(1, Some("Dårlig tittel"), None, "nb", None, None, None, None, None)
service.updateLearningStepV2(
PUBLISHED_ID,
STEP1.id.get,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class SearchServiceTest
learningPathId = None,
seqNo = 0,
title = List(),
introduction = List(),
description = List(),
embedUrl = List(),
`type` = StepType.INTRODUCTION,
Expand Down
Loading

0 comments on commit dba51e3

Please sign in to comment.