优化查询逻辑
This commit is contained in:
parent
740fd32c1f
commit
87a01e1292
7
src/main/java/dev/surl/surl/common/KeyValuePair.kt
Normal file
7
src/main/java/dev/surl/surl/common/KeyValuePair.kt
Normal file
@ -0,0 +1,7 @@
|
||||
package dev.surl.surl.common
|
||||
|
||||
@Suppress("UNUSED")
|
||||
class KeyValuePair<K, V>(
|
||||
val key: K,
|
||||
val value: V
|
||||
)
|
@ -1,5 +1,6 @@
|
||||
package dev.surl.surl.controller
|
||||
|
||||
import dev.surl.surl.common.KeyValuePair
|
||||
import dev.surl.surl.common.Msg
|
||||
import dev.surl.surl.service.SurlService
|
||||
import dev.surl.surl.util.JwtTokenUtil
|
||||
@ -19,7 +20,7 @@ class SurlGetController(
|
||||
* 获取用户名下短链接列表
|
||||
*/
|
||||
@GetMapping(path = ["/api/surl/get"])
|
||||
fun getUrlsByUser(@RequestHeader headers: HttpHeaders): Msg<List<String>> {
|
||||
fun getUrlsByUser(@RequestHeader headers: HttpHeaders): Msg<List<KeyValuePair<String, String>>> {
|
||||
|
||||
// 从认证头获取用户名
|
||||
val username = jwtTokenUtil.getUsernameFromHeader(headers)
|
||||
|
@ -1,7 +1,9 @@
|
||||
package dev.surl.surl.service
|
||||
|
||||
import dev.surl.surl.common.KeyValuePair
|
||||
import dev.surl.surl.dao.Surl
|
||||
import dev.surl.surl.dsl.Surls
|
||||
import dev.surl.surl.dsl.Users
|
||||
import dev.surl.surl.util.*
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.jetbrains.exposed.sql.batchInsert
|
||||
@ -60,13 +62,12 @@ class SurlService {
|
||||
* 根据用户名获取短链接列表
|
||||
* @param username 用户名
|
||||
*/
|
||||
fun getUrlsByUser(username: String): List<String> {
|
||||
val user = userService.getUserByUsername(username) ?: return emptyList()
|
||||
fun getUrlsByUser(username: String): List<KeyValuePair<String, String>> {
|
||||
return transaction {
|
||||
Surl.find {
|
||||
Surls.user eq user.id
|
||||
Users.leftJoin(Surls).select(Surls.id, Surls.url).where {
|
||||
Users.username eq username
|
||||
}.map {
|
||||
it.url
|
||||
KeyValuePair(numberToKey(it[Surls.id].value), it[Surls.url])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user