From d17dbeeda4cf78f7553c83369aae7c37e6626c6b Mon Sep 17 00:00:00 2001 From: lulz1 Date: Wed, 22 May 2024 16:52:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A4=87=E4=BB=BD=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/kotlin/echo/org/Application.kt | 15 +++-------- .../org/application/route/DocumentRoute.kt | 6 ----- src/main/kotlin/echo/org/domain/Folder.kt | 4 +-- .../echo/org/instructure/FolderExposed.kt | 16 +++++++---- src/main/kotlin/echo/org/plugins/Koin.kt | 27 +++++++++++++++++++ src/test/kotlin/echo/org/ApplicationTest.kt | 9 ++----- 6 files changed, 46 insertions(+), 31 deletions(-) create mode 100644 src/main/kotlin/echo/org/plugins/Koin.kt diff --git a/src/main/kotlin/echo/org/Application.kt b/src/main/kotlin/echo/org/Application.kt index 51fa2b8..cc7b4a2 100644 --- a/src/main/kotlin/echo/org/Application.kt +++ b/src/main/kotlin/echo/org/Application.kt @@ -1,15 +1,11 @@ package echo.org -import echo.org.application.route.DocumentModule + import echo.org.instructure.AliyunOss import echo.org.instructure.DatabaseSingleton -import echo.org.plugins.configureRouting -import echo.org.plugins.configureSecurity -import echo.org.plugins.configureSerialization -import echo.org.plugins.configureTemplating +import echo.org.plugins.* import io.ktor.server.application.* -import org.koin.ktor.ext.inject -import org.koin.ktor.plugin.Koin + fun main(args: Array) { io.ktor.server.netty.EngineMain.main(args) @@ -17,16 +13,13 @@ fun main(args: Array) { } fun Application.module() { - // Initialize Koin - install(Koin) { - modules(DocumentModule) - } AliyunOss.init() configureSecurity() configureSerialization() configureRouting() configureTemplating() initDatabase() + configureKoin() } fun Application.initDatabase() { diff --git a/src/main/kotlin/echo/org/application/route/DocumentRoute.kt b/src/main/kotlin/echo/org/application/route/DocumentRoute.kt index 42c3c3b..4d4fa04 100644 --- a/src/main/kotlin/echo/org/application/route/DocumentRoute.kt +++ b/src/main/kotlin/echo/org/application/route/DocumentRoute.kt @@ -17,16 +17,10 @@ import java.net.URLEncoder import java.nio.charset.StandardCharsets import java.time.LocalDateTime -val DocumentModule = module { - single { DocumentRepositoryImpl() } - single { AliyunOssFileServiceImpl() } -} fun Route.handleDocuments() { - val documentRepository by inject() val fileOSS by inject() - route("/documents") { get("") { val res = documentRepository.pageQuery(DocumentQueryParam()) diff --git a/src/main/kotlin/echo/org/domain/Folder.kt b/src/main/kotlin/echo/org/domain/Folder.kt index 16f1ec5..1f6b30a 100644 --- a/src/main/kotlin/echo/org/domain/Folder.kt +++ b/src/main/kotlin/echo/org/domain/Folder.kt @@ -50,10 +50,10 @@ interface FolderRepository { suspend fun getDocumentsInFolder(folderId: Long): List // Add a document to a folder - suspend fun addDocumentToFolder(folderId: Long, documentId: Long) + suspend fun addDocumentToFolder(folderId: Long, documentIdList: List) // Remove a document from a folder - suspend fun removeDocumentFromFolder(folderId: Long, documentId: Long) + suspend fun removeDocumentFromFolder(folderId: Long, documentIdList: List) // Search folders by name or tags suspend fun searchFolders(query: String): List diff --git a/src/main/kotlin/echo/org/instructure/FolderExposed.kt b/src/main/kotlin/echo/org/instructure/FolderExposed.kt index a2f63c2..7a3d14d 100644 --- a/src/main/kotlin/echo/org/instructure/FolderExposed.kt +++ b/src/main/kotlin/echo/org/instructure/FolderExposed.kt @@ -10,6 +10,7 @@ import echo.org.instructure.Folders.id import echo.org.instructure.Folders.parentFolderId import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList import org.jetbrains.exposed.sql.javatime.CurrentDateTime import org.jetbrains.exposed.sql.javatime.datetime import java.time.LocalDateTime @@ -115,12 +116,18 @@ class FolderRepositoryImpl : FolderRepository { } } - override suspend fun addDocumentToFolder(folderId: Long, documentId: Long) = dbQuery { - TODO("Not yet implemented") + override suspend fun addDocumentToFolder(folderId: Long, documentIdList: List): Unit = dbQuery { + FolderDocuments.batchInsert(documentIdList) { + this[FolderDocuments.folderId] = folderId + this[documentId] = documentId + } } - override suspend fun removeDocumentFromFolder(folderId: Long, documentId: Long) = dbQuery { - TODO("Not yet implemented") + override suspend fun removeDocumentFromFolder(folderId: Long, documentIdList: List): Unit = dbQuery { + FolderDocuments.deleteWhere { + (FolderDocuments.folderId eq folderId) and + (documentId inList documentIdList) + } } override suspend fun searchFolders(query: String): List = dbQuery { @@ -128,5 +135,4 @@ class FolderRepositoryImpl : FolderRepository { (Folders.name like "%$query%") or (Folders.tags like "%$query%") }.map { Folders.toFolder(it) } } - } \ No newline at end of file diff --git a/src/main/kotlin/echo/org/plugins/Koin.kt b/src/main/kotlin/echo/org/plugins/Koin.kt new file mode 100644 index 0000000..9fd1876 --- /dev/null +++ b/src/main/kotlin/echo/org/plugins/Koin.kt @@ -0,0 +1,27 @@ +package echo.org.plugins + +import echo.org.application.route.handleDocuments +import echo.org.domain.DocumentRepository +import echo.org.domain.FileOSS +import echo.org.instructure.AliyunOssFileServiceImpl +import echo.org.instructure.DocumentRepositoryImpl +import io.ktor.server.application.* +import io.ktor.server.http.content.* +import io.ktor.server.routing.* +import org.koin.dsl.module +import org.koin.ktor.plugin.Koin + + +val DocumentModule = module { + single { DocumentRepositoryImpl() } + single { AliyunOssFileServiceImpl() } + +} + + +fun Application.configureKoin() { + // Initialize Koin + install(Koin) { + modules(DocumentModule) + } +} \ No newline at end of file diff --git a/src/test/kotlin/echo/org/ApplicationTest.kt b/src/test/kotlin/echo/org/ApplicationTest.kt index 8c50952..527aefd 100644 --- a/src/test/kotlin/echo/org/ApplicationTest.kt +++ b/src/test/kotlin/echo/org/ApplicationTest.kt @@ -1,17 +1,12 @@ package echo.org -import echo.org.application.route.DocumentModule -import echo.org.instructure.Documents + import echo.org.plugins.* import io.ktor.client.request.* import io.ktor.client.statement.* import io.ktor.http.* -import io.ktor.server.application.* + import io.ktor.server.testing.* -import org.jetbrains.exposed.sql.SchemaUtils -import org.jetbrains.exposed.sql.transactions.transaction -import org.koin.dsl.module -import org.koin.ktor.plugin.Koin import kotlin.test.* class ApplicationTest {