Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/Triumers/Newsnippet-Back
Browse files Browse the repository at this point in the history
…into user
  • Loading branch information
moomint8 committed Apr 12, 2024
2 parents 4d8ad5f + a7cadb4 commit dbca0fc
Show file tree
Hide file tree
Showing 12 changed files with 198 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
import org.triumers.newsnippetback.domain.aggregate.vo.QuizResponse;
import org.triumers.newsnippetback.domain.dto.QuizDTO;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/quiz")
Expand All @@ -22,44 +26,32 @@ public QuizController(QuizService quizService) {
this.quizService = quizService;
}

/* 1. 해당 날짜에 출제되는 문제의 카테고리, 지문, 선택지, 정답률 조회
ㄴ 문제는 10개씩, 각 날짜마다 1번-10번까지 번호 부여 */
/* 해당 날짜의 출제되는 문제의 카테고리, 지문, 선택지, 정답률, 정답, 해설, 원본 링크 조회
* ㄴ 문제는 10개씩, 각 날짜마다 1번-10번까지 번호 부여 */
@PostMapping("/test")
public ResponseEntity<QuizResponse> findQuizByDateAndNo(@RequestBody QuizRequest quizRequest) {
public ResponseEntity<List<QuizResponse>> findQuizByDateAndNo(@RequestBody QuizRequest quizRequest) {
try {
QuizDTO quizDTO = quizService.findQuizByDateAndNo(quizRequest);

QuizResponse quizResponse = new QuizResponse();
quizResponse.setContent(quizDTO.getContent());
quizResponse.setOptionA(quizDTO.getOptionA());
quizResponse.setOptionB(quizDTO.getOptionB());
quizResponse.setOptionC(quizDTO.getOptionC());
quizResponse.setOptionD(quizDTO.getOptionD());
quizResponse.setCategoryName(quizDTO.getCategoryName());
quizResponse.setCorrectRate(quizDTO.getCorrectRate());

return ResponseEntity.ok().body(quizResponse);
} catch (NoSuchElementException e) {
return ResponseEntity.notFound().build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}


/* 2. 해당 문제의 정답, 해설, 원본 링크 조회
ㄴ 문제는 10개씩, 각 날짜마다 1번-10번까지 번호 부여 */
@PostMapping("/answer")
public ResponseEntity<QuizResponse> findQuizAnswerByDateAndNo(@RequestBody QuizRequest quizRequest) {
try {
QuizDTO quizDTO = quizService.findQuizAnswerByDateAndNo(quizRequest);

QuizResponse quizResponse = new QuizResponse();
quizResponse.setAnswer(quizDTO.getAnswer());
quizResponse.setExplanation(quizDTO.getExplanation());
quizResponse.setNewsLink(quizDTO.getNewsLink());

return ResponseEntity.ok().body(quizResponse);
LocalDate date = quizRequest.getDate();
List<QuizDTO> quizDTOs = quizService.findAllQuizzesByDate(date);
List<QuizResponse> quizResponses = quizDTOs.stream()
.map(quizDTO -> {
QuizResponse quizResponse = new QuizResponse();
quizResponse.setId(quizDTO.getId());
quizResponse.setNo(quizDTO.getNo());
quizResponse.setContent(quizDTO.getContent());
quizResponse.setOptionA(quizDTO.getOptionA());
quizResponse.setOptionB(quizDTO.getOptionB());
quizResponse.setOptionC(quizDTO.getOptionC());
quizResponse.setOptionD(quizDTO.getOptionD());
quizResponse.setCategoryName(quizDTO.getCategoryName());
quizResponse.setCorrectRate(quizDTO.getCorrectRate());
quizResponse.setAnswer(quizDTO.getAnswer());
quizResponse.setExplanation(quizDTO.getExplanation());
quizResponse.setNewsLink(quizDTO.getNewsLink());
return quizResponse;
})
.collect(Collectors.toList());
return ResponseEntity.ok().body(quizResponses);
} catch (NoSuchElementException e) {
return ResponseEntity.notFound().build();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public SolvedController(SolvedService solvedService, ModelMapper modelMapper) {
this.modelMapper = modelMapper;
}

/* 설명. 1. 사용자가 입력한 답과 문제의 정답 여부 판단 */
/* 설명. 1. 사용자가 입력한 답과 문제의 정답 여부 판단 및 사용자가 입력한 답 db에 저장 */
@PostMapping("/check")
public ResponseEntity<SolvedIsCorrectResponse> findSelectedOptionAndCompareAnswer(@RequestBody SolvedRequest solvedRequest) {
try {
Expand Down Expand Up @@ -82,6 +82,7 @@ public ResponseEntity<SolvedQuizResponse> findSolvedQuizByUserIdAndQuizId(@Reque
solvedQuizResponse.setOptionC(solvedDTO.getOptionC());
solvedQuizResponse.setOptionD(solvedDTO.getOptionD());
solvedQuizResponse.setAnswer(solvedDTO.getAnswer());
solvedQuizResponse.setSelectedOption(solvedDTO.getSelectedOption());
solvedQuizResponse.setExplanation(solvedDTO.getExplanation());
solvedQuizResponse.setNewsLink(solvedDTO.getNewsLink());
solvedQuizResponse.setDate(solvedDTO.getDate());
Expand Down Expand Up @@ -112,4 +113,22 @@ public ResponseEntity<List<SolvedResultResponse>> findCorrectQuizByUserIdAndSolv
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}

@PostMapping("/find/allByDate")
public ResponseEntity<List<SolvedQuizResponse>> findSolvedQuizByUserIdAndDate(@RequestBody SolvedRequest solvedRequest){
try {
modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);

List<SolvedDTO> solvedList = solvedService.findSolvedQuizListByUserIdAndDate(solvedRequest);
List<SolvedQuizResponse> SolvedQuizListResponse = solvedList.stream()
.map(dot -> modelMapper.map(dot, SolvedQuizResponse.class))
.collect(Collectors.toList());

return ResponseEntity.ok().body(SolvedQuizListResponse);
} catch (NoSuchElementException e){
return ResponseEntity.notFound().build();
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.triumers.newsnippetback.Application.service;

import org.triumers.newsnippetback.domain.aggregate.vo.QuizRequest;
import org.triumers.newsnippetback.domain.dto.QuizDTO;

import java.time.LocalDate;
import java.util.List;

public interface QuizService {

QuizDTO findQuizByDateAndNo(QuizRequest quizRequest);
List<QuizDTO> findAllQuizzesByDate(LocalDate date);


QuizDTO findQuizAnswerByDateAndNo(QuizRequest quizRequest);
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package org.triumers.newsnippetback.Application.service;

import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.triumers.newsnippetback.domain.aggregate.entity.Category;
import org.triumers.newsnippetback.domain.aggregate.entity.Quiz;
import org.triumers.newsnippetback.domain.aggregate.vo.QuizRequest;
import org.triumers.newsnippetback.domain.dto.QuizDTO;
import org.triumers.newsnippetback.domain.repository.CategoryRepository;
import org.triumers.newsnippetback.domain.repository.QuizRepository;

import java.time.LocalDate;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.stream.Collectors;

@Service
public class QuizServiceImpl implements QuizService {
Expand All @@ -26,39 +26,6 @@ public QuizServiceImpl(QuizRepository quizRepository, CategoryRepository categor
this.categoryRepository = categoryRepository;
}

// 1
@Override
public QuizDTO findQuizByDateAndNo(QuizRequest quizRequest) {
LocalDate date = quizRequest.getDate();
int no = quizRequest.getNo();

Quiz quiz = quizRepository.findByDateAndNo(date, no);

if (quiz == null) {
throw new NoSuchElementException("Quiz not found for date: " + date + " and no: " + no);
}

QuizDTO quizDTO = new QuizDTO();
quizDTO.setContent(quiz.getContent());
quizDTO.setOptionA(quiz.getOptionA());
quizDTO.setOptionB(quiz.getOptionB());
quizDTO.setOptionC(quiz.getOptionC());
quizDTO.setOptionD(quiz.getOptionD());

// 카테고리 ID를 사용하여 카테고리 이름 조회
int categoryId = quiz.getCategoryId();
Category category = categoryRepository.findById(categoryId);
if (category == null) {
throw new NoSuchElementException("Category not found for id: " + categoryId);
}
quizDTO.setCategoryName(category.getCategoryName());

// 정답률 계산
double correctRate = calculateCorrectRate(quiz.getCorrectCnt(), quiz.getSolvedCnt());
quizDTO.setCorrectRate(correctRate);

return quizDTO;
}

// 정답률 계산 메서드
private double calculateCorrectRate(int correctCnt, int solvedCnt) {
Expand All @@ -69,24 +36,35 @@ private double calculateCorrectRate(int correctCnt, int solvedCnt) {
}


// 2
@Override
public QuizDTO findQuizAnswerByDateAndNo(QuizRequest quizRequest) {
LocalDate date = quizRequest.getDate();
int no = quizRequest.getNo();

Quiz quiz = quizRepository.findByDateAndNo(date, no);
public List<QuizDTO> findAllQuizzesByDate(LocalDate date) {
List<Quiz> quizzes = quizRepository.findAllByDate(date);
List<QuizDTO> quizDTOs = quizzes.stream()
.map(quiz -> {
QuizDTO quizDTO = new QuizDTO();
quizDTO.setId(quiz.getId());
quizDTO.setNo(quiz.getNo());
quizDTO.setContent(quiz.getContent());
quizDTO.setOptionA(quiz.getOptionA());
quizDTO.setOptionB(quiz.getOptionB());
quizDTO.setOptionC(quiz.getOptionC());
quizDTO.setOptionD(quiz.getOptionD());
quizDTO.setCategoryName(getCategoryName(quiz.getCategoryId()));
quizDTO.setCorrectRate(calculateCorrectRate(quiz.getCorrectCnt(), quiz.getSolvedCnt()));
quizDTO.setAnswer(quiz.getAnswer());
quizDTO.setExplanation(quiz.getExplanation());
quizDTO.setNewsLink(quiz.getNewsLink());
return quizDTO;
})
.collect(Collectors.toList());
return quizDTOs;
}

if (quiz == null) {
throw new NoSuchElementException("Quiz not found for date: " + date + " and no: " + no);
private String getCategoryName(int categoryId) {
Category category = categoryRepository.findById(categoryId);
if (category == null) {
throw new NoSuchElementException("Category not found for id: " + categoryId);
}

QuizDTO quizDTO = new QuizDTO();
quizDTO.setAnswer(quiz.getAnswer());
quizDTO.setExplanation(quiz.getExplanation());
quizDTO.setNewsLink(quiz.getNewsLink());

return quizDTO;
return category.getCategoryName();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ public interface SolvedService {
List<Solved> findSolvedQuizListByUserId(SolvedRequest solvedRequest);

List<Solved> findCorrectQuizByUserIdAndSolvedDate(SolvedResultRequest solvedResultRequest);

List<SolvedDTO> findSolvedQuizListByUserIdAndDate(SolvedRequest solvedRequest);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,24 @@ public SolvedDTO findSelectedOptionAndCompareAnswer(SolvedRequest solvedRequest)

int userId = solvedRequest.getUserId();
int quizId = solvedRequest.getQuizId();
String seletedOption = solvedRequest.getSelectedOption();

Solved solved = solvedRepository.findSelectedOptionByUserIdAndQuizId(userId, quizId);
Quiz answer = quizRepository.findAnswerById(quizId);

if (answer == null || solved == null) {
throw new NoSuchElementException("Quiz or Selected Option not found for quizId: " + quizId);
}

SolvedDTO solvedDTO = new SolvedDTO();
solvedDTO.setUserId(userId);
solvedDTO.setQuizId(quizId);
solvedDTO.setSolvedDate(LocalDate.now());
solvedDTO.setCorrect(Objects.equals(answer.getAnswer(), seletedOption));
solvedDTO.setSelectedOption(seletedOption);

if (Objects.equals(answer.getAnswer(), solved.getSelectedOption())) {
solvedDTO.setCorrect(true);
solved.setCorrect(true);
solvedRepository.save(solved);
Solved solved = modelMapper.map(solvedDTO, Solved.class);
solvedRepository.save(solved);

return solvedDTO;
if (answer == null || solved == null) {
throw new NoSuchElementException("Quiz or Selected Option not found for quizId: " + quizId);
}

solvedDTO.setCorrect(false);
solved.setCorrect(false);
solvedRepository.save(solved);

return solvedDTO;
}

Expand Down Expand Up @@ -128,4 +122,40 @@ public List<Solved> findCorrectQuizByUserIdAndSolvedDate(SolvedResultRequest sol
return solvedList;
}

@Override
public List<SolvedDTO> findSolvedQuizListByUserIdAndDate(SolvedRequest solvedRequest) {
System.out.println(solvedRequest);
int userId = solvedRequest.getUserId();
LocalDate solvedDate = solvedRequest.getSolvedDate();

List<Solved> solvedList = solvedRepository.findSolvedQuizByUserIdAndSolvedDate(userId, solvedDate);
List<SolvedDTO> solvedDTOList = new ArrayList<>();

for (Solved solved: solvedList) {
int id = solved.getQuizId();
Quiz quiz = quizRepository.findById(id).orElseThrow();

SolvedDTO solvedDTO = new SolvedDTO();

solvedDTO.setUserId(solved.getUserId());
solvedDTO.setQuizId(quiz.getId());
solvedDTO.setCategoryId(quiz.getCategoryId());
solvedDTO.setContent(quiz.getContent());
solvedDTO.setOptionA(quiz.getOptionA());
solvedDTO.setOptionB(quiz.getOptionB());
solvedDTO.setOptionC(quiz.getOptionC());
solvedDTO.setOptionD(quiz.getOptionD());
solvedDTO.setAnswer(quiz.getAnswer());
solvedDTO.setSelectedOption(solved.getSelectedOption());
solvedDTO.setExplanation(quiz.getExplanation());
solvedDTO.setNewsLink(quiz.getNewsLink());
solvedDTO.setDate(quiz.getDate());

solvedDTOList.add(solvedDTO);
}

return solvedDTOList;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
public class SolvedQuizResponse {
private int userId;
private int quizId;
private String selectedOption;
private int categoryId;
private String content;
private String optionA;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.*;

import java.time.LocalDate;

@NoArgsConstructor
@AllArgsConstructor
@Getter
Expand All @@ -10,4 +12,12 @@
public class SolvedRequest {
private int userId;
private int quizId;
private LocalDate solvedDate;
private String selectedOption;
private boolean isCorrect;

public SolvedRequest(int userId, int quizId) {
this.userId = userId;
this.quizId = quizId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ public interface QuizRepository extends JpaRepository<Quiz, Integer> {
Integer countByDateAndOriginQuizId(LocalDate localDate, int id);

Quiz findByOriginQuizIdAndDate(int id, LocalDate date);

List<Quiz> findAllByDate(LocalDate date);

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface SolvedRepository extends JpaRepository<Solved, Integer> {
List<Solved> findIdByUserIdAndIsCorrectAndSolvedDate(int userId, boolean isCorrect, LocalDate solvedDate);

List<Solved> findSolvedQuizByUserId(int userId);

List<Solved> findSolvedQuizByUserIdAndSolvedDate(int userId, LocalDate solvedDate);
}
Loading

0 comments on commit dbca0fc

Please sign in to comment.