Skip to content

Commit 8ad2846

Browse files
committed
Add support for moving folder
1 parent 62b748a commit 8ad2846

File tree

4 files changed

+54
-16
lines changed

4 files changed

+54
-16
lines changed

myndla-api/src/main/scala/no/ndla/myndlaapi/model/api/FolderDTO.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import sttp.tapir.Schema.annotations.description
2020

2121
import java.util.UUID
2222
import no.ndla.common.DeriveHelpers
23+
import no.ndla.common.model.api.UpdateOrDelete
2324

2425
case class OwnerDTO(
2526
@description("Name of the owner") name: String
@@ -104,6 +105,7 @@ case class NewFolderDTO(
104105
)
105106

106107
case class UpdatedFolderDTO(
108+
@description("Id of parent folder") parentId: UpdateOrDelete[String],
107109
@description("Folder name") name: Option[String],
108110
@description("Status of the folder (private, shared)") status: Option[String],
109111
@description("Description of the folder") description: Option[String]

myndla-api/src/main/scala/no/ndla/myndlaapi/service/FolderConverterService.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import com.typesafe.scalalogging.StrictLogging
1313
import no.ndla.common.{Clock, model}
1414
import no.ndla.common.errors.ValidationException
1515
import no.ndla.common.implicits.*
16+
import no.ndla.common.model.api.{Delete, Missing, UpdateWith}
1617
import no.ndla.common.model.api.myndla.UpdatedMyNDLAUserDTO
1718
import no.ndla.common.model.domain.myndla
1819
import no.ndla.common.model.domain.myndla.{
@@ -75,6 +76,15 @@ class FolderConverterService(using clock: Clock) extends StrictLogging {
7576
}
7677

7778
def mergeFolder(existing: domain.Folder, updated: api.UpdatedFolderDTO): domain.Folder = {
79+
val parentId = updated.parentId match {
80+
case Delete => None
81+
case Missing => existing.parentId
82+
case UpdateWith(value) =>
83+
toUUIDValidated(Some(value), "parentId") match {
84+
case Success(uuid) => Some(uuid)
85+
case failure => throw failure.failed.get
86+
}
87+
}
7888
val name = updated.name.getOrElse(existing.name)
7989
val status = updated.status.flatMap(FolderStatus.valueOf).getOrElse(existing.status)
8090
val description = updated.description.orElse(existing.description)
@@ -91,7 +101,7 @@ class FolderConverterService(using clock: Clock) extends StrictLogging {
91101
resources = existing.resources,
92102
subfolders = existing.subfolders,
93103
feideId = existing.feideId,
94-
parentId = existing.parentId,
104+
parentId = parentId,
95105
name = name,
96106
status = status,
97107
rank = existing.rank,

myndla-api/src/test/scala/no/ndla/myndlaapi/service/FolderConverterServiceTest.scala

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
package no.ndla.myndlaapi.service
1010

1111
import no.ndla.common.model.NDLADate
12+
import no.ndla.common.model.api.{Delete, Missing, UpdateWith}
1213
import no.ndla.common.model.api.myndla.{MyNDLAGroupDTO, MyNDLAUserDTO, UpdatedMyNDLAUserDTO}
1314
import no.ndla.common.model.domain.ResourceType
1415
import no.ndla.common.model.domain.myndla.{FolderStatus, MyNDLAGroup, MyNDLAUser, UserRole}
@@ -263,14 +264,28 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
263264
user = None
264265
)
265266
val updatedWithData =
266-
UpdatedFolderDTO(name = Some("newNamae"), status = Some("shared"), description = Some("halla"))
267-
val updatedWithoutData = UpdatedFolderDTO(name = None, status = None, description = None)
267+
UpdatedFolderDTO(
268+
parentId = Missing,
269+
name = Some("newNamae"),
270+
status = Some("shared"),
271+
description = Some("halla")
272+
)
273+
val updatedWithoutData = UpdatedFolderDTO(parentId = Missing, name = None, status = None, description = None)
268274
val updatedWithGarbageData =
269275
UpdatedFolderDTO(
276+
parentId = Missing,
270277
name = Some("huehueuheasdasd+++"),
271278
status = Some("det å joike er noe kult"),
272279
description = Some("jog ska visa deg garbage jog")
273280
)
281+
val newParentUUID = UUID.randomUUID()
282+
val updatedWithNewParent = UpdatedFolderDTO(
283+
parentId = UpdateWith[String](newParentUUID.toString),
284+
name = None,
285+
status = None,
286+
description = None
287+
)
288+
val updatedWithNoParent = UpdatedFolderDTO(parentId = Delete, name = None, status = None, description = None)
274289

275290
val expected1 =
276291
existing.copy(name = "newNamae", status = FolderStatus.SHARED, shared = Some(shared), description = Some("halla"))
@@ -280,14 +295,20 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
280295
status = FolderStatus.PRIVATE,
281296
description = Some("jog ska visa deg garbage jog")
282297
)
298+
val expected4 = existing.copy(parentId = Some(newParentUUID))
299+
val expected5 = existing.copy(parentId = None)
283300

284301
val result1 = service.mergeFolder(existing, updatedWithData)
285302
val result2 = service.mergeFolder(existing, updatedWithoutData)
286303
val result3 = service.mergeFolder(existing, updatedWithGarbageData)
304+
val result4 = service.mergeFolder(existing, updatedWithNewParent)
305+
val result5 = service.mergeFolder(existing, updatedWithNoParent)
287306

288307
result1 should be(expected1)
289308
result2 should be(expected2)
290309
result3 should be(expected3)
310+
result4 should be(expected4)
311+
result5 should be(expected5)
291312
}
292313

293314
test("that mergeFolder works correctly for shared field and folder status update") {
@@ -312,12 +333,12 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
312333
)
313334
val existingShared = existingBase.copy(status = FolderStatus.SHARED, shared = Some(sharedBefore))
314335
val existingPrivate = existingBase.copy(status = FolderStatus.PRIVATE, shared = None)
315-
val updatedShared = UpdatedFolderDTO(name = None, status = Some("shared"), description = None)
316-
val updatedPrivate = UpdatedFolderDTO(name = None, status = Some("private"), description = None)
317-
val expected1 = existingBase.copy(status = FolderStatus.SHARED, shared = Some(sharedBefore))
318-
val expected2 = existingBase.copy(status = FolderStatus.PRIVATE, shared = None)
319-
val expected3 = existingBase.copy(status = FolderStatus.SHARED, shared = Some(sharedNow))
320-
val expected4 = existingBase.copy(status = FolderStatus.PRIVATE, shared = None)
336+
val updatedShared = UpdatedFolderDTO(parentId = Missing, name = None, status = Some("shared"), description = None)
337+
val updatedPrivate = UpdatedFolderDTO(parentId = Missing, name = None, status = Some("private"), description = None)
338+
val expected1 = existingBase.copy(status = FolderStatus.SHARED, shared = Some(sharedBefore))
339+
val expected2 = existingBase.copy(status = FolderStatus.PRIVATE, shared = None)
340+
val expected3 = existingBase.copy(status = FolderStatus.SHARED, shared = Some(sharedNow))
341+
val expected4 = existingBase.copy(status = FolderStatus.PRIVATE, shared = None)
321342

322343
val result1 = service.mergeFolder(existingShared, updatedShared)
323344
val result2 = service.mergeFolder(existingShared, updatedPrivate)

myndla-api/src/test/scala/no/ndla/myndlaapi/service/FolderWriteServiceTest.scala

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ package no.ndla.myndlaapi.service
1010

1111
import no.ndla.common.errors.{AccessDeniedException, ValidationException}
1212
import no.ndla.common.model.NDLADate
13+
import no.ndla.common.model.api.Missing
1314
import no.ndla.common.model.domain.ResourceType
1415
import no.ndla.common.model.domain.myndla.{FolderStatus, UserRole}
1516
import no.ndla.myndlaapi.TestData.{emptyDomainFolder, emptyDomainResource, emptyMyNDLAUser}
@@ -670,7 +671,7 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
670671
val feideId = "FEIDE"
671672
val folderId = UUID.randomUUID()
672673
val parentId = UUID.randomUUID()
673-
val updateFolder = api.UpdatedFolderDTO(name = Some("asd"), status = None, description = None)
674+
val updateFolder = api.UpdatedFolderDTO(parentId = Missing, name = Some("asd"), status = None, description = None)
674675

675676
val existingFolder = domain.Folder(
676677
id = folderId,
@@ -733,7 +734,8 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
733734
val feideId = "FEIDE"
734735
val folderId = UUID.randomUUID()
735736
val parentId = UUID.randomUUID()
736-
val updateFolder = api.UpdatedFolderDTO(name = None, status = Some("shared"), description = None)
737+
val updateFolder =
738+
api.UpdatedFolderDTO(parentId = Missing, name = None, status = Some("shared"), description = None)
737739

738740
val existingFolder = domain.Folder(
739741
id = folderId,
@@ -991,7 +993,8 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
991993
val myNDLAUser = emptyMyNDLAUser.copy(userRole = UserRole.STUDENT)
992994
when(userService.getOrCreateMyNDLAUserIfNotExist(any, any)(using any)).thenReturn(Success(myNDLAUser))
993995

994-
val updatedFolder = api.UpdatedFolderDTO(name = None, status = Some("shared"), description = None)
996+
val updatedFolder =
997+
api.UpdatedFolderDTO(parentId = Missing, name = None, status = Some("shared"), description = None)
995998
val Failure(result) =
996999
service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), updatedFolder): @unchecked
9971000
result.getMessage should be("You do not have necessary permissions to share folders.")
@@ -1004,7 +1007,7 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
10041007
when(userService.getOrCreateMyNDLAUserIfNotExist(any, any)(using any)).thenReturn(Success(myNDLAUser))
10051008
when(configService.isMyNDLAWriteRestricted).thenReturn(Success(true))
10061009

1007-
val updatedFolder = api.UpdatedFolderDTO(name = Some("asd"), status = None, description = None)
1010+
val updatedFolder = api.UpdatedFolderDTO(parentId = Missing, name = Some("asd"), status = None, description = None)
10081011
val Failure(result) =
10091012
service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), updatedFolder): @unchecked
10101013
result.getMessage should be("You do not have write access while write restriction is active.")
@@ -1015,7 +1018,7 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
10151018
when(userService.getOrCreateMyNDLAUserIfNotExist(any, any)(using any)).thenReturn(Success(myNDLAUser))
10161019
when(configService.isMyNDLAWriteRestricted).thenReturn(Success(false))
10171020

1018-
val updatedFolder = api.UpdatedFolderDTO(name = Some("asd"), status = None, description = None)
1021+
val updatedFolder = api.UpdatedFolderDTO(parentId = Missing, name = Some("asd"), status = None, description = None)
10191022
val result = service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), updatedFolder)
10201023
result.isSuccess should be(true)
10211024
}
@@ -1027,8 +1030,10 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
10271030
when(userService.getOrCreateMyNDLAUserIfNotExist(any, any)(using any)).thenReturn(Success(myNDLAUser))
10281031
when(configService.isMyNDLAWriteRestricted).thenReturn(Success(true))
10291032

1030-
val folderWithUpdatedName = api.UpdatedFolderDTO(name = Some("asd"), status = None, description = None)
1031-
val folderWithUpdatedStatus = api.UpdatedFolderDTO(name = None, status = Some("shared"), description = None)
1033+
val folderWithUpdatedName =
1034+
api.UpdatedFolderDTO(parentId = Missing, name = Some("asd"), status = None, description = None)
1035+
val folderWithUpdatedStatus =
1036+
api.UpdatedFolderDTO(parentId = Missing, name = None, status = Some("shared"), description = None)
10321037
val result1 = service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), folderWithUpdatedName)
10331038
val result2 = service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), folderWithUpdatedStatus)
10341039
result1.isSuccess should be(true)

0 commit comments

Comments
 (0)