diff --git a/src/main/kotlin/echo/org/instructure/FolderExposed.kt b/src/main/kotlin/echo/org/instructure/FolderExposed.kt index bf7c5fc..a2f63c2 100644 --- a/src/main/kotlin/echo/org/instructure/FolderExposed.kt +++ b/src/main/kotlin/echo/org/instructure/FolderExposed.kt @@ -5,6 +5,7 @@ import echo.org.domain.Folder import echo.org.domain.FolderRepository import echo.org.domain.FolderVO import echo.org.instructure.DatabaseSingleton.dbQuery +import echo.org.instructure.FolderDocuments.documentId import echo.org.instructure.Folders.id import echo.org.instructure.Folders.parentFolderId import org.jetbrains.exposed.sql.* @@ -45,6 +46,13 @@ object Folders : Table() { } } +object FolderDocuments : Table() { + val folderId = long("folder_id").references(Folders.id, onDelete = ReferenceOption.CASCADE) + val documentId = long("document_id").references(Documents.id, onDelete = ReferenceOption.CASCADE) + override val primaryKey = PrimaryKey(folderId, documentId) +} + + class FolderRepositoryImpl : FolderRepository { override suspend fun createFolder(folderVO: FolderVO): Folder = dbQuery { Folders.insert { @@ -90,12 +98,21 @@ class FolderRepositoryImpl : FolderRepository { override suspend fun getSubfolders(folderId: Long): List = dbQuery { Folders.selectAll().where { parentFolderId eq folderId } - .mapNotNull { Folders.toFolder(it) } + .mapNotNull { Folders.toFolder(it) } .toList() } override suspend fun getDocumentsInFolder(folderId: Long): List = dbQuery { - TODO("Not yet implemented") + val toList = FolderDocuments.selectAll().where { + FolderDocuments.folderId eq folderId + }.map { + it[documentId] + }.toList() + Documents.selectAll().where { + Documents.id inList toList + }.map { + Documents.toDocument(it) + } } override suspend fun addDocumentToFolder(folderId: Long, documentId: Long) = dbQuery {