-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #614 from NDLANO/author-type-enum
Author type enum
- Loading branch information
Showing
79 changed files
with
2,509 additions
and
375 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
article-api/src/main/scala/no/ndla/articleapi/db/migration/V58__FixContributorTypes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Part of NDLA article-api | ||
* Copyright (C) 2025 NDLA | ||
* | ||
* See LICENSE | ||
* | ||
*/ | ||
|
||
package no.ndla.articleapi.db.migration | ||
|
||
import io.circe.{ACursor, parser} | ||
import io.circe.generic.auto.* | ||
import io.circe.syntax.EncoderOps | ||
import no.ndla.common.model.domain.{Author, ContributorType} | ||
import no.ndla.database.DocumentMigration | ||
|
||
class V58__FixContributorTypes extends DocumentMigration { | ||
override val tableName: String = "contentdata" | ||
override val columnName: String = "document" | ||
|
||
override def convertColumn(value: String): String = { | ||
val oldDocument = parser.parse(value).toTry.get | ||
val copyright = oldDocument.hcursor.downField("copyright") | ||
val creators = convertList(copyright, "creators") | ||
val processors = convertList(copyright, "processors") | ||
val rightsholders = convertList(copyright, "rightsholders") | ||
|
||
val newDocument = oldDocument.hcursor | ||
.downField("copyright") | ||
.withFocus(_.mapObject(_.remove("creators").add("creators", creators.asJson))) | ||
.withFocus(_.mapObject(_.remove("processors").add("processors", processors.asJson))) | ||
.withFocus(_.mapObject(_.remove("rightsholders").add("rightsholders", rightsholders.asJson))) | ||
newDocument.top.get.noSpaces | ||
} | ||
|
||
private def convertList(cursor: ACursor, fieldName: String): List[Author] = { | ||
val field = cursor.downField(fieldName) | ||
if (field.succeeded) { | ||
field.as[Option[List[OldAuthor]]].toTry.get match { | ||
case None => List.empty | ||
case Some(authors) => convertAuthors(authors) | ||
} | ||
} else List.empty | ||
} | ||
|
||
private def convertAuthors(authors: List[OldAuthor]): List[Author] = { | ||
authors.map(author => { | ||
ContributorType.valueOf(author.`type`.toLowerCase) match { | ||
case None => Author(ContributorType.mapping(author.`type`.toLowerCase), author.name) | ||
case Some(a) => Author(a, author.name) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
case class OldAuthor(`type`: String, name: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
153 changes: 153 additions & 0 deletions
153
...cle-api/src/test/scala/no/ndla/articleapi/db/migration/V58__FixContributorTypesTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
/* | ||
* Part of NDLA article-api | ||
* Copyright (C) 2024 NDLA | ||
* | ||
* See LICENSE | ||
* | ||
*/ | ||
|
||
package no.ndla.articleapi.db.migration | ||
|
||
import io.circe.parser | ||
import no.ndla.articleapi.{TestEnvironment, UnitSuite} | ||
|
||
class V58__FixContributorTypesTest extends UnitSuite with TestEnvironment { | ||
test("That copyright has correct contributor types") { | ||
val migration = new V58__FixContributorTypes | ||
val oldDocument = | ||
""" | ||
|{ | ||
| "id": 1, | ||
| "tags": [], | ||
| "title": [ | ||
| { | ||
| "title": "Title", | ||
| "language": "nb" | ||
| } | ||
| ], | ||
| "content": [ | ||
| { | ||
| "content": "<section>Content</section>", | ||
| "language": "nb" | ||
| } | ||
| ], | ||
| "created": "2017-05-29T09:43:41.000Z", | ||
| "updated": "2017-07-18T10:21:08.000Z", | ||
| "revision": 1, | ||
| "copyright": { | ||
| "origin": "", | ||
| "license": "CC-BY-SA-4.0", | ||
| "validTo": null, | ||
| "processed": false, | ||
| "validFrom": null, | ||
| "creators": [ | ||
| { | ||
| "type": "Forfatter", | ||
| "name": "Sissel Paaske" | ||
| } | ||
| ], | ||
| "processors": [], | ||
| "rightsholders": [ | ||
| { | ||
| "type": "Supplier", | ||
| "name": "Cerpus AS" | ||
| } | ||
| ] | ||
| }, | ||
| "grepCodes": [], | ||
| "metaImage": [], | ||
| "published": "2017-07-18T10:21:08.000Z", | ||
| "updatedBy": "r0gHb9Xg3li4yyXv0QSGQczV3bviakrT", | ||
| "conceptIds": [], | ||
| "disclaimer": {}, | ||
| "articleType": "standard", | ||
| "availability": "everyone", | ||
| "introduction": [ | ||
| { | ||
| "language": "nb", | ||
| "introduction": "Introduction." | ||
| } | ||
| ], | ||
| "revisionDate": "2030-01-01T00:00:00.000Z", | ||
| "visualElement": [], | ||
| "relatedContent": [], | ||
| "metaDescription": [ | ||
| { | ||
| "content": "Metabeskrivelse", | ||
| "language": "nb" | ||
| } | ||
| ], | ||
| "requiredLibraries": [] | ||
|} | ||
|""".stripMargin | ||
val newDocument = | ||
""" | ||
|{ | ||
| "id": 1, | ||
| "tags": [], | ||
| "title": [ | ||
| { | ||
| "title": "Title", | ||
| "language": "nb" | ||
| } | ||
| ], | ||
| "content": [ | ||
| { | ||
| "content": "<section>Content</section>", | ||
| "language": "nb" | ||
| } | ||
| ], | ||
| "created": "2017-05-29T09:43:41.000Z", | ||
| "updated": "2017-07-18T10:21:08.000Z", | ||
| "revision": 1, | ||
| "copyright": { | ||
| "origin": "", | ||
| "license": "CC-BY-SA-4.0", | ||
| "validTo": null, | ||
| "processed": false, | ||
| "validFrom": null, | ||
| "creators": [ | ||
| { | ||
| "type": "writer", | ||
| "name": "Sissel Paaske" | ||
| } | ||
| ], | ||
| "processors": [], | ||
| "rightsholders": [ | ||
| { | ||
| "type": "supplier", | ||
| "name": "Cerpus AS" | ||
| } | ||
| ] | ||
| }, | ||
| "grepCodes": [], | ||
| "metaImage": [], | ||
| "published": "2017-07-18T10:21:08.000Z", | ||
| "updatedBy": "r0gHb9Xg3li4yyXv0QSGQczV3bviakrT", | ||
| "conceptIds": [], | ||
| "disclaimer": {}, | ||
| "articleType": "standard", | ||
| "availability": "everyone", | ||
| "introduction": [ | ||
| { | ||
| "language": "nb", | ||
| "introduction": "Introduction." | ||
| } | ||
| ], | ||
| "revisionDate": "2030-01-01T00:00:00.000Z", | ||
| "visualElement": [], | ||
| "relatedContent": [], | ||
| "metaDescription": [ | ||
| { | ||
| "content": "Metabeskrivelse", | ||
| "language": "nb" | ||
| } | ||
| ], | ||
| "requiredLibraries": [] | ||
|} | ||
|""".stripMargin | ||
|
||
val expected = parser.parse(newDocument).toTry.get | ||
migration.convertColumn(oldDocument) should be(expected.noSpaces) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.