From fdc26f6d72380b012c3c7f1b5e85a3c1fda67353 Mon Sep 17 00:00:00 2001 From: devxb Date: Mon, 23 Dec 2024 22:28:45 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20/users/{username}=20api=EC=9D=98=20lazy?= =?UTF-8?q?=20initialize=20=EC=97=90=EB=9F=AC=EB=A5=BC=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitanimals/render/controller/PersonaController.kt | 2 +- .../org/gitanimals/render/domain/UserRepository.kt | 11 +++++++++++ .../org/gitanimals/render/domain/UserService.kt | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt b/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt index f4917e5..b62b33e 100644 --- a/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt +++ b/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt @@ -25,7 +25,7 @@ class PersonaController( @GetMapping("/users/{username}") fun getUserByName(@PathVariable("username") username: String): UserResponse { - return UserResponse.from(userService.getUserByName(username)) + return UserResponse.from(userService.getUserByNameWithAllContributions(username)) } @GetMapping("/personas/{persona-id}") diff --git a/src/main/kotlin/org/gitanimals/render/domain/UserRepository.kt b/src/main/kotlin/org/gitanimals/render/domain/UserRepository.kt index bc8dc18..fdcf82a 100644 --- a/src/main/kotlin/org/gitanimals/render/domain/UserRepository.kt +++ b/src/main/kotlin/org/gitanimals/render/domain/UserRepository.kt @@ -1,10 +1,21 @@ package org.gitanimals.render.domain import org.springframework.data.jpa.repository.JpaRepository +import org.springframework.data.jpa.repository.Query +import org.springframework.data.repository.query.Param interface UserRepository : JpaRepository { fun findByName(name: String): User? + @Query( + """ + select u from User as u + left join fetch u.contributions + where u.name = :name + """ + ) + fun findByNameWithContributions(@Param("name") name: String): User? + fun existsByName(name: String): Boolean } diff --git a/src/main/kotlin/org/gitanimals/render/domain/UserService.kt b/src/main/kotlin/org/gitanimals/render/domain/UserService.kt index cebf7f7..fe3f56b 100644 --- a/src/main/kotlin/org/gitanimals/render/domain/UserService.kt +++ b/src/main/kotlin/org/gitanimals/render/domain/UserService.kt @@ -48,6 +48,9 @@ class UserService( fun getUserByName(name: String): User = userRepository.findByName(name) ?: throw IllegalArgumentException("Cannot find exists user by name \"$name\"") + fun getUserByNameWithAllContributions(name: String): User = userRepository.findByName(name) + ?: throw IllegalArgumentException("Cannot find exists user by name \"$name\"") + @Transactional fun createNewUser(name: String, contributions: Map): User = userRepository.save(User.newUser(name, contributions))