Skip to content

Commit

Permalink
feat: List로 가지던 연관 관계 Set으로 변경, Repository 메서드 네이밍 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
nuyh99 committed Aug 10, 2023
1 parent 5657fc3 commit bf1d8ea
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package wooteco.prolog.steps;

import static org.assertj.core.api.Assertions.assertThat;
import static wooteco.prolog.fixtures.KeywordAcceptanceFixture.KEYWORD_REQUEST;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import org.springframework.http.HttpStatus;
import wooteco.prolog.AcceptanceSteps;
import wooteco.prolog.session.application.dto.SessionRequest;

import static org.assertj.core.api.Assertions.assertThat;
import static wooteco.prolog.fixtures.KeywordAcceptanceFixture.KEYWORD_REQUEST;

public class KeywordStepDefinitions extends AcceptanceSteps {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package wooteco.prolog.roadmap.application;

import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_SESSION_NOT_FOUND_EXCEPTION;

import java.util.List;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import wooteco.prolog.common.exception.BadRequestException;
Expand All @@ -15,6 +11,11 @@
import wooteco.prolog.roadmap.domain.repository.KeywordRepository;
import wooteco.prolog.session.domain.repository.SessionRepository;

import java.util.List;

import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_SESSION_NOT_FOUND_EXCEPTION;

@Transactional
@Service
public class KeywordService {
Expand Down Expand Up @@ -55,7 +56,7 @@ public KeywordResponse findKeywordWithAllChild(final Long sessionId, final Long
existSession(sessionId);
existKeyword(keywordId);

Keyword keyword = keywordRepository.findFetchById(keywordId);
Keyword keyword = keywordRepository.findFetchByIdOrderBySeq(keywordId);

return KeywordResponse.createWithAllChildResponse(keyword);
}
Expand All @@ -82,7 +83,7 @@ public void updateKeyword(final Long sessionId, final Long keywordId,

public void deleteKeyword(final Long sessionId, final Long keywordId) {
existSession(sessionId);
Keyword keyword = keywordRepository.findFetchById(keywordId);
Keyword keyword = keywordRepository.findFetchByIdOrderBySeq(keywordId);

keywordRepository.delete(keyword);
}
Expand Down
32 changes: 4 additions & 28 deletions backend/src/main/java/wooteco/prolog/roadmap/domain/Keyword.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
package wooteco.prolog.roadmap.domain;


import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_AND_KEYWORD_PARENT_SAME_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_SEQUENCE_EXCEPTION;

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -26,18 +9,11 @@

import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import javax.persistence.*;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

import javax.persistence.*;
import java.util.*;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_AND_KEYWORD_PARENT_SAME_EXCEPTION;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_SEQUENCE_EXCEPTION;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -62,8 +38,8 @@ public class Keyword {
@Column(name = "session_id", nullable = false)
private Long sessionId;

@OneToMany(mappedBy = "keyword", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.EAGER)
private List<RecommendedPost> recommendedPosts = new ArrayList<>();
@OneToMany(mappedBy = "keyword", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<RecommendedPost> recommendedPosts = new HashSet<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
package wooteco.prolog.roadmap.domain.repository;

import java.util.List;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import wooteco.prolog.roadmap.domain.Keyword;

import java.util.List;
import java.util.Optional;

import static org.springframework.data.jpa.repository.EntityGraph.EntityGraphType.FETCH;

public interface KeywordRepository extends JpaRepository<Keyword, Long> {

@EntityGraph(attributePaths = "recommendedPosts", type = FETCH)
Optional<Keyword> findById(final long id);

@EntityGraph(attributePaths = "recommendedPosts", type = FETCH)
List<Keyword> findAll();

@Query("SELECT k FROM Keyword k "
+ "LEFT JOIN FETCH k.children c "
+ "LEFT JOIN FETCH k.recommendedPosts "
+ "LEFT JOIN FETCH k.parent p "
+ "LEFT JOIN FETCH c.children lc WHERE k.id = :keywordId ORDER BY k.seq")
Keyword findFetchById(@Param("keywordId") Long keywordId);
+ "LEFT JOIN FETCH p.recommendedPosts "
+ "LEFT JOIN FETCH c.recommendedPosts "
+ "LEFT JOIN FETCH c.children lc "
+ "LEFT JOIN FETCH lc.recommendedPosts "
+ "LEFT JOIN FETCH lc.children "
+ "WHERE k.id = :keywordId ORDER BY k.seq")
Keyword findFetchByIdOrderBySeq(@Param("keywordId") Long keywordId);

@Query("SELECT k FROM Keyword k "
+ "LEFT JOIN FETCH k.recommendedPosts "
+ "WHERE k.sessionId = :sessionId AND k.parent IS NULL")
List<Keyword> findBySessionIdAndParentIsNull(@Param("sessionId") Long sessionId);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,5 @@
package wooteco.prolog.roadmap.application;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION;

import java.util.Collections;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -22,6 +13,13 @@
import wooteco.prolog.roadmap.domain.repository.KeywordRepository;
import wooteco.prolog.session.domain.repository.SessionRepository;

import java.util.Collections;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.*;
import static wooteco.prolog.common.exception.BadRequestCode.ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION;

@ExtendWith(MockitoExtension.class)
class KeywordServiceTest {

Expand Down Expand Up @@ -105,13 +103,13 @@ void findKeywordWithAllChild() {
when(keywordRepository.existsById(any())).thenReturn(true);

Keyword keyword = new Keyword(1L, "", "", 1, 1, 1L, null, Collections.emptySet());
when(keywordRepository.findFetchById(1L)).thenReturn(keyword);
when(keywordRepository.findFetchByIdOrderBySeq(1L)).thenReturn(keyword);

//when
keywordService.findKeywordWithAllChild(1L, 1L);

//then
verify(keywordRepository, times(1)).findFetchById(any());
verify(keywordRepository, times(1)).findFetchByIdOrderBySeq(any());
}

@DisplayName("sessionId로 최상위 키워드들을 찾을 수 있다")
Expand Down Expand Up @@ -178,7 +176,7 @@ void deleteKeyword() {
keywordService.deleteKeyword(1L, 1L);

//then
verify(keywordRepository, times(1)).findFetchById(any());
verify(keywordRepository, times(1)).findFetchByIdOrderBySeq(any());
verify(keywordRepository, times(1)).delete(any());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class RecommendedPostServiceTest {
@Autowired
private DataInitializer dataInitializer;


private Keyword keyword;

@BeforeEach
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package wooteco.prolog.roadmap.repository;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

import java.util.List;
import javax.persistence.EntityManager;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,6 +9,12 @@
import wooteco.prolog.session.domain.repository.SessionRepository;
import wooteco.support.utils.RepositoryTest;

import javax.persistence.EntityManager;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertAll;

@RepositoryTest
class KeywordRepositoryTest {

Expand Down Expand Up @@ -41,7 +42,7 @@ class KeywordRepositoryTest {
em.clear();

// when
Keyword extract = keywordRepository.findFetchById(keywordParentId);
Keyword extract = keywordRepository.findFetchByIdOrderBySeq(keywordParentId);

// then
assertAll(
Expand Down Expand Up @@ -73,7 +74,7 @@ class KeywordRepositoryTest {
em.clear();

// when
Keyword extract = keywordRepository.findFetchById(keywordParentId);
Keyword extract = keywordRepository.findFetchByIdOrderBySeq(keywordParentId);

// then
assertAll(
Expand Down

0 comments on commit bf1d8ea

Please sign in to comment.