Skip to content

Commit

Permalink
WIP: Begin moving Folder domain classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jnatten committed Nov 30, 2023
1 parent 0898522 commit 7174bbf
Show file tree
Hide file tree
Showing 20 changed files with 867 additions and 171 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
/*
* Part of NDLA learningpath-api.
* Copyright (C) 2022 NDLA
* Part of NDLA common
* Copyright (C) 2023 NDLA
*
* See LICENSE
*
*/

package no.ndla.learningpathapi.model.domain
package no.ndla.common.model.domain.myndla

import no.ndla.common.model.NDLADate
import no.ndla.learningpathapi.Props
import no.ndla.common.model.domain.myndla
import org.json4s.FieldSerializer._
import org.json4s.ext.EnumNameSerializer
import org.json4s.{DefaultFormats, FieldSerializer, Formats}
Expand Down Expand Up @@ -80,12 +79,11 @@ case class Folder(
}

trait DBFolder {
this: Props =>

object DBFolder extends SQLSyntaxSupport[Folder] {
implicit val jsonEncoder: Formats = DefaultFormats + new EnumNameSerializer(FolderStatus)
override val tableName = "folders"
override lazy val schemaName: Option[String] = Some(props.MetaSchema)
// TODO: Do we need this: override lazy val schemaName: Option[String] = Some(props.MetaSchema)

val repositorySerializer: Formats = jsonEncoder + FieldSerializer[Folder](
ignore("id") orElse
Expand Down Expand Up @@ -113,7 +111,7 @@ trait DBFolder {
for {
id <- id
status <- status
} yield Folder(
} yield myndla.Folder(
id = id,
parentId = parentId,
feideId = feideId,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/*
* Part of NDLA learningpath-api.
* Copyright (C) 2022 NDLA
* Part of NDLA common
* Copyright (C) 2023 NDLA
*
* See LICENSE
*
*/

package no.ndla.learningpathapi.model.domain
package no.ndla.common.model.domain.myndla

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* Part of NDLA common
* Copyright (C) 2023 NDLA
*
* See LICENSE
*/

package no.ndla.common.model.domain

package object myndla {
type FeideID = String
type FeideAccessToken = String
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package no.ndla.learningpathapi
import com.zaxxer.hikari.HikariDataSource
import no.ndla.common.Clock
import no.ndla.common.configuration.BaseComponentRegistry
import no.ndla.common.model.domain.myndla.DBFolder
import no.ndla.learningpathapi.controller.{
ConfigController,
FolderController,
Expand All @@ -24,7 +25,6 @@ import no.ndla.learningpathapi.controller.{
import no.ndla.learningpathapi.integration._
import no.ndla.learningpathapi.model.api.ErrorHelpers
import no.ndla.learningpathapi.model.domain.{
DBFolder,
DBFolderResource,
DBLearningPath,
DBLearningStep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ package no.ndla.learningpathapi.controller

import cats.implicits._
import no.ndla.common.errors.{AccessDeniedException, ValidationException}
import no.ndla.common.model.domain.myndla.FolderStatus
import no.ndla.learningpathapi.integration.DataSource
import no.ndla.learningpathapi.model.api.{Error, ErrorHelpers, ImportReport, ValidationError}
import no.ndla.learningpathapi.model.domain._
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package no.ndla.learningpathapi.model.domain

import no.ndla.common.model.domain.myndla.Folder

import java.util.UUID

case class FolderAndDirectChildren(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

package no.ndla.learningpathapi.model.domain

import no.ndla.common.model.domain.myndla.FolderStatus

object UserRole extends Enumeration {
val EMPLOYEE: UserRole.Value = Value("employee")
val STUDENT: UserRole.Value = Value("student")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.typesafe.scalalogging.StrictLogging
import no.ndla.common.Clock
import no.ndla.common.errors.RollbackException
import no.ndla.common.model.NDLADate
import no.ndla.common.model.domain.myndla.{DBFolder, Folder, FolderStatus, NewFolderData}
import no.ndla.learningpathapi.integration.DataSource
import no.ndla.learningpathapi.model.domain._
import org.json4s.Formats
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ import cats.implicits._
import io.lemonlabs.uri.typesafe.dsl._
import no.ndla.common.{Clock, errors}
import no.ndla.common.errors.ValidationException
import no.ndla.common.model.domain.learningpath
import no.ndla.common.model.domain.{learningpath, myndla}
import no.ndla.common.model.domain.learningpath.{EmbedType, EmbedUrl}
import no.ndla.common.model.domain.myndla.{FolderStatus, NewFolderData}
import no.ndla.common.model.{api => commonApi, domain => common}
import no.ndla.language.Language.{
AllLanguages,
UnknownLanguage,
findByLanguageOrBestEffort,
getSearchLanguage,
mergeLanguageFields
}
import no.ndla.language.Language.{AllLanguages, UnknownLanguage, findByLanguageOrBestEffort, getSearchLanguage, mergeLanguageFields}
import no.ndla.learningpathapi.Props
import no.ndla.learningpathapi.integration._
import no.ndla.learningpathapi.model.api.{LearningPathStatus => _, _}
Expand Down Expand Up @@ -702,11 +697,11 @@ trait ConverterService {
newFolder: api.NewFolder,
parentId: Option[UUID],
newRank: Option[Int]
): Try[domain.NewFolderData] = {
val newStatus = domain.FolderStatus.valueOf(newFolder.status).getOrElse(domain.FolderStatus.PRIVATE)
): Try[NewFolderData] = {
val newStatus = FolderStatus.valueOf(newFolder.status).getOrElse(myndla.FolderStatus.PRIVATE)

Success(
NewFolderData(
myndla.NewFolderData(
parentId = parentId,
name = newFolder.name,
status = newStatus,
Expand All @@ -717,14 +712,14 @@ trait ConverterService {
}

def toApiFolder(
domainFolder: domain.Folder,
breadcrumbs: List[api.Breadcrumb],
feideUser: Option[domain.MyNDLAUser]
domainFolder: myndla.Folder,
breadcrumbs: List[api.Breadcrumb],
feideUser: Option[domain.MyNDLAUser]
): Try[api.Folder] = {
def loop(
folder: domain.Folder,
crumbs: List[api.Breadcrumb],
feideUser: Option[domain.MyNDLAUser]
folder: myndla.Folder,
crumbs: List[api.Breadcrumb],
feideUser: Option[domain.MyNDLAUser]
): Try[api.Folder] = folder.subfolders
.traverse(folder => {
val newCrumb = api.Breadcrumb(
Expand Down Expand Up @@ -759,7 +754,7 @@ trait ConverterService {
loop(domainFolder, breadcrumbs, feideUser)
}

def mergeFolder(existing: domain.Folder, updated: api.UpdatedFolder): domain.Folder = {
def mergeFolder(existing: myndla.Folder, updated: api.UpdatedFolder): myndla.Folder = {
val name = updated.name.getOrElse(existing.name)
val status = updated.status.flatMap(FolderStatus.valueOf).getOrElse(existing.status)
val description = updated.description.orElse(existing.description)
Expand All @@ -771,7 +766,7 @@ trait ConverterService {
case _ => None
}

domain.Folder(
myndla.Folder(
id = existing.id,
resources = existing.resources,
subfolders = existing.subfolders,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,15 @@ import cats.implicits._
import no.ndla.common.Clock
import no.ndla.common.errors.{AccessDeniedException, ValidationException}
import no.ndla.common.implicits.TryQuestionMark
import no.ndla.common.model.domain.myndla
import no.ndla.common.model.domain.myndla.{FolderStatus, NewFolderData}
import no.ndla.common.model.{api => commonApi}
import no.ndla.learningpathapi.model.api._
import no.ndla.learningpathapi.model.domain.UserInfo.LearningpathTokenUser
import no.ndla.learningpathapi.model.domain.config.ConfigKey
import no.ndla.learningpathapi.model.domain.{StepStatus, LearningPathStatus => _, _}
import no.ndla.learningpathapi.model.{api, domain}
import no.ndla.learningpathapi.repository.{
ConfigRepository,
FolderRepository,
LearningPathRepositoryComponent,
UserRepository
}
import no.ndla.learningpathapi.repository.{ConfigRepository, FolderRepository, LearningPathRepositoryComponent, UserRepository}
import no.ndla.network.clients.{FeideApiClient, FeideGroup, RedisClient}
import no.ndla.network.tapir.auth.TokenUser
import scalikejdbc.{AutoSession, DBSession}
Expand Down Expand Up @@ -244,20 +241,20 @@ trait ReadService {

private def createFavorite(
feideId: domain.FeideID
): Try[domain.Folder] = {
val favoriteFolder = domain.NewFolderData(
): Try[myndla.Folder] = {
val favoriteFolder = NewFolderData(
parentId = None,
name = FavoriteFolderDefaultName,
status = domain.FolderStatus.PRIVATE,
status = FolderStatus.PRIVATE,
rank = 1.some,
description = None
)
folderRepository.insertFolder(feideId, favoriteFolder)
}

def getBreadcrumbs(folder: domain.Folder)(implicit session: DBSession): Try[List[api.Breadcrumb]] = {
def getBreadcrumbs(folder: myndla.Folder)(implicit session: DBSession): Try[List[api.Breadcrumb]] = {
@tailrec
def getParentRecursively(folder: domain.Folder, crumbs: List[api.Breadcrumb]): Try[List[api.Breadcrumb]] = {
def getParentRecursively(folder: myndla.Folder, crumbs: List[api.Breadcrumb]): Try[List[api.Breadcrumb]] = {
folder.parentId match {
case None => Success(crumbs)
case Some(parentId) =>
Expand Down Expand Up @@ -301,7 +298,7 @@ trait ReadService {
} yield converted
}

private[service] def mergeWithFavorite(folders: List[domain.Folder], feideId: FeideID): Try[List[domain.Folder]] = {
private[service] def mergeWithFavorite(folders: List[myndla.Folder], feideId: FeideID): Try[List[myndla.Folder]] = {
for {
favorite <- if (folders.isEmpty) createFavorite(feideId).map(_.some) else Success(None)
combined = favorite.toList ++ folders
Expand All @@ -310,7 +307,7 @@ trait ReadService {

private def withResources(folderId: UUID, shouldIncludeResources: Boolean)(implicit
session: DBSession
): Try[domain.Folder] = folderRepository
): Try[myndla.Folder] = folderRepository
.folderWithId(folderId)
.flatMap(folder => {
val folderResources =
Expand All @@ -324,7 +321,7 @@ trait ReadService {
folderId: UUID,
includeSubfolders: Boolean,
includeResources: Boolean
)(implicit session: DBSession): Try[domain.Folder] = {
)(implicit session: DBSession): Try[myndla.Folder] = {
val folderWithContent = (includeSubfolders, includeResources) match {
case (true, true) => folderRepository.getFolderAndChildrenSubfoldersWithResources(folderId)
case (true, false) => folderRepository.getFolderAndChildrenSubfolders(folderId)
Expand All @@ -334,7 +331,7 @@ trait ReadService {
getWith404IfNone(folderId, folderWithContent)
}

def getWith404IfNone(folderId: UUID, maybeFolder: Try[Option[domain.Folder]]): Try[domain.Folder] = {
def getWith404IfNone(folderId: UUID, maybeFolder: Try[Option[myndla.Folder]]): Try[myndla.Folder] = {
maybeFolder match {
case Failure(ex) => Failure(ex)
case Success(Some(folder)) => Success(folder)
Expand All @@ -343,10 +340,10 @@ trait ReadService {
}

private def getSubfolders(
folders: List[domain.Folder],
includeSubfolders: Boolean,
includeResources: Boolean
)(implicit session: DBSession): Try[List[domain.Folder]] =
folders: List[myndla.Folder],
includeSubfolders: Boolean,
includeResources: Boolean
)(implicit session: DBSession): Try[List[myndla.Folder]] =
folders
.traverse(f => getSingleFolderWithContent(f.id, includeSubfolders, includeResources))

Expand Down
Loading

0 comments on commit 7174bbf

Please sign in to comment.