优化查询逻辑

This commit is contained in:
05412 2024-07-23 11:38:53 +08:00
parent 740fd32c1f
commit 87a01e1292
3 changed files with 15 additions and 6 deletions

View File

@ -0,0 +1,7 @@
package dev.surl.surl.common
@Suppress("UNUSED")
class KeyValuePair<K, V>(
val key: K,
val value: V
)

View File

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

View File

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