Skip to content

Commit

Permalink
fix: /users/{username} api의 lazy initialize 에러를 수정한다
Browse files Browse the repository at this point in the history
  • Loading branch information
devxb committed Dec 23, 2024
1 parent 705f52f commit fdc26f6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/org/gitanimals/render/domain/UserRepository.kt
Original file line number Diff line number Diff line change
@@ -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<User, Long> {

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
}
3 changes: 3 additions & 0 deletions src/main/kotlin/org/gitanimals/render/domain/UserService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Int>): User =
userRepository.save(User.newUser(name, contributions))
Expand Down

0 comments on commit fdc26f6

Please sign in to comment.