From d35157e1248bf21171f48b28ae87c19bab0a554d Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Wed, 5 Jul 2023 16:10:22 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BF=AE=E6=94=B9=E4=BA=86Knowledge?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E6=8E=A5=E5=8F=A3=E7=9A=84=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=20=E6=94=AF=E6=8C=81=E4=BA=86getKnowledgeBri?= =?UTF-8?q?ef=E7=9A=84null=E4=BC=A0=E5=8F=82=20=E4=BF=AE=E6=94=B9=E4=BA=86?= =?UTF-8?q?FileTicket=E7=9A=84=E6=B6=88=E6=81=AF=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auto/knowledge/config/SerializeConfig.kt | 19 +++++++++++++++++++ .../controller/KnowledgeController.kt | 15 ++++++++++++--- .../auto/knowledge/entity/rpc/FileTicket.kt | 10 +++++++--- .../auto/knowledge/entity/vo/KnowledgeVO.kt | 2 +- .../repository/KnowledgeRepository.kt | 2 ++ 5 files changed, 41 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/cn/edu/hfut/auto/knowledge/config/SerializeConfig.kt diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/config/SerializeConfig.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/config/SerializeConfig.kt new file mode 100644 index 0000000..e4629ae --- /dev/null +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/config/SerializeConfig.kt @@ -0,0 +1,19 @@ +package cn.edu.hfut.auto.knowledge.config + +import org.springframework.context.annotation.Configuration +import org.springframework.core.convert.converter.Converter +import org.springframework.stereotype.Component +import java.util.* + +@Configuration +class SerializeConfig { + @Component + class NullableUUIDConverter : Converter { + override fun convert(source: String): UUID? { + if (source.isEmpty() || source.equals("null", true)) { + return null + } + return UUID.fromString(source) + } + } +} \ No newline at end of file diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/KnowledgeController.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/KnowledgeController.kt index 1b7055d..10f2500 100644 --- a/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/KnowledgeController.kt +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/KnowledgeController.kt @@ -2,6 +2,7 @@ package cn.edu.hfut.auto.knowledge.controller import cn.dev33.satoken.annotation.SaCheckRole import cn.edu.hfut.auto.knowledge.entity.Knowledge +import cn.edu.hfut.auto.knowledge.entity.KnowledgeDraft import cn.edu.hfut.auto.knowledge.entity.KnowledgeFileAttribute import cn.edu.hfut.auto.knowledge.entity.by import cn.edu.hfut.auto.knowledge.entity.rpc.FileTicket @@ -36,8 +37,16 @@ class KnowledgeController( ) { @GetMapping("/{knowledgeId}") - suspend fun getKnowledgeBrief(@PathVariable knowledgeId: UUID): Knowledge = - knowledgeRepository.findNullable(knowledgeId, Knowledge.AS_PARENT_FETCHER) ?: throw BusinessError(ErrorCode.RESOURCE_NOT_FOUND) + suspend fun getKnowledgeBrief(@PathVariable knowledgeId: UUID?): Knowledge { + if (knowledgeId == null) { + return new(Knowledge::class).by { + @Suppress("UNCHECKED_CAST") + children() + .addAll(knowledgeRepository.findAllByParentId(null, Knowledge.BRIEF_FETCHER) as List) + } + } + return knowledgeRepository.findNullable(knowledgeId, Knowledge.AS_PARENT_FETCHER) ?: throw BusinessError(ErrorCode.RESOURCE_NOT_FOUND) + } @SaCheckRole("1") @Transactional(rollbackFor = [Throwable::class]) @@ -66,7 +75,7 @@ class KnowledgeController( id = knowledgeResult.id } }) - FileTicket(fileResult).run { + FileTicket(fileResult, vo.md5).run { try { kafkaTemplate.send(UPLOAD_FILE_TOPIC, this).await() } catch (e: KafkaException) { diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/rpc/FileTicket.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/rpc/FileTicket.kt index c3f96a2..882bbe3 100644 --- a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/rpc/FileTicket.kt +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/rpc/FileTicket.kt @@ -7,9 +7,11 @@ import java.util.UUID data class FileTicket( val ticket: String, - val id: UUID + val id: UUID, + val md5: String, + val size: Long ) { - constructor(knowledgeFileAttribute: KnowledgeFileAttribute): this( + constructor(knowledgeFileAttribute: KnowledgeFileAttribute, md5: String): this( buildString { knowledgeFileAttribute.run { append(id) @@ -19,6 +21,8 @@ data class FileTicket( }.let { Sha2Crypt.sha256Crypt(it.toByteArray()) }, - knowledgeFileAttribute.id + knowledgeFileAttribute.id, + md5, + knowledgeFileAttribute.size ) } diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/vo/KnowledgeVO.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/vo/KnowledgeVO.kt index b83283c..c630509 100644 --- a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/vo/KnowledgeVO.kt +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/vo/KnowledgeVO.kt @@ -6,7 +6,7 @@ data class KnowledgeFileUploadVO( val name: String, val brief: String, val size: Long, - val sha256: String, + val md5: String, val tags: List, val parentId: UUID? ) \ No newline at end of file diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/repository/KnowledgeRepository.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/repository/KnowledgeRepository.kt index 1c1b9ee..8ffb7ac 100644 --- a/src/main/kotlin/cn/edu/hfut/auto/knowledge/repository/KnowledgeRepository.kt +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/repository/KnowledgeRepository.kt @@ -7,4 +7,6 @@ import java.util.* interface KnowledgeRepository : KRepository { fun findByNameAndParentId(name: String, parentId: UUID?, fetcher: Fetcher? = null): Knowledge? + + fun findAllByParentId(parentId: UUID?, fetcher: Fetcher? = null): List } \ No newline at end of file