Skip to content

이희승 seminar1 과제#42

Open
rayark1 wants to merge 15 commits into
wafflestudio:masterfrom
rayark1:week1
Open

이희승 seminar1 과제#42
rayark1 wants to merge 15 commits into
wafflestudio:masterfrom
rayark1:week1

Conversation

@rayark1
Copy link
Copy Markdown

@rayark1 rayark1 commented Sep 30, 2023

안녕하세요~ 1주차 과제와 추가과제를 제출합니다!

  • 다음은 구현 시 고려한 사항입니다.
  1. 엔티티와 DTO 사이의 Mapping을 돕기위한 Mapper class를 추가하였습니다.
  2. 특정 엔티티 객체를 사용할 때, manytoone, onetoone의 경우 default fetchtype이 eager로 설정되어 의도치 않은 쿼리가 나가는 것을 방지 하기 위해, 기본적으로 fetch type을 lazy로 설정하였습니다.
  3. 연습을 위해 fun findByOpenTrue()에서 @EntityGraph를 사용하였습니다.
  4. controller에서 user?일 경우를 처리하기 위해서, UserNotFoundException을 추가하였습니다.
  5. 현재 db에 User가 저장되어있지 않기 때문에 playlistIntegration test 작성시 singleton 패턴을 이용하여 testuser를 생성하였습니다.
  6. cacheImpl 의 경우에는 단순 구현을 위해 스프링 어노테이션을 사용하지 않았으며, HashMap을 사용하였습니다.
  • 다음은 질문 사항들입니다!
  1. song이나 album keyword 검색 시에 대소문자가 정확하게 일치해야 검색이 가능하도록 설계가 되어있던데, 의도한 것인지 궁금합니다.
  2. Integration 테스트를 할 때, 5번과 같은 상황에서 보통 beforeeach같은 어노테이션을 사용하는지, 아니면 val username = "test-$System.currentTimeMillis()" 이런 식으로 편법(?)을 써서 테스트 하는 것 중 무엇이 좋을지가 궁금합니다.

과제 너무 재밌게 했습니다~

Copy link
Copy Markdown
Member

@PFCJeong PFCJeong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

안녕하세요~ 과제 너무 잘해주셨습니다.

  1. 네 의도하였습니다. ignoreCase 조건까지 넣지 않아도 이미 충분히 과제량이 많다고 생각했습니다.

  2. 여러 방법이 있고, 사람마다 의견이 갈리는 것 같습니다. 몇 가지 소개해드리겠습니다.

2.1. @Transactional 롤백

@Transactional
@AutoConfigureMockMvc
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class UserIntegrationTest @Autowired constructor(

2.2 @BeforeEach 사용

@BeforeEach
fun truncate() {
    // truncate database here
}

2.3 @EventListener(BeforeTestExecutionEvent::class) 사용

@Configuration
class TestConfig {

   @EventListener(BeforeTestExecutionEvent::class)
   fun truncate() {
      // truncate database here
   }
}

2.4 데이터가 안겹치게 테스트 코드 작성

val username = "test-$System.currentTimeMillis()"

감사합니다.

@Column(nullable = false)
val duration: Int,

@ManyToOne(fetch = FetchType.LAZY)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

override fun search(keyword: String): List<Song> {
TODO()
val songEntities = songRepository.findByTitleContainingWithJoinFetch(keyword)
return songEntities.map { entity -> SongMapper.toSongDTO(entity) }
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

map(SongMapper::toSongDTO) 이렇게 해줄 수도 있습니다.

import com.wafflestudio.seminar.spring2023.song.repository.ArtistEntity
import com.wafflestudio.seminar.spring2023.song.repository.SongEntity

class SongMapper {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mapper 클래스를 만드는 것도 좋은 방법입니다. 👍

import org.springframework.data.jpa.repository.Query

interface PlaylistRepository : JpaRepository<PlaylistEntity, Long> {
@Query("SELECT p FROM playlists p LEFT JOIN FETCH p.playlistSongs WHERE p.id = :id")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 딱 필요한 부분만 잘 가져오셨네요.

import org.springframework.data.jpa.repository.JpaRepository

interface PlaylistGroupRepository : JpaRepository<PlaylistGroupEntity, Long> {
@EntityGraph(attributePaths = ["playlists"])
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EntityGraph도 좋은 방법입니다 👍
다만 left join만 가능하다는 단점도 있습니다.

throw PlaylistNeverLikedException()
}

val likedPlaylist = playlistLikeRepository.findByPlaylistIdAndUserId(playlistId, userId)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exists, find 두 번의 쿼리가 발생하네요. 싱클 쿼리로도 가능할 것 같습니다.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exists 함수를 재사용하면 좋겠다는 것을 생각하다보니 쿼리 숫자를 신경쓰지 못했네요! find 쿼리로 두부분을 합쳐서 처리하는게 성능상 더 좋을 것 같습니다. 감사합니다~

@PFCJeong PFCJeong force-pushed the master branch 2 times, most recently from 1a8e813 to f420cff Compare October 3, 2023 16:41
@PFCJeong PFCJeong force-pushed the master branch 3 times, most recently from 633df04 to 66e14db Compare October 22, 2023 00:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants