Skip to content

Commit

Permalink
feat: 用户信息查询接口
Browse files Browse the repository at this point in the history
  • Loading branch information
dragove committed May 2, 2024
1 parent 638e5a9 commit 6d4411b
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class SecurityConfig(
"/",
"/error",
"/version",
"/user/info",
"/user/activateAccount",
"/user/password/reset_request",
"/user/password/reset",
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/plus/maa/backend/controller/UserController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.validation.Valid
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
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.RequestParam
import org.springframework.web.bind.annotation.RestController
import plus.maa.backend.config.doc.RequireJwt
import plus.maa.backend.config.security.AuthenticationHelper
Expand Down Expand Up @@ -148,4 +150,13 @@ class UserController(
@Operation(summary = "用户登录")
@ApiResponse(description = "登录结果")
fun login(@RequestBody user: @Valid LoginDTO): MaaResult<MaaLoginRsp> = success("登陆成功", userService.login(user))

/**
* 查询用户信息
*/
@GetMapping("/info")
@Operation(summary = "查询用户信息")
@ApiResponse(description = "用户详情信息")
fun getUserInfo(@RequestParam userId: String): MaaResult<MaaUserInfo> = success(userService.get(userId))

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package plus.maa.backend.controller.response.user
import plus.maa.backend.repository.entity.MaaUser

/**
* 用户可对外公开的信息
*
* @author AnselYuki
*/
data class MaaUserInfo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ data class MaaUser(
companion object {
@Transient
val UNKNOWN: MaaUser = MaaUser(
userId = "",
userName = "未知用户:(",
email = "[email protected]",
password = "unknown",
Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/plus/maa/backend/service/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class UserService(
)
return try {
userRepository.save(user).run(::MaaUserInfo)
} catch (e: DuplicateKeyException) {
} catch (_: DuplicateKeyException) {
throw MaaResultException(MaaStatusCode.MAA_USER_EXISTS)
}
}
Expand Down Expand Up @@ -224,4 +224,8 @@ class UserService(
operator fun get(id: String): MaaUser? = userMap[id]
fun getOrDefault(id: String) = get(id) ?: MaaUser.UNKNOWN
}

fun get(userId: String): MaaUserInfo =
(userRepository.findByUserId(userId) ?: MaaUser.UNKNOWN).run(::MaaUserInfo)

}

0 comments on commit 6d4411b

Please sign in to comment.