From 28afd457e98e50c8cac0b0c5353419e021509021 Mon Sep 17 00:00:00 2001 From: ArgonarioD Date: Wed, 5 Jul 2023 13:13:45 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E6=8E=A5=E5=8F=A3=E7=9A=84=E5=AE=9E=E7=8E=B0=20getUse?= =?UTF-8?q?rInformation=20queryTags=20putTags=20getKnowledgeBrief?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/KnowledgeController.kt | 7 +++++ .../knowledge/controller/TagController.kt | 27 +++++++++++++++++++ .../knowledge/controller/UserController.kt | 25 +++++++++++++++++ .../hfut/auto/knowledge/entity/Knowledge.kt | 5 ++-- .../entity/KnowledgeFileAttribute.kt | 10 +++++++ .../edu/hfut/auto/knowledge/entity/Notice.kt | 12 +++++++++ .../cn/edu/hfut/auto/knowledge/entity/User.kt | 9 +++++++ .../knowledge/repository/TagRepository.kt | 8 ++++++ .../auto/knowledge/util/PermissionUtils.kt | 10 +++++++ 9 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/TagController.kt create mode 100644 src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/UserController.kt create mode 100644 src/main/kotlin/cn/edu/hfut/auto/knowledge/repository/TagRepository.kt create mode 100644 src/main/kotlin/cn/edu/hfut/auto/knowledge/util/PermissionUtils.kt 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 b011dc4..1b7055d 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 @@ -16,11 +16,14 @@ import org.apache.kafka.common.KafkaException import org.babyfish.jimmer.kt.new import org.springframework.kafka.core.KafkaTemplate import org.springframework.transaction.annotation.Transactional +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.PostMapping import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController import java.time.LocalDateTime +import java.util.UUID const val UPLOAD_FILE_TOPIC = "upload_file" @@ -32,6 +35,10 @@ class KnowledgeController( private val knowledgeFileAttributeRepository: KnowledgeFileAttributeRepository ) { + @GetMapping("/{knowledgeId}") + suspend fun getKnowledgeBrief(@PathVariable knowledgeId: UUID): Knowledge = + knowledgeRepository.findNullable(knowledgeId, Knowledge.AS_PARENT_FETCHER) ?: throw BusinessError(ErrorCode.RESOURCE_NOT_FOUND) + @SaCheckRole("1") @Transactional(rollbackFor = [Throwable::class]) @PostMapping("/file") diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/TagController.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/TagController.kt new file mode 100644 index 0000000..4d642b3 --- /dev/null +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/TagController.kt @@ -0,0 +1,27 @@ +package cn.edu.hfut.auto.knowledge.controller + +import cn.dev33.satoken.annotation.SaCheckRole +import cn.edu.hfut.auto.knowledge.entity.Tag +import cn.edu.hfut.auto.knowledge.repository.TagRepository +import org.springframework.web.bind.annotation.* + +@RequestMapping("/tag") +@RestController +class TagController( + private val tagRepository: TagRepository +) { + @GetMapping + suspend fun queryTags(name: String): List = tagRepository.findTagsByNameStartsWith(name) + + @SaCheckRole("1") + @PutMapping + suspend fun putTags(@RequestBody tags: List) { + val deleting = tagRepository.findAll() + .toMutableSet() + .apply { + removeAll(tags.toSet()) + } + tagRepository.deleteAll(deleting) + tagRepository.saveAll(tags) + } +} \ No newline at end of file diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/UserController.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/UserController.kt new file mode 100644 index 0000000..0af7999 --- /dev/null +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/controller/UserController.kt @@ -0,0 +1,25 @@ +package cn.edu.hfut.auto.knowledge.controller + +import cn.edu.hfut.auto.knowledge.entity.User +import cn.edu.hfut.auto.knowledge.exception.BusinessError +import cn.edu.hfut.auto.knowledge.exception.ErrorCode +import cn.edu.hfut.auto.knowledge.repository.UserRepository +import cn.edu.hfut.auto.knowledge.util.getLoginUser +import com.fasterxml.jackson.databind.ObjectMapper +import org.springframework.web.bind.annotation.GetMapping +import org.springframework.web.bind.annotation.PathVariable +import org.springframework.web.bind.annotation.RequestMapping +import org.springframework.web.bind.annotation.RestController + +@RequestMapping("/user") +@RestController +class UserController( + private val userRepository: UserRepository, + private val objectMapper: ObjectMapper +) { + @GetMapping(path = ["", "/{userId}"]) + suspend fun getUserInformation(@PathVariable(required = false) userId: Long?): User = + userRepository.findNullable(userId ?: getLoginUser(objectMapper).id, User.BRIEF_FETCHER) + ?: throw BusinessError(ErrorCode.RESOURCE_NOT_FOUND) + +} \ No newline at end of file diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/Knowledge.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/Knowledge.kt index 969ef9d..55ba131 100644 --- a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/Knowledge.kt +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/Knowledge.kt @@ -28,12 +28,13 @@ interface Knowledge { companion object { val BRIEF_FETCHER = newFetcher(Knowledge::class).by { allScalarFields() - knowledgeFileAttribute() + knowledgeFileAttribute(KnowledgeFileAttribute.BRIEF_FETCHER) } val AS_PARENT_FETCHER = newFetcher(Knowledge::class).by { allScalarFields() + parent() children(BRIEF_FETCHER) - knowledgeFileAttribute() + knowledgeFileAttribute(KnowledgeFileAttribute.BRIEF_FETCHER) } val AS_CHILD_FETCHER = newFetcher(Knowledge::class).by { allScalarFields() diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/KnowledgeFileAttribute.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/KnowledgeFileAttribute.kt index 02edafd..5f00ff2 100644 --- a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/KnowledgeFileAttribute.kt +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/KnowledgeFileAttribute.kt @@ -1,6 +1,7 @@ package cn.edu.hfut.auto.knowledge.entity import org.babyfish.jimmer.sql.* +import org.babyfish.jimmer.sql.kt.fetcher.newFetcher import org.babyfish.jimmer.sql.meta.UUIDIdGenerator import java.util.* @@ -28,4 +29,13 @@ interface KnowledgeFileAttribute { @ManyToMany(mappedBy = "knowledgeFiles") val notes: List + + companion object { + val BRIEF_FETCHER = newFetcher(KnowledgeFileAttribute::class).by { + allScalarFields() + tags { + allScalarFields() + } + } + } } \ No newline at end of file diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/Notice.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/Notice.kt index 74ce10b..e1534e2 100644 --- a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/Notice.kt +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/Notice.kt @@ -4,6 +4,7 @@ import org.babyfish.jimmer.sql.Entity import org.babyfish.jimmer.sql.GeneratedValue import org.babyfish.jimmer.sql.Id import org.babyfish.jimmer.sql.ManyToOne +import org.babyfish.jimmer.sql.kt.fetcher.newFetcher import org.babyfish.jimmer.sql.meta.UUIDIdGenerator import java.time.LocalDateTime import java.util.* @@ -15,10 +16,21 @@ interface Notice { val id: UUID val viewed: Boolean val createTime: LocalDateTime + @ManyToOne val note: Note + @ManyToOne val comment: Comment + @ManyToOne val targetUser: User + + companion object { + val BRIEF_FETCHER = newFetcher(Notice::class).by { + allScalarFields() + note { allScalarFields() } + comment { allScalarFields() } + } + } } \ No newline at end of file diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/User.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/User.kt index e9f95ec..de0ca90 100644 --- a/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/User.kt +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/entity/User.kt @@ -2,6 +2,7 @@ package cn.edu.hfut.auto.knowledge.entity import com.fasterxml.jackson.annotation.JsonIgnore import org.babyfish.jimmer.sql.* +import org.babyfish.jimmer.sql.kt.fetcher.newFetcher @Entity @Table(name = "t_user") @@ -28,4 +29,12 @@ interface User { @ManyToMany(mappedBy = "starers") val starredNotes: List + + companion object { + val BRIEF_FETCHER = newFetcher(User::class).by { + allScalarFields() + password(false) + notices(Notice.BRIEF_FETCHER) + } + } } \ No newline at end of file diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/repository/TagRepository.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/repository/TagRepository.kt new file mode 100644 index 0000000..27879d7 --- /dev/null +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/repository/TagRepository.kt @@ -0,0 +1,8 @@ +package cn.edu.hfut.auto.knowledge.repository + +import cn.edu.hfut.auto.knowledge.entity.Tag +import org.babyfish.jimmer.spring.repository.KRepository + +interface TagRepository : KRepository { + fun findTagsByNameStartsWith(name: String): List +} \ No newline at end of file diff --git a/src/main/kotlin/cn/edu/hfut/auto/knowledge/util/PermissionUtils.kt b/src/main/kotlin/cn/edu/hfut/auto/knowledge/util/PermissionUtils.kt new file mode 100644 index 0000000..10d88b9 --- /dev/null +++ b/src/main/kotlin/cn/edu/hfut/auto/knowledge/util/PermissionUtils.kt @@ -0,0 +1,10 @@ +package cn.edu.hfut.auto.knowledge.util + +import cn.dev33.satoken.stp.StpUtil +import cn.edu.hfut.auto.knowledge.entity.User +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.module.kotlin.readValue + +@Suppress("NOTHING_TO_INLINE") +inline fun getLoginUser(objectMapper: ObjectMapper): User = + objectMapper.readValue(StpUtil.getLoginId() as String) \ No newline at end of file