Skip to content

Commit

Permalink
Merge branch 'roadmap-develop' into feature/1541_roadmap_document
Browse files Browse the repository at this point in the history
# Conflicts:
#	backend/src/documentation/java/wooteco/prolog/docu/KeywordDocumentation.java
  • Loading branch information
BGuga committed Sep 25, 2023
2 parents 867bdb8 + 81c2e33 commit a88c769
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package wooteco.prolog.docu;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
Expand Down Expand Up @@ -146,17 +148,23 @@ public class KeywordDocumentation extends NewDocumentation {
"자바의 자료구조인 List에 대한 설명을 작성했습니다.",
1,
1,
0,
0,
1L,
null
emptyList(),
emptySet()
),
new KeywordResponse(
1L,
"Set",
"자바의 자료구조인 Set에 대한 설명을 작성했습니다.",
2,
1,
0,
0,
1L,
null
emptyList(),
emptySet()
))
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
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 @@ -11,11 +15,6 @@
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 @@ -51,6 +50,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 +68,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 +86,13 @@ public KeywordsResponse findSessionIncludeRootKeywords(final Long sessionId) {
return KeywordsResponse.createResponse(keywords);
}

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

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 @@ -5,17 +5,11 @@
import lombok.NoArgsConstructor;
import wooteco.prolog.roadmap.domain.Keyword;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static java.util.Collections.*;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class KeywordResponse {
Expand Down Expand Up @@ -48,13 +42,6 @@ public KeywordResponse(final Long keywordId, final String name, final String des
this.childrenKeywords = childrenKeywords;
}

public KeywordResponse(final Long keywordId, final String name, final String description,
final int order,
final int importance, final Long parentKeywordId,
final Set<KeywordResponse> childrenKeywords) {
this(keywordId, name, description, order, importance, 0, 0, parentKeywordId, emptyList(), childrenKeywords);
}

public static KeywordResponse createResponse(final Keyword keyword) {
return new KeywordResponse(
keyword.getId(),
Expand All @@ -81,7 +68,10 @@ public static KeywordResponse createWithAllChildResponse(final Keyword keyword)
keyword.getDescription(),
keyword.getSeq(),
keyword.getImportance(),
0,
0,
keyword.getParentIdOrNull(),
createRecommendedPostResponses(keyword),
createChildren(keyword.getChildren()));
}

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> newFindByParentIsNull();

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.newFindByParentIsNull();

//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

0 comments on commit a88c769

Please sign in to comment.