-
Notifications
You must be signed in to change notification settings - Fork 0
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 #51 from doghae/Feat/issue#14
Feat/issue#14
- Loading branch information
Showing
5 changed files
with
112 additions
and
2 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
src/main/java/team5/doghae/domain/question/controller/QuestionController.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,42 @@ | ||
package team5.doghae.domain.question.controller; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import team5.doghae.common.resolver.AuthUser; | ||
import team5.doghae.common.response.SuccessResponse; | ||
import team5.doghae.common.security.jwt.JwtTokenInfo; | ||
import team5.doghae.domain.question.dto.QuestionRequest; | ||
import team5.doghae.domain.question.dto.QuestionResponse; | ||
import team5.doghae.domain.question.service.QuestionService; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/question") | ||
public class QuestionController { | ||
|
||
private final QuestionService questionService; | ||
|
||
|
||
@GetMapping("/{questionId}") | ||
public ResponseEntity<SuccessResponse<QuestionResponse.Create>> getSingleWrongedQuestion( | ||
@AuthUser JwtTokenInfo jwtTokenInfo, | ||
@PathVariable("questionId") Long questionId | ||
) { | ||
return SuccessResponse.of( | ||
questionService.getSingleWrongedQuestion(jwtTokenInfo.getUserId(), questionId) | ||
).setStatus(HttpStatus.OK); | ||
} | ||
|
||
@PostMapping("/{questionId}") | ||
public ResponseEntity<SuccessResponse<QuestionResponse.Evaluate>> retryWrongedQuestion( | ||
@AuthUser JwtTokenInfo jwtTokenInfo, | ||
@PathVariable("questionId") Long questionId, | ||
@RequestBody QuestionRequest.Evaluate retryRequest | ||
) { | ||
return SuccessResponse.of( | ||
questionService.retryWrongedQuestion(jwtTokenInfo.getUserId(), questionId, retryRequest) | ||
).setStatus(HttpStatus.OK); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/team5/doghae/domain/question/service/QuestionService.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,63 @@ | ||
package team5.doghae.domain.question.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import team5.doghae.domain.question.domain.Question; | ||
import team5.doghae.domain.question.dto.QuestionRequest; | ||
import team5.doghae.domain.question.dto.QuestionResponse; | ||
import team5.doghae.domain.question.repository.QuestionRepository; | ||
import team5.doghae.domain.review.domain.Review; | ||
import team5.doghae.domain.review.repository.ReviewRepository; | ||
import team5.doghae.domain.review_question_map.domain.ReviewQuestionMap; | ||
import team5.doghae.domain.review_question_map.repository.ReviewQuestionRepository; | ||
import team5.doghae.domain.user.repository.UserRepository; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
public class QuestionService { | ||
|
||
private final QuestionRepository questionRepository; | ||
private final UserRepository userRepository; | ||
private final ReviewRepository reviewRepository; | ||
private final ReviewQuestionRepository reviewQuestionRepository; | ||
|
||
|
||
public QuestionResponse.Create getSingleWrongedQuestion(Long userId, Long questionId) { | ||
|
||
Review review = reviewRepository.findByUserId(userId) | ||
.orElseThrow(() -> new NoSuchElementException("틀린 문제가 없습니다")); | ||
|
||
Question question = questionRepository.findById(questionId) | ||
.orElseThrow(() -> new NoSuchElementException("해당 문제가 존재하지 않습니다")); | ||
|
||
ReviewQuestionMap reviewQuestionMap = reviewQuestionRepository.findByReviewAndQuestion(review, question) | ||
.orElseThrow(() -> new NoSuchElementException("틀리지 않은 문제입니다.")); | ||
|
||
return QuestionResponse.Create.of(reviewQuestionMap.getQuestion()); | ||
} | ||
|
||
@Transactional | ||
public QuestionResponse.Evaluate retryWrongedQuestion(Long userId, Long questionId, QuestionRequest.Evaluate retryRequest) { | ||
|
||
Review review = reviewRepository.findByUserId(userId) | ||
.orElseThrow(() -> new NoSuchElementException("틀린 문제가 없습니다")); | ||
|
||
Question question = questionRepository.findById(questionId) | ||
.orElseThrow(() -> new NoSuchElementException("해당 문제가 존재하지 않습니다")); | ||
|
||
ReviewQuestionMap reviewQuestionMap = reviewQuestionRepository.findByReviewAndQuestion(review, question) | ||
.orElseThrow(() -> new NoSuchElementException("잘못된 요청입니다.")); | ||
|
||
boolean isAnswer = question.getAnswer().equals(retryRequest.getAnswer()); | ||
|
||
if (isAnswer) { | ||
reviewQuestionRepository.delete(reviewQuestionMap); | ||
} | ||
|
||
return QuestionResponse.Evaluate.of(question, isAnswer); | ||
} | ||
} |
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