feat: 新增了一些接口的实现

getUserInformation
queryTags
putTags
getKnowledgeBrief
master
ArgonarioD 2023-07-05 13:13:45 +08:00
parent 42757aa7e8
commit 28afd457e9
9 changed files with 111 additions and 2 deletions

View File

@ -16,11 +16,14 @@ import org.apache.kafka.common.KafkaException
import org.babyfish.jimmer.kt.new import org.babyfish.jimmer.kt.new
import org.springframework.kafka.core.KafkaTemplate import org.springframework.kafka.core.KafkaTemplate
import org.springframework.transaction.annotation.Transactional 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.PostMapping
import org.springframework.web.bind.annotation.RequestBody import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController import org.springframework.web.bind.annotation.RestController
import java.time.LocalDateTime import java.time.LocalDateTime
import java.util.UUID
const val UPLOAD_FILE_TOPIC = "upload_file" const val UPLOAD_FILE_TOPIC = "upload_file"
@ -32,6 +35,10 @@ class KnowledgeController(
private val knowledgeFileAttributeRepository: KnowledgeFileAttributeRepository 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") @SaCheckRole("1")
@Transactional(rollbackFor = [Throwable::class]) @Transactional(rollbackFor = [Throwable::class])
@PostMapping("/file") @PostMapping("/file")

View File

@ -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<Tag> = tagRepository.findTagsByNameStartsWith(name)
@SaCheckRole("1")
@PutMapping
suspend fun putTags(@RequestBody tags: List<Tag>) {
val deleting = tagRepository.findAll()
.toMutableSet()
.apply {
removeAll(tags.toSet())
}
tagRepository.deleteAll(deleting)
tagRepository.saveAll(tags)
}
}

View File

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

View File

@ -28,12 +28,13 @@ interface Knowledge {
companion object { companion object {
val BRIEF_FETCHER = newFetcher(Knowledge::class).by { val BRIEF_FETCHER = newFetcher(Knowledge::class).by {
allScalarFields() allScalarFields()
knowledgeFileAttribute() knowledgeFileAttribute(KnowledgeFileAttribute.BRIEF_FETCHER)
} }
val AS_PARENT_FETCHER = newFetcher(Knowledge::class).by { val AS_PARENT_FETCHER = newFetcher(Knowledge::class).by {
allScalarFields() allScalarFields()
parent()
children(BRIEF_FETCHER) children(BRIEF_FETCHER)
knowledgeFileAttribute() knowledgeFileAttribute(KnowledgeFileAttribute.BRIEF_FETCHER)
} }
val AS_CHILD_FETCHER = newFetcher(Knowledge::class).by { val AS_CHILD_FETCHER = newFetcher(Knowledge::class).by {
allScalarFields() allScalarFields()

View File

@ -1,6 +1,7 @@
package cn.edu.hfut.auto.knowledge.entity package cn.edu.hfut.auto.knowledge.entity
import org.babyfish.jimmer.sql.* import org.babyfish.jimmer.sql.*
import org.babyfish.jimmer.sql.kt.fetcher.newFetcher
import org.babyfish.jimmer.sql.meta.UUIDIdGenerator import org.babyfish.jimmer.sql.meta.UUIDIdGenerator
import java.util.* import java.util.*
@ -28,4 +29,13 @@ interface KnowledgeFileAttribute {
@ManyToMany(mappedBy = "knowledgeFiles") @ManyToMany(mappedBy = "knowledgeFiles")
val notes: List<Note> val notes: List<Note>
companion object {
val BRIEF_FETCHER = newFetcher(KnowledgeFileAttribute::class).by {
allScalarFields()
tags {
allScalarFields()
}
}
}
} }

View File

@ -4,6 +4,7 @@ import org.babyfish.jimmer.sql.Entity
import org.babyfish.jimmer.sql.GeneratedValue import org.babyfish.jimmer.sql.GeneratedValue
import org.babyfish.jimmer.sql.Id import org.babyfish.jimmer.sql.Id
import org.babyfish.jimmer.sql.ManyToOne import org.babyfish.jimmer.sql.ManyToOne
import org.babyfish.jimmer.sql.kt.fetcher.newFetcher
import org.babyfish.jimmer.sql.meta.UUIDIdGenerator import org.babyfish.jimmer.sql.meta.UUIDIdGenerator
import java.time.LocalDateTime import java.time.LocalDateTime
import java.util.* import java.util.*
@ -15,10 +16,21 @@ interface Notice {
val id: UUID val id: UUID
val viewed: Boolean val viewed: Boolean
val createTime: LocalDateTime val createTime: LocalDateTime
@ManyToOne @ManyToOne
val note: Note val note: Note
@ManyToOne @ManyToOne
val comment: Comment val comment: Comment
@ManyToOne @ManyToOne
val targetUser: User val targetUser: User
companion object {
val BRIEF_FETCHER = newFetcher(Notice::class).by {
allScalarFields()
note { allScalarFields() }
comment { allScalarFields() }
}
}
} }

View File

@ -2,6 +2,7 @@ package cn.edu.hfut.auto.knowledge.entity
import com.fasterxml.jackson.annotation.JsonIgnore import com.fasterxml.jackson.annotation.JsonIgnore
import org.babyfish.jimmer.sql.* import org.babyfish.jimmer.sql.*
import org.babyfish.jimmer.sql.kt.fetcher.newFetcher
@Entity @Entity
@Table(name = "t_user") @Table(name = "t_user")
@ -28,4 +29,12 @@ interface User {
@ManyToMany(mappedBy = "starers") @ManyToMany(mappedBy = "starers")
val starredNotes: List<Note> val starredNotes: List<Note>
companion object {
val BRIEF_FETCHER = newFetcher(User::class).by {
allScalarFields()
password(false)
notices(Notice.BRIEF_FETCHER)
}
}
} }

View File

@ -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<Tag, Long> {
fun findTagsByNameStartsWith(name: String): List<Tag>
}

View File

@ -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<User>(StpUtil.getLoginId() as String)