Skip to content

Commit 9f8519b

Browse files
committed
Support moving folders
1 parent 9482ee7 commit 9f8519b

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

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

Lines changed: 3 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")
@@ -124,6 +125,8 @@ case class NewFolderDTO(
124125
)
125126

126127
case class UpdatedFolderDTO(
128+
@description("Id of parent folder")
129+
parentId: UpdateOrDelete[String],
127130
@description("Folder name")
128131
name: Option[String],
129132
@description("Status of the folder (private, shared)")

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

Lines changed: 10 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.{
@@ -74,6 +75,14 @@ class FolderConverterService(using clock: Clock) extends StrictLogging {
7475
}
7576

7677
def mergeFolder(existing: domain.Folder, updated: api.UpdatedFolderDTO): domain.Folder = {
78+
val parentId = updated.parentId match {
79+
case Delete => None
80+
case Missing => existing.parentId
81+
case UpdateWith(value) => toUUIDValidated(Some(value), "parentId") match {
82+
case Success(uuid) => Some(uuid)
83+
case failure => throw failure.failed.get
84+
}
85+
}
7786
val name = updated.name.getOrElse(existing.name)
7887
val status = updated.status.flatMap(FolderStatus.valueOf).getOrElse(existing.status)
7988
val description = updated.description.orElse(existing.description)
@@ -90,7 +99,7 @@ class FolderConverterService(using clock: Clock) extends StrictLogging {
9099
resources = existing.resources,
91100
subfolders = existing.subfolders,
92101
feideId = existing.feideId,
93-
parentId = existing.parentId,
102+
parentId = parentId,
94103
name = name,
95104
status = status,
96105
rank = existing.rank,

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

Lines changed: 25 additions & 5 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}
@@ -253,14 +254,27 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
253254
description = Some("hei"),
254255
user = None,
255256
)
256-
val updatedWithData =
257-
UpdatedFolderDTO(name = Some("newNamae"), status = Some("shared"), description = Some("halla"))
258-
val updatedWithoutData = UpdatedFolderDTO(name = None, status = None, description = None)
257+
val updatedWithData = UpdatedFolderDTO(
258+
parentId = Missing,
259+
name = Some("newNamae"),
260+
status = Some("shared"),
261+
description = Some("halla"),
262+
)
263+
val updatedWithoutData = UpdatedFolderDTO(parentId = Missing, name = None, status = None, description = None)
259264
val updatedWithGarbageData = UpdatedFolderDTO(
265+
parentId = Missing,
260266
name = Some("huehueuheasdasd+++"),
261267
status = Some("det å joike er noe kult"),
262268
description = Some("jog ska visa deg garbage jog"),
263269
)
270+
val newParentUUID = UUID.randomUUID()
271+
val updatedWithNewParent = UpdatedFolderDTO(
272+
parentId = UpdateWith[String](newParentUUID.toString),
273+
name = None,
274+
status = None,
275+
description = None,
276+
)
277+
val updatedWithNoParent = UpdatedFolderDTO(parentId = Delete, name = None, status = None, description = None)
264278

265279
val expected1 =
266280
existing.copy(name = "newNamae", status = FolderStatus.SHARED, shared = Some(shared), description = Some("halla"))
@@ -270,14 +284,20 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
270284
status = FolderStatus.PRIVATE,
271285
description = Some("jog ska visa deg garbage jog"),
272286
)
287+
val expected4 = existing.copy(parentId = Some(newParentUUID))
288+
val expected5 = existing.copy(parentId = None)
273289

274290
val result1 = service.mergeFolder(existing, updatedWithData)
275291
val result2 = service.mergeFolder(existing, updatedWithoutData)
276292
val result3 = service.mergeFolder(existing, updatedWithGarbageData)
293+
val result4 = service.mergeFolder(existing, updatedWithNewParent)
294+
val result5 = service.mergeFolder(existing, updatedWithNoParent)
277295

278296
result1 should be(expected1)
279297
result2 should be(expected2)
280298
result3 should be(expected3)
299+
result4 should be(expected4)
300+
result5 should be(expected5)
281301
}
282302

283303
test("that mergeFolder works correctly for shared field and folder status update") {
@@ -302,8 +322,8 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
302322
)
303323
val existingShared = existingBase.copy(status = FolderStatus.SHARED, shared = Some(sharedBefore))
304324
val existingPrivate = existingBase.copy(status = FolderStatus.PRIVATE, shared = None)
305-
val updatedShared = UpdatedFolderDTO(name = None, status = Some("shared"), description = None)
306-
val updatedPrivate = UpdatedFolderDTO(name = None, status = Some("private"), description = None)
325+
val updatedShared = UpdatedFolderDTO(parentId = Missing, name = None, status = Some("shared"), description = None)
326+
val updatedPrivate = UpdatedFolderDTO(parentId = Missing, name = None, status = Some("private"), description = None)
307327
val expected1 = existingBase.copy(status = FolderStatus.SHARED, shared = Some(sharedBefore))
308328
val expected2 = existingBase.copy(status = FolderStatus.PRIVATE, shared = None)
309329
val expected3 = existingBase.copy(status = FolderStatus.SHARED, shared = Some(sharedNow))

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

Lines changed: 14 additions & 9 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}
@@ -679,7 +680,7 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
679680
val feideId = "FEIDE"
680681
val folderId = UUID.randomUUID()
681682
val parentId = UUID.randomUUID()
682-
val updateFolder = api.UpdatedFolderDTO(name = Some("asd"), status = None, description = None)
683+
val updateFolder = api.UpdatedFolderDTO(parentId = Missing, name = Some("asd"), status = None, description = None)
683684

684685
val existingFolder = domain.Folder(
685686
id = folderId,
@@ -742,7 +743,8 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
742743
val feideId = "FEIDE"
743744
val folderId = UUID.randomUUID()
744745
val parentId = UUID.randomUUID()
745-
val updateFolder = api.UpdatedFolderDTO(name = None, status = Some("shared"), description = None)
746+
val updateFolder =
747+
api.UpdatedFolderDTO(parentId = Missing, name = None, status = Some("shared"), description = None)
746748

747749
val existingFolder = domain.Folder(
748750
id = folderId,
@@ -981,7 +983,8 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
981983
val myNDLAUser = emptyMyNDLAUser.copy(userRole = UserRole.STUDENT)
982984
when(userService.getOrCreateMyNDLAUserIfNotExist(any, any)(using any)).thenReturn(Success(myNDLAUser))
983985

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

997-
val updatedFolder = api.UpdatedFolderDTO(name = Some("asd"), status = None, description = None)
1000+
val updatedFolder = api.UpdatedFolderDTO(parentId = Missing, name = Some("asd"), status = None, description = None)
9981001
val Failure(result) =
9991002
service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), updatedFolder): @unchecked
10001003
result.getMessage should be("You do not have write access while write restriction is active.")
@@ -1005,7 +1008,7 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
10051008
when(userService.getOrCreateMyNDLAUserIfNotExist(any, any)(using any)).thenReturn(Success(myNDLAUser))
10061009
when(configService.isMyNDLAWriteRestricted).thenReturn(Success(false))
10071010

1008-
val updatedFolder = api.UpdatedFolderDTO(name = Some("asd"), status = None, description = None)
1011+
val updatedFolder = api.UpdatedFolderDTO(parentId = Missing, name = Some("asd"), status = None, description = None)
10091012
val result = service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), updatedFolder)
10101013
result.isSuccess should be(true)
10111014
}
@@ -1015,10 +1018,12 @@ class FolderWriteServiceTest extends UnitTestSuite with TestEnvironment {
10151018
when(userService.getOrCreateMyNDLAUserIfNotExist(any, any)(using any)).thenReturn(Success(myNDLAUser))
10161019
when(configService.isMyNDLAWriteRestricted).thenReturn(Success(true))
10171020

1018-
val folderWithUpdatedName = api.UpdatedFolderDTO(name = Some("asd"), status = None, description = None)
1019-
val folderWithUpdatedStatus = api.UpdatedFolderDTO(name = None, status = Some("shared"), description = None)
1020-
val result1 = service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), folderWithUpdatedName)
1021-
val result2 = service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), folderWithUpdatedStatus)
1021+
val folderWithUpdatedName =
1022+
api.UpdatedFolderDTO(parentId = Missing, name = Some("asd"), status = None, description = None)
1023+
val folderWithUpdatedStatus =
1024+
api.UpdatedFolderDTO(parentId = Missing, name = None, status = Some("shared"), description = None)
1025+
val result1 = service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), folderWithUpdatedName)
1026+
val result2 = service.isOperationAllowedOrAccessDenied("feideid", Some("accesstoken"), folderWithUpdatedStatus)
10221027
result1.isSuccess should be(true)
10231028
result2.isSuccess should be(true)
10241029
}

0 commit comments

Comments
 (0)