Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor : 엔드포인트에서 사용하지 않는 session ID가 있으면, 없애고 새로운 엔드포인트 생성 #1549

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ public KeywordResponse findKeyword(final Long sessionId, final Long keywordId) {
return KeywordResponse.createResponse(keyword);
}

@Transactional(readOnly = true)
public KeywordResponse newFindKeyword(final Long keywordId) {
Keyword keyword = keywordRepository.findById(keywordId)
.orElseThrow(() -> new BadRequestException(ROADMAP_KEYWORD_NOT_FOUND_EXCEPTION));

return KeywordResponse.createResponse(keyword);
}

@Transactional(readOnly = true)
public KeywordResponse findKeywordWithAllChild(final Long sessionId, final Long keywordId) {
existSession(sessionId);
Expand All @@ -61,6 +69,15 @@ public KeywordResponse findKeywordWithAllChild(final Long sessionId, final Long
return KeywordResponse.createWithAllChildResponse(keyword);
}

@Transactional(readOnly = true)
public KeywordResponse newFindKeywordWithAllChild(final Long keywordId) {
existKeyword(keywordId);

Keyword keyword = keywordRepository.findFetchByIdOrderBySeq(keywordId);

return KeywordResponse.createWithAllChildResponse(keyword);
}

@Transactional(readOnly = true)
public KeywordsResponse findSessionIncludeRootKeywords(final Long sessionId) {
existSession(sessionId);
Expand All @@ -70,6 +87,13 @@ public KeywordsResponse findSessionIncludeRootKeywords(final Long sessionId) {
return KeywordsResponse.createResponse(keywords);
}

@Transactional(readOnly = true)
public KeywordsResponse newFindSessionIncludeRootKeywords() {
List<Keyword> keywords = keywordRepository.newFindParentIsNull();

return KeywordsResponse.createResponse(keywords);
}

public void updateKeyword(final Long sessionId, final Long keywordId,
final KeywordUpdateRequest request) {
existSession(sessionId); // 세션이 없다면 예외가 발생
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ public interface KeywordRepository extends JpaRepository<Keyword, Long> {
+ "WHERE k.sessionId = :sessionId AND k.parent IS NULL")
List<Keyword> findBySessionIdAndParentIsNull(@Param("sessionId") Long sessionId);

@Query("SELECT k FROM Keyword k "
+ "LEFT JOIN FETCH k.recommendedPosts "
+ "WHERE k.parent IS NULL")
List<Keyword> newFindParentIsNull();

List<Keyword> findBySessionIdIn(final Set<Long> sessionIds);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wooteco.prolog.roadmap.application.KeywordService;
import wooteco.prolog.roadmap.application.dto.KeywordCreateRequest;
Expand All @@ -17,7 +16,6 @@
import wooteco.prolog.roadmap.application.dto.KeywordsResponse;

@RestController
@RequestMapping("/sessions/{sessionId}/keywords")
public class KeywordController {

private final KeywordService keywordService;
Expand All @@ -26,47 +24,68 @@ public KeywordController(final KeywordService keywordService) {
this.keywordService = keywordService;
}

@PostMapping
@PostMapping("/sessions/{sessionId}/keywords")
public ResponseEntity<Void> createKeyword(@PathVariable Long sessionId,
@RequestBody KeywordCreateRequest createRequest) {
@RequestBody KeywordCreateRequest createRequest) {
Long keywordId = keywordService.createKeyword(sessionId, createRequest);
return ResponseEntity.created(
URI.create("/sessions/" + sessionId + "/keywords/" + keywordId)).build();
}

@GetMapping("/{keywordId}")
@GetMapping("/sessions/{sessionId}/keywords/{keywordId}")
public ResponseEntity<KeywordResponse> findKeyword(@PathVariable Long sessionId,
@PathVariable Long keywordId) {
@PathVariable Long keywordId) {
KeywordResponse response = keywordService.findKeyword(sessionId, keywordId);
return ResponseEntity.ok(response);
}

@PutMapping("/{keywordId}")
@GetMapping("/keywords/{keywordId}")
public ResponseEntity<KeywordResponse> newFindKeyword(
@PathVariable Long keywordId
) {
KeywordResponse response = keywordService.newFindKeyword(keywordId);
return ResponseEntity.ok(response);
}

@PutMapping("/sessions/{sessionId}/keywords/{keywordId}")
public ResponseEntity<Void> updateKeyword(@PathVariable Long sessionId,
@PathVariable Long keywordId,
@RequestBody KeywordUpdateRequest updateRequest) {
@PathVariable Long keywordId,
@RequestBody KeywordUpdateRequest updateRequest) {
keywordService.updateKeyword(sessionId, keywordId, updateRequest);
return ResponseEntity.noContent().build();
}

@DeleteMapping("/{keywordId}")
@DeleteMapping("/sessions/{sessionId}/keywords/{keywordId}")
public ResponseEntity<Void> deleteKeyword(@PathVariable Long sessionId,
@PathVariable Long keywordId) {
@PathVariable Long keywordId) {
keywordService.deleteKeyword(sessionId, keywordId);
return ResponseEntity.noContent().build();
}

@GetMapping
@GetMapping("/sessions/{sessionId}/keywords")
public ResponseEntity<KeywordsResponse> findSessionIncludeRootKeywords(
@PathVariable Long sessionId) {
@PathVariable Long sessionId
) {
KeywordsResponse response = keywordService.findSessionIncludeRootKeywords(sessionId);
return ResponseEntity.ok(response);
}

@GetMapping("/{keywordId}/children")
@GetMapping("/keywords")
public ResponseEntity<KeywordsResponse> newFindSessionIncludeRootKeywords() {
KeywordsResponse response = keywordService.newFindSessionIncludeRootKeywords();
return ResponseEntity.ok(response);
}

@GetMapping("/sessions/{sessionId}/keywords/{keywordId}/children")
public ResponseEntity<KeywordResponse> find(@PathVariable Long sessionId,
@PathVariable Long keywordId) {
@PathVariable Long keywordId) {
KeywordResponse response = keywordService.findKeywordWithAllChild(sessionId, keywordId);
return ResponseEntity.ok(response);
}

@GetMapping("/keywords/{keywordId}/children")
public ResponseEntity<KeywordResponse> newFind(@PathVariable Long keywordId) {
KeywordResponse response = keywordService.newFindKeywordWithAllChild(keywordId);
return ResponseEntity.ok(response);
}
}
48 changes: 31 additions & 17 deletions backend/src/main/java/wooteco/prolog/roadmap/ui/QuizController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import wooteco.prolog.login.domain.AuthMemberPrincipal;
import wooteco.prolog.login.ui.LoginMember;
Expand All @@ -20,49 +20,63 @@

@RestController
@AllArgsConstructor
@RequestMapping("/sessions/{sessionId}/keywords/{keywordId}/quizs")
public class QuizController {

private final QuizService quizService;

//: todo admin login 생기면 검증 추가
@PostMapping
@PostMapping("/sessions/{sessionId}/keywords/{keywordId}/quizs")
public ResponseEntity<Void> create(@PathVariable Long sessionId, @PathVariable Long keywordId,
@RequestBody QuizRequest quizRequest) {
@RequestBody QuizRequest quizRequest) {
final Long quizId = quizService.createQuiz(keywordId, quizRequest);

return ResponseEntity.created(
URI.create("/sessions/" + sessionId + "/keywords/" + keywordId + "/quizs/" + quizId))
.build();
}

@GetMapping("/{quizId}")
@GetMapping("/sessions/{sessionId}/keywords/{keywordId}/quizs/{quizId}")
public ResponseEntity<QuizResponse> findQuizById(@PathVariable Long quizId,
@AuthMemberPrincipal LoginMember member) {
@AuthMemberPrincipal LoginMember member) {
return ResponseEntity.ok(quizService.findById(quizId, member.getId()));
}

@GetMapping
@GetMapping("/quizs/{quizId}")
public ResponseEntity<QuizResponse> newFindQuizById(
@PathVariable Long quizId,
@AuthMemberPrincipal LoginMember member
) {
return ResponseEntity.ok(quizService.findById(quizId, member.getId()));
}

@GetMapping("/sessions/{sessionId}/keywords/{keywordId}/quizs")
public ResponseEntity<QuizzesResponse> findQuizzesByKeyword(@PathVariable Long sessionId,
@PathVariable Long keywordId,
@AuthMemberPrincipal LoginMember member) {
@PathVariable Long keywordId,
@AuthMemberPrincipal LoginMember member) {
return ResponseEntity.ok(quizService.findQuizzesByKeywordId(keywordId, member.getId()));
}

@PutMapping("/{quizId}")
@GetMapping("/quizs")
public ResponseEntity<QuizzesResponse> newFindQuizzesByKeyword(
@RequestParam("keyword-id") Long keywordId,
@AuthMemberPrincipal LoginMember member
) {
return ResponseEntity.ok(quizService.findQuizzesByKeywordId(keywordId, member.getId()));
}

@PutMapping("/sessions/{sessionId}/keywords/{keywordId}/quizs/{quizId}")
ResponseEntity<Void> updateQuiz(@PathVariable Long sessionId,
@PathVariable Long keywordId,
@PathVariable Long quizId,
@RequestBody QuizRequest quizRequest) {
@PathVariable Long keywordId,
@PathVariable Long quizId,
@RequestBody QuizRequest quizRequest) {
quizService.updateQuiz(quizId, quizRequest);
return ResponseEntity.noContent().build();
}


@DeleteMapping("/{quizId}")
@DeleteMapping("/sessions/{sessionId}/keywords/{keywordId}/quizs/{quizId}")
public ResponseEntity<Void> deleteQuiz(@PathVariable Long sessionId,
@PathVariable Long keywordId,
@PathVariable Long quizId) {
@PathVariable Long keywordId,
@PathVariable Long quizId) {
quizService.deleteQuiz(quizId);
return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,49 @@ void findBySessionIdIn() {
.isEqualTo(Arrays.asList(keyword1, keyword2, keyword3, keyword4, keyword9, keyword10));
}

@Test
@DisplayName("newFindParentIsNull() : 모든 상위 키워드들을 조회할 수 있다.")
void newFindParentIsNull() {
//given
final Curriculum curriculum = curriculumRepository.save(new Curriculum("커리큘럼1"));

final Session session1 = sessionRepository.save(new Session(curriculum.getId(), "세션1"));
final Session session2 = sessionRepository.save(new Session(curriculum.getId(), "세션2"));
final Session session3 = sessionRepository.save(new Session(curriculum.getId(), "세션3"));
final Session session4 = sessionRepository.save(new Session(curriculum.getId(), "세션4"));
final Session session5 = sessionRepository.save(new Session(curriculum.getId(), "세션5"));

final Keyword keyword1 = keywordRepository.save(
Keyword.createKeyword("자바1", "자바 설명1", 1, 5, session1.getId(), null));
final Keyword keyword2 = keywordRepository.save(
Keyword.createKeyword("자바2", "자바 설명2", 2, 5, session1.getId(), keyword1));
final Keyword keyword3 = keywordRepository.save(
Keyword.createKeyword("자바3", "자바 설명3", 3, 5, session1.getId(), null));
final Keyword keyword4 = keywordRepository.save(
Keyword.createKeyword("자바4", "자바 설명4", 4, 5, session1.getId(), keyword3));
final Keyword keyword5 = keywordRepository.save(
Keyword.createKeyword("자바5", "자바 설명5", 5, 5, session2.getId(), null));
final Keyword keyword6 = keywordRepository.save(
Keyword.createKeyword("자바6", "자바 설명6", 6, 5, session2.getId(), keyword1));
final Keyword keyword7 = keywordRepository.save(
Keyword.createKeyword("자바7", "자바 설명7", 7, 5, session2.getId(), null));
final Keyword keyword8 = keywordRepository.save(
Keyword.createKeyword("자바8", "자바 설명8", 8, 5, session3.getId(), keyword2));
final Keyword keyword9 = keywordRepository.save(
Keyword.createKeyword("자바9", "자바 설명9", 9, 5, session4.getId(), keyword2));
final Keyword keyword10 = keywordRepository.save(
Keyword.createKeyword("자바10", "자바 설명10", 10, 5, session5.getId(), null));

//when
final List<Keyword> keywords = keywordRepository.newFindParentIsNull();

//then
assertThat(keywords)
.usingRecursiveComparison()
.ignoringFields("id", "parent.id")
.isEqualTo(Arrays.asList(keyword1, keyword3, keyword5, keyword7, keyword10));
}

private Keyword createKeywordParent(final Keyword keyword) {
return keywordRepository.save(keyword);
}
Expand Down