feat: 修改了Knowledge相关接口的一些实现

支持了getKnowledgeBrief的null传参
修改了FileTicket的消息格式
master
ArgonarioD 2023-07-05 16:10:22 +08:00
parent 28afd457e9
commit d35157e124
5 changed files with 41 additions and 7 deletions

View File

@ -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<String, UUID?> {
override fun convert(source: String): UUID? {
if (source.isEmpty() || source.equals("null", true)) {
return null
}
return UUID.fromString(source)
}
}
}

View File

@ -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<KnowledgeDraft>)
}
}
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) {

View File

@ -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
)
}

View File

@ -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<Long>,
val parentId: UUID?
)

View File

@ -7,4 +7,6 @@ import java.util.*
interface KnowledgeRepository : KRepository<Knowledge, UUID> {
fun findByNameAndParentId(name: String, parentId: UUID?, fetcher: Fetcher<Knowledge>? = null): Knowledge?
fun findAllByParentId(parentId: UUID?, fetcher: Fetcher<Knowledge>? = null): List<Knowledge>
}