Skip to content

Commit

Permalink
Merge pull request #51 from doghae/Feat/issue#14
Browse files Browse the repository at this point in the history
Feat/issue#14
  • Loading branch information
LSBsity authored Aug 6, 2024
2 parents 1f515a7 + 3377e8d commit d2411c3
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 2 deletions.
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
public interface ReviewRepository extends JpaRepository<Review, Long> {

Optional<Review> findByUser(User user);

Optional<Review> findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
import team5.doghae.domain.review_question_map.domain.ReviewQuestionMap;

import java.util.List;
import java.util.Optional;

public interface ReviewQuestionRepository extends JpaRepository<ReviewQuestionMap, Long> {

List<ReviewQuestionMap> findAllByReview(Review review);

List<ReviewQuestionMap> findAllByReviewAndQuestion(Review review, Question question);

Optional<ReviewQuestionMap> findByReviewAndQuestion(Review review, Question question);
}

Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public List<QuestionResponse.Evaluate> evaluateStage(Long userId, Long stageId,

if (findReview.isEmpty()) {
Review newReview = Review.of(user);
reviewRepository.save(newReview);
review = reviewRepository.save(newReview);
} else {
review = findReview.get();
}
Expand Down

0 comments on commit d2411c3

Please sign in to comment.