-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1472 from woowacourse/feature/1461-로드맵_키워드_반환_기능_추가
feat : 커리큘럼 ID로 키워드들 조회 API 구현
- Loading branch information
Showing
7 changed files
with
201 additions
and
2 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
backend/src/main/java/wooteco/prolog/roadmap/application/RoadMapService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package wooteco.prolog.roadmap.application; | ||
|
||
import static wooteco.prolog.common.exception.BadRequestCode.CURRICULUM_NOT_FOUND_EXCEPTION; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import wooteco.prolog.common.exception.BadRequestException; | ||
import wooteco.prolog.roadmap.application.dto.KeywordsResponse; | ||
import wooteco.prolog.roadmap.domain.Curriculum; | ||
import wooteco.prolog.roadmap.domain.Keyword; | ||
import wooteco.prolog.roadmap.domain.repository.CurriculumRepository; | ||
import wooteco.prolog.roadmap.domain.repository.KeywordRepository; | ||
import wooteco.prolog.session.domain.Session; | ||
import wooteco.prolog.session.domain.repository.SessionRepository; | ||
|
||
@RequiredArgsConstructor | ||
@Transactional | ||
@Service | ||
public class RoadMapService { | ||
|
||
private final CurriculumRepository curriculumRepository; | ||
private final SessionRepository sessionRepository; | ||
private final KeywordRepository keywordRepository; | ||
|
||
@Transactional(readOnly = true) | ||
public KeywordsResponse findAllKeywords(final Long curriculumId) { | ||
final Curriculum curriculum = curriculumRepository.findById(curriculumId) | ||
.orElseThrow(() -> new BadRequestException(CURRICULUM_NOT_FOUND_EXCEPTION)); | ||
|
||
final Set<Long> sessionIds = sessionRepository.findAllByCurriculumId(curriculum.getId()) | ||
.stream() | ||
.map(Session::getId) | ||
.collect(Collectors.toSet()); | ||
|
||
final List<Keyword> keywords = keywordRepository.findBySessionIdIn(sessionIds); | ||
|
||
return KeywordsResponse.createResponseWithChildren(keywords); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
backend/src/main/java/wooteco/prolog/roadmap/ui/RoadmapController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package wooteco.prolog.roadmap.ui; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import wooteco.prolog.roadmap.application.RoadMapService; | ||
import wooteco.prolog.roadmap.application.dto.KeywordsResponse; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class RoadmapController { | ||
|
||
private final RoadMapService roadMapService; | ||
|
||
@GetMapping("/roadmaps") | ||
public KeywordsResponse findRoadMapKeyword(@RequestParam final Long curriculumId) { | ||
return roadMapService.findAllKeywords(curriculumId); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
backend/src/test/java/wooteco/prolog/roadmap/application/RoadMapServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package wooteco.prolog.roadmap.application; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyLong; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import wooteco.prolog.roadmap.application.dto.KeywordsResponse; | ||
import wooteco.prolog.roadmap.domain.Curriculum; | ||
import wooteco.prolog.roadmap.domain.Keyword; | ||
import wooteco.prolog.roadmap.domain.repository.CurriculumRepository; | ||
import wooteco.prolog.roadmap.domain.repository.KeywordRepository; | ||
import wooteco.prolog.session.domain.Session; | ||
import wooteco.prolog.session.domain.repository.SessionRepository; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class RoadMapServiceTest { | ||
|
||
@Mock | ||
private CurriculumRepository curriculumRepository; | ||
@Mock | ||
private SessionRepository sessionRepository; | ||
@Mock | ||
private KeywordRepository keywordRepository; | ||
@InjectMocks | ||
private RoadMapService roadMapService; | ||
|
||
@Test | ||
@DisplayName("curriculumId가 주어지면 해당 커리큘럼의 키워드들을 전부 조회할 수 있다.") | ||
void findAllKeywords() { | ||
//given | ||
final Curriculum curriculum = new Curriculum(1L, "커리큘럼1"); | ||
final Session session = new Session(1L, curriculum.getId(), "세션1"); | ||
final List<Session> sessions = Arrays.asList(session); | ||
final Keyword keyword = new Keyword(1L, "자바1", "자바 설명1", 1, 5, session.getId(), | ||
null, Collections.emptySet()); | ||
|
||
when(curriculumRepository.findById(anyLong())) | ||
.thenReturn(Optional.of(curriculum)); | ||
|
||
when(sessionRepository.findAllByCurriculumId(anyLong())) | ||
.thenReturn(sessions); | ||
|
||
when(keywordRepository.findBySessionIdIn(any())) | ||
.thenReturn(Arrays.asList(keyword)); | ||
|
||
final KeywordsResponse expected = KeywordsResponse.createResponseWithChildren(Arrays.asList(keyword)); | ||
|
||
//when | ||
final KeywordsResponse actual = | ||
roadMapService.findAllKeywords(curriculum.getId()); | ||
|
||
//then | ||
assertThat(actual) | ||
.usingRecursiveComparison() | ||
.isEqualTo(expected); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters