-
Notifications
You must be signed in to change notification settings - Fork 0
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
Feat : 마이페이지 기능을 위한 저장기능을 구현한다 #30
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
39 changes: 39 additions & 0 deletions
39
src/main/java/com/ripple/BE/learning/domain/concept/ConceptScrap.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,39 @@ | ||
package com.ripple.BE.learning.domain.concept; | ||
|
||
import com.ripple.BE.user.domain.User; | ||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.FetchType; | ||
import jakarta.persistence.GeneratedValue; | ||
import jakarta.persistence.GenerationType; | ||
import jakarta.persistence.Id; | ||
import jakarta.persistence.JoinColumn; | ||
import jakarta.persistence.ManyToOne; | ||
import jakarta.persistence.Table; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Table(name = "concept_scraps") | ||
@Getter | ||
@Builder | ||
@Entity | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class ConceptScrap { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
@Column(name = "id", nullable = false) | ||
private Long id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private User user; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
@JoinColumn(name = "concept_id") | ||
private Concept concept; | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/ripple/BE/learning/repository/ConceptScrapRepository.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,8 @@ | ||
package com.ripple.BE.learning.repository; | ||
|
||
import com.ripple.BE.learning.domain.concept.ConceptScrap; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface ConceptScrapRepository extends JpaRepository<ConceptScrap, Long> {} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/ripple/BE/learning/repository/QuizScrapRepository.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,8 @@ | ||
package com.ripple.BE.learning.repository; | ||
|
||
import com.ripple.BE.learning.domain.quiz.QuizScrap; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface QuizScrapRepository extends JpaRepository<QuizScrap, Long> {} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,13 +6,16 @@ | |
import com.ripple.BE.learning.domain.learningset.UserLearningSet; | ||
import com.ripple.BE.learning.domain.quiz.FailQuiz; | ||
import com.ripple.BE.learning.domain.quiz.Quiz; | ||
import com.ripple.BE.learning.domain.quiz.QuizScrap; | ||
import com.ripple.BE.learning.domain.type.Type; | ||
import com.ripple.BE.learning.dto.QuizDTO; | ||
import com.ripple.BE.learning.dto.QuizListDTO; | ||
import com.ripple.BE.learning.dto.QuizResultDTO; | ||
import com.ripple.BE.learning.exception.LearningException; | ||
import com.ripple.BE.learning.exception.QuizException; | ||
import com.ripple.BE.learning.exception.errorcode.LearningErrorCode; | ||
import com.ripple.BE.learning.repository.QuizRepository; | ||
import com.ripple.BE.learning.repository.QuizScrapRepository; | ||
import com.ripple.BE.learning.repository.UserLearningSetRepository; | ||
import com.ripple.BE.learning.service.learningset.LearningSetService; | ||
import com.ripple.BE.user.domain.User; | ||
|
@@ -28,10 +31,12 @@ | |
@RequiredArgsConstructor | ||
@Service | ||
@Slf4j | ||
@Transactional(readOnly = true) | ||
public class QuizService { | ||
|
||
private final UserLearningSetRepository userLearningSetRepository; | ||
private final QuizRepository quizRepository; | ||
private final QuizScrapRepository quizScrapRepository; | ||
|
||
private final QuizRedisService quizRedisService; | ||
private final LearningSetService learningSetService; | ||
|
@@ -83,17 +88,17 @@ public QuizResultDTO submitAnswer(final long userId, final long quizId, final in | |
|
||
QuizListDTO quizListDTO = | ||
quizRedisService.fetchFromRedis(userId, QUESTION_TYPE, QuizListDTO.class); | ||
|
||
// 마이페이지의 저장한 퀴즈에서 다시풀기 진행 시 (퀴즈 진행 redis 데이터가 없을 경우) | ||
if (quizListDTO == null) { | ||
throw new LearningException(QUIZ_PROGRESS_NOT_FOUND); | ||
Quiz quiz = getQuizById(quizId); | ||
boolean isCorrect = isCorrectAnswer(QuizDTO.toQuizDTO(quiz), answerIndex); | ||
|
||
return QuizResultDTO.toQuizResultDTO(isCorrect, quiz.getExplanation()); | ||
} | ||
|
||
QuizDTO quizDTO = getQuizDTO(quizListDTO, quizId); // 퀴즈 정보 가져오기 | ||
|
||
boolean isCorrect = | ||
quizDTO | ||
.answer() | ||
.trim() | ||
.equals(quizDTO.choiceList().choices().get(answerIndex).content().trim()); | ||
boolean isCorrect = isCorrectAnswer(quizDTO, answerIndex); | ||
|
||
if (!isCorrect) { | ||
quizRedisService.saveToRedisList(userId, WRONG_ANSWER_TYPE, quizId); // 오답 리스트에 추가 | ||
|
@@ -148,4 +153,39 @@ private QuizDTO getQuizDTO(final QuizListDTO quizListDTO, final long quizId) { | |
.findFirst() | ||
.orElseThrow(() -> new LearningException(QUIZ_PROGRESS_NOT_FOUND)); | ||
} | ||
|
||
/** | ||
* 퀴즈 스크랩 | ||
* | ||
* @param userId | ||
* @param quizId | ||
*/ | ||
@Transactional | ||
public void scrapQuiz(final long userId, final long quizId) { | ||
|
||
User user = userService.findUserById(userId); | ||
Quiz quiz = | ||
quizRepository.findById(quizId).orElseThrow(() -> new QuizException(QUIZ_NOT_FOUND)); | ||
|
||
quizScrapRepository.save(QuizScrap.builder().user(user).quiz(quiz).build()); | ||
} | ||
|
||
/** | ||
* 개별 퀴즈 조회 | ||
* | ||
* @param quizId | ||
* @return 퀴즈 목록 반환 | ||
*/ | ||
public QuizDTO getSingleQuiz(final long quizId) { | ||
Quiz quiz = getQuizById(quizId); | ||
return QuizDTO.toQuizDTO(quiz); | ||
} | ||
|
||
// 정답 여부 확인 | ||
private boolean isCorrectAnswer(final QuizDTO quizDTO, final int answerIndex) { | ||
return quizDTO | ||
.answer() | ||
.trim() | ||
.equals(quizDTO.choiceList().choices().get(answerIndex).content().trim()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 메소드로 따로 뺀 부분 좋은거 같아요 |
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 이 부분은 생각 못했는데 추가해주셔서 감사합니다