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