Skip to content

Commit

Permalink
Merge pull request #548 from NDLANO/add-topic-to-favourites
Browse files Browse the repository at this point in the history
Add topic to possible favorite types
  • Loading branch information
gunnarvelle authored Dec 3, 2024
2 parents 8018290 + 8967ab1 commit dc1d15a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ object ResourceType extends Enum[ResourceType] with CirceEnum[ResourceType] {
implicit val enumTsType: TSNamedType[ResourceType] =
TSType.alias[ResourceType]("ResourceType", TSUnion(values.map(e => TSLiteralString(e.entryName))))

case object Article extends ResourceType("article")
case object Audio extends ResourceType("audio")
case object Concept extends ResourceType("concept")
case object Image extends ResourceType("image")
case object Audio extends ResourceType("audio")
case object Multidisciplinary extends ResourceType("multidisciplinary")
case object Article extends ResourceType("article")
case object Learningpath extends ResourceType("learningpath")
case object Multidisciplinary extends ResourceType("multidisciplinary")
case object Topic extends ResourceType("topic")
case object Video extends ResourceType("video")
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import sttp.tapir.server.ServerEndpoint
import java.util.UUID

trait FolderController {
this: FolderReadService with FolderWriteService with ErrorHandling with TapirController =>
this: FolderReadService & FolderWriteService & ErrorHandling & TapirController =>
val folderController: FolderController

class FolderController extends TapirController {
Expand Down Expand Up @@ -71,7 +71,7 @@ trait FolderController {
s"Which resource types to exclude. If None all resource types are included. To provide multiple resource types, separate by comma (,)."
)

import io.circe.generic.auto._
import io.circe.generic.auto.*

private def getAllFolders: ServerEndpoint[Any, Eff] = endpoint.get
.summary("Fetch top folders that belongs to a user")
Expand Down Expand Up @@ -111,7 +111,7 @@ trait FolderController {
folderWriteService.newFolder(newFolder, feideHeader)
}

private def updateFolder: ServerEndpoint[Any, Eff] = endpoint.patch
private def updateFolder(): ServerEndpoint[Any, Eff] = endpoint.patch
.summary("Update folder with new data")
.description("Update folder with new data")
.in(feideHeader)
Expand All @@ -123,7 +123,7 @@ trait FolderController {
folderWriteService.updateFolder(folderId, updatedFolder, feideHeader)
}

private def removeFolder: ServerEndpoint[Any, Eff] = endpoint.delete
private def removeFolder(): ServerEndpoint[Any, Eff] = endpoint.delete
.summary("Remove folder from user folders")
.description("Remove folder from user folders")
.in(feideHeader)
Expand All @@ -134,7 +134,7 @@ trait FolderController {
folderWriteService.deleteFolder(folderId, feideHeader).map(_ => ())
}

val defaultSize = 5
private val defaultSize: Int = 5
val size: EndpointInput.Query[Int] = query[Int]("size")
.description("Limit the number of results to this many elements")
.default(defaultSize)
Expand Down Expand Up @@ -179,7 +179,7 @@ trait FolderController {
folderWriteService.newFolderResourceConnection(folderId, newResource, feideHeader)
}

private def updateResource: ServerEndpoint[Any, Eff] = endpoint.patch
private def updateResource(): ServerEndpoint[Any, Eff] = endpoint.patch
.summary("Updated selected resource")
.description("Updates selected resource")
.in("resources" / pathResourceId)
Expand All @@ -191,7 +191,7 @@ trait FolderController {
folderWriteService.updateResource(resourceId, updatedResource, feideHeader)
}

private def deleteResource: ServerEndpoint[Any, Eff] = endpoint.delete
private def deleteResource(): ServerEndpoint[Any, Eff] = endpoint.delete
.summary("Delete selected resource")
.description("Delete selected resource")
.in(feideHeader)
Expand All @@ -215,7 +215,7 @@ trait FolderController {
folderReadService.getSharedFolder(folderId, feideHeader)
}

val folderStatus: EndpointInput.Query[FolderStatus.Value] =
private val folderStatus: EndpointInput.Query[FolderStatus.Value] =
query[FolderStatus.Value]("folder-status").description("Status of the folder")
private def changeStatusForFolderAndSubFolders: ServerEndpoint[Any, Eff] = endpoint.patch
.summary("Change status for given folder and all its subfolders")
Expand Down Expand Up @@ -292,7 +292,7 @@ trait FolderController {
folderWriteService.newSaveSharedFolder(folderId, feideHeader)
}

private def deleteFolderUserConnection: ServerEndpoint[Any, Eff] = endpoint.delete
private def deleteFolderUserConnection(): ServerEndpoint[Any, Eff] = endpoint.delete
.summary("Deletes a saved shared folder")
.description("Deletes a saved shared folder")
.in("shared" / pathFolderId / "save")
Expand All @@ -309,19 +309,19 @@ trait FolderController {
fetchRecent,
getSingleFolder,
createNewFolder,
updateFolder,
removeFolder,
updateFolder(),
removeFolder(),
createFolderResource,
updateResource,
deleteResource,
updateResource(),
deleteResource(),
fetchSharedFolder,
changeStatusForFolderAndSubFolders,
cloneFolder,
sortFolderResources,
sortFolderFolders,
sortSavedSharedFolders,
createFolderUserConnection,
deleteFolderUserConnection
deleteFolderUserConnection()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ trait StatsController {
private val pathResourceIds =
path[CommaSeparated[String]]("resourceIds").description("IDs of the resources to look up")

def getFolderResourceFavorites: ServerEndpoint[Any, Eff] = endpoint.get
private def getFolderResourceFavorites: ServerEndpoint[Any, Eff] = endpoint.get
.summary("Get folder resource favorites")
.description("Get folder resource favorites")
.in("favorites" / pathResourceType / pathResourceIds)
Expand All @@ -57,7 +57,7 @@ trait StatsController {
folderReadService.getFavouriteStatsForResource(resourceIds.values, resourceType.values)
}

def getAllTheFavorites: ServerEndpoint[Any, Eff] = endpoint.get
private def getAllTheFavorites: ServerEndpoint[Any, Eff] = endpoint.get
.summary("Get number of favorites for favorited resources")
.description("Get number of favorites for favorited resources")
.in("favorites")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,8 @@ import scala.annotation.tailrec
import scala.util.{Failure, Success, Try}

trait FolderReadService {
this: FolderConverterService
with FolderRepository
with UserRepository
with FeideApiClient
with Clock
with ConfigService
with UserService =>
this: FolderConverterService & FolderRepository & UserRepository & FeideApiClient & Clock & ConfigService &
UserService =>

val folderReadService: FolderReadService

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,8 @@ import scala.annotation.tailrec
import scala.util.{Failure, Success, Try}

trait FolderWriteService {
this: FolderReadService
with Clock
with FeideApiClient
with FolderRepository
with FolderConverterService
with UserRepository
with ConfigService
with UserService
with SearchApiClient =>
this: FolderReadService & Clock & FeideApiClient & FolderRepository & FolderConverterService & UserRepository &
ConfigService & UserService & SearchApiClient =>

val folderWriteService: FolderWriteService
class FolderWriteService {
Expand Down Expand Up @@ -543,7 +536,7 @@ trait FolderWriteService {
_ <- checkDepth(validatedParentId)
} yield validatedParentId

private def getNextRank(siblings: Seq[_]): Int = siblings.length + 1
private def getNextRank(siblings: Seq[?]): Int = siblings.length + 1

private[service] def changeStatusToSharedIfParentIsShared(
newFolder: NewFolder,
Expand Down Expand Up @@ -630,6 +623,7 @@ trait FolderWriteService {
resource.resourceType match {
case ResourceType.Multidisciplinary => searchApiClient.reindexDraft(resource.resourceId)
case ResourceType.Article => searchApiClient.reindexDraft(resource.resourceId)
case ResourceType.Topic => searchApiClient.reindexDraft(resource.resourceId)
case ResourceType.Learningpath => searchApiClient.reindexLearningpath(resource.resourceId)
case _ =>
}
Expand Down Expand Up @@ -697,7 +691,7 @@ trait FolderWriteService {
def canWriteDuringMyNDLAWriteRestrictionsOrAccessDenied(
feideId: FeideID,
feideAccessToken: Option[FeideAccessToken]
): Try[_] = {
): Try[?] = {
getMyNDLAUser(feideId, feideAccessToken)
.flatMap(myNDLAUser =>
canWriteNow(myNDLAUser).flatMap {
Expand Down Expand Up @@ -739,7 +733,7 @@ trait FolderWriteService {
folderRepository.deleteFolderUserConnection(folderId.some, feideId.some)
}

private def isTeacherOrAccessDenied(feideId: FeideID, feideAccessToken: Option[FeideAccessToken]): Try[_] = {
private def isTeacherOrAccessDenied(feideId: FeideID, feideAccessToken: Option[FeideAccessToken]): Try[?] = {
getMyNDLAUser(feideId, feideAccessToken)
.flatMap(myNDLAUser => {
if (myNDLAUser.isTeacher) Success(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import sttp.client3.quick.*

import java.util.UUID
import java.util.concurrent.Executors
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.{ExecutionContext, ExecutionContextExecutorService, Future}
import scala.concurrent.duration.DurationInt
import scala.util.{Failure, Success}

Expand All @@ -41,7 +41,7 @@ class CloneFolderTest
with UnitSuite {

val myndlaApiPort: Int = findFreePort
val pgc: PostgreSQLContainer[_] = postgresContainer.get
val pgc: PostgreSQLContainer[?] = postgresContainer.get
val redisPort: Int = redisContainer.get.port
val myndlaproperties: MyNdlaApiProperties = new MyNdlaApiProperties {
override def ApplicationPort: Int = myndlaApiPort
Expand Down Expand Up @@ -87,7 +87,8 @@ class CloneFolderTest
val myndlaApiFolderUrl: String = s"$myndlaApiBaseUrl/myndla-api/v1/folders"

override def beforeAll(): Unit = {
implicit val ec = ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor)
implicit val ec: ExecutionContextExecutorService =
ExecutionContext.fromExecutorService(Executors.newSingleThreadExecutor)
Future { myndlaApi.run() }: Unit
Thread.sleep(4000)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,28 @@ class FolderRepositoryTest
val resource1 =
repository.insertResource("feide", "/path1", ResourceType.Article, created, TestData.baseResourceDocument)
val resource2 =
repository.insertResource("feide", "/path2", ResourceType.Article, created, TestData.baseResourceDocument)
repository.insertResource("feide", "/path2", ResourceType.Topic, created, TestData.baseResourceDocument)
val resource3 =
repository.insertResource("feide", "/path3", ResourceType.Article, created, TestData.baseResourceDocument)
repository.insertResource("feide", "/path3", ResourceType.Multidisciplinary, created, TestData.baseResourceDocument)
val resource4 =
repository.insertResource("feide", "/path4", ResourceType.Image, created, TestData.baseResourceDocument)
val resource5 =
repository.insertResource("feide", "/path5", ResourceType.Audio, created, TestData.baseResourceDocument)
val resource6 =
repository.insertResource("feide", "/path6", ResourceType.Concept, created, TestData.baseResourceDocument)
val resource7 =
repository.insertResource("feide", "/path7", ResourceType.Learningpath, created, TestData.baseResourceDocument)
val resource8 =
repository.insertResource("feide", "/path8", ResourceType.Video, created, TestData.baseResourceDocument)

repository.resourceWithId(resource1.get.id) should be(resource1)
repository.resourceWithId(resource2.get.id) should be(resource2)
repository.resourceWithId(resource3.get.id) should be(resource3)
repository.resourceWithId(resource4.get.id) should be(resource4)
repository.resourceWithId(resource5.get.id) should be(resource5)
repository.resourceWithId(resource6.get.id) should be(resource6)
repository.resourceWithId(resource7.get.id) should be(resource7)
repository.resourceWithId(resource8.get.id) should be(resource8)
}

test("that connecting folders and resources works as expected") {
Expand Down
1 change: 1 addition & 0 deletions project/myndlaapi.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ object myndlaapi extends Module {
"ConfigMetaRestricted",
"no.ndla.common.model.api.myndla.MyNDLAUser",
"no.ndla.common.model.api.myndla.UpdatedMyNDLAUser",
"no.ndla.common.model.domain.ResourceType",
"config.ConfigMeta",
"Folder",
"FolderData",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,15 @@ trait SearchConverterService {
Success(
value.getFavorites(
draft.id.get.toString,
List(MyNDLAResourceType.Article, MyNDLAResourceType.Multidisciplinary)
List(MyNDLAResourceType.Article, MyNDLAResourceType.Multidisciplinary, MyNDLAResourceType.Topic)
)
)
case None =>
myndlaApiClient
.getStatsFor(draft.id.get.toString, List(MyNDLAResourceType.Article, MyNDLAResourceType.Multidisciplinary))
.getStatsFor(
draft.id.get.toString,
List(MyNDLAResourceType.Article, MyNDLAResourceType.Multidisciplinary, MyNDLAResourceType.Topic)
)
.map(_.map(_.favourites).sum)
}).?

Expand Down
2 changes: 1 addition & 1 deletion typescript/types-backend/myndla-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,4 +288,4 @@ export interface IUserFolder {
sharedFolders: IFolder[]
}

export type ResourceType = ("concept" | "image" | "audio" | "multidisciplinary" | "article" | "learningpath" | "video")
export type ResourceType = ("article" | "audio" | "concept" | "image" | "learningpath" | "multidisciplinary" | "topic" | "video")

0 comments on commit dc1d15a

Please sign in to comment.