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