feat: report 조회 기능 추가

This commit is contained in:
박정민 2024-08-06 16:32:45 +09:00
parent 8474cc9c18
commit d8b236960f
10 changed files with 207 additions and 32 deletions

View File

@ -1,37 +1,62 @@
package com.edufocus.edufocus.report.controller;
import com.edufocus.edufocus.report.entity.dto.ReportDetailResponseDto;
import com.edufocus.edufocus.report.entity.dto.ReportListResponseDto;
import com.edufocus.edufocus.report.entity.dto.ReportResponse;
import com.edufocus.edufocus.report.entity.vo.Report;
import com.edufocus.edufocus.report.entity.dto.ReportRequset;
import com.edufocus.edufocus.report.service.ReportService;
import com.edufocus.edufocus.user.util.JWTUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/report")
@Slf4j
@RequiredArgsConstructor
public class ReportController{
public class ReportController {
private final ReportService reportService;
private final JWTUtil jwtUtil;
@PostMapping("/submit/{quizsetId}")
public ResponseEntity<ReportResponse> submit(@PathVariable("quizsetId") Long quizestId, @RequestBody ReportRequset reportRequset, HttpServletRequest request) throws SQLException {
String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token));
@PostMapping("/submit")
public ResponseEntity<ReportResponse> submit(@RequestBody ReportRequset reportRequset) throws SQLException {
ReportResponse report = reportService.grading(reportRequset);
return new ResponseEntity<>(report,HttpStatus.CREATED);
ReportResponse report = reportService.grading(userId, quizestId, reportRequset);
return new ResponseEntity<>(report, HttpStatus.CREATED);
}
@GetMapping("/myreport/{id}")
public ResponseEntity<List<ReportListResponseDto>> reportList(@PathVariable Long id, HttpServletRequest request) throws SQLException {
String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token));
List<ReportListResponseDto> reportList = reportService.resultList(userId);
return new ResponseEntity<>(reportList, HttpStatus.CREATED);
}
@GetMapping("/detailreport/{id}")
public ResponseEntity<ReportDetailResponseDto> myReport(@PathVariable("id") Long reportId) throws SQLException {
ReportDetailResponseDto detailReport = reportService.reportDetail(reportId);
return new ResponseEntity<>(detailReport, HttpStatus.CREATED);
}
}

View File

@ -5,7 +5,8 @@ import lombok.Getter;
@Getter
public class AnswerInput {
Long answerinputID;
String answer;
}

View File

@ -0,0 +1,21 @@
package com.edufocus.edufocus.report.entity.dto;
import com.edufocus.edufocus.quiz.entity.Quiz;
import lombok.Builder;
import lombok.Data;
import java.util.Date;
import java.util.List;
@Data
@Builder
public class ReportDetailResponseDto {
private int allCount;
private String title;
private int correctCount;
private Date testAt;
private List<Quiz> correctQuizzes;
private List<Quiz> incorrectQuizzes;
}

View File

@ -0,0 +1,14 @@
package com.edufocus.edufocus.report.entity.dto;
import lombok.Builder;
import lombok.Data;
import java.util.Date;
@Data
@Builder
public class ReportListResponseDto {
private String title;
private Date date;
private Long reportId;
}

View File

@ -10,11 +10,9 @@ import java.util.List;
public class ReportRequset {
private Long userId;
private Long quizsetId;
List<AnswerInput> answerInputList;
// [1,2,,3]
// List<answerDto> a
// userID :
// quizSetId :

View File

@ -3,5 +3,9 @@ package com.edufocus.edufocus.report.repository;
import com.edufocus.edufocus.report.entity.vo.Answer;
import org.springframework.data.jpa.repository.JpaRepository;
public interface AnswerRepository extends JpaRepository<Answer,Long> {
import java.util.List;
public interface AnswerRepository extends JpaRepository<Answer, Long> {
List<Answer> findByReport_Id(Long reportId);
}

View File

@ -4,6 +4,10 @@ import com.edufocus.edufocus.report.entity.vo.Report;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface ReportRepository extends JpaRepository<Report, Long> {
List<Report> findByUser_Id(Long userId);
}

View File

@ -1,14 +1,20 @@
package com.edufocus.edufocus.report.service;
import com.edufocus.edufocus.report.entity.dto.ReportDetailResponseDto;
import com.edufocus.edufocus.report.entity.dto.ReportListResponseDto;
import com.edufocus.edufocus.report.entity.dto.ReportResponse;
import com.edufocus.edufocus.report.entity.vo.Report;
import com.edufocus.edufocus.report.entity.dto.ReportRequset;
import org.springframework.stereotype.Service;
import java.sql.SQLException;
import java.util.List;
@Service
public interface ReportService {
ReportResponse grading(ReportRequset reportRequset) throws SQLException;
ReportResponse grading(Long userId, Long quizesetId, ReportRequset reportRequset) throws SQLException;
ReportDetailResponseDto reportDetail(Long userId) throws SQLException;
List<ReportListResponseDto> resultList(Long userId) throws SQLException;
}

View File

@ -3,13 +3,12 @@ package com.edufocus.edufocus.report.service;
import com.edufocus.edufocus.quiz.entity.Quiz;
import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.quiz.repository.QuizRepository;
import com.edufocus.edufocus.quiz.repository.QuizSetRepository;
import com.edufocus.edufocus.quiz.service.QuizService;
import com.edufocus.edufocus.quiz.service.QuizSetService;
import com.edufocus.edufocus.report.entity.dto.ReportResponse;
import com.edufocus.edufocus.report.entity.dto.*;
import com.edufocus.edufocus.report.entity.vo.Answer;
import com.edufocus.edufocus.report.entity.dto.AnswerInput;
import com.edufocus.edufocus.report.entity.vo.Report;
import com.edufocus.edufocus.report.entity.dto.ReportRequset;
import com.edufocus.edufocus.report.repository.AnswerRepository;
import com.edufocus.edufocus.report.repository.ReportRepository;
import com.edufocus.edufocus.user.model.entity.vo.User;
@ -34,15 +33,17 @@ public class ReportServiceImpl implements ReportService {
private final ReportRepository reportRepository;
private final QuizRepository quizRepository;
private final AnswerRepository answerRepository;
private final AnswerService answerService;
private final UserRepository userRepository;
private final QuizSetRepository quizSetRepository;
@Override
public ReportResponse grading(ReportRequset reportRequset) throws SQLException {
public ReportResponse grading(Long userId, Long quizsetId, ReportRequset reportRequset) throws SQLException {
QuizSet quizSet = quizSetService.findQuizSet(reportRequset.getQuizsetId());
QuizSet quizSet = quizSetService.findQuizSet(quizsetId);
List<Quiz> quizList = quizSet.getQuizzes();
List<AnswerInput> answerInputList = reportRequset.getAnswerInputList();
@ -51,7 +52,7 @@ public class ReportServiceImpl implements ReportService {
int allCount = quizList.size();
int correctCount = 0;
User testuser = userRepository.findById(reportRequset.getUserId()).orElse(null);
User testuser = userRepository.findById(userId).orElse(null);
for (int idx = 0; idx < answerInputList.size(); idx++) {
Quiz quiz = quizList.get(idx);
@ -107,4 +108,105 @@ public class ReportServiceImpl implements ReportService {
return reportResponse;
}
@Override
public ReportDetailResponseDto reportDetail(Long repordId) {
Report report = reportRepository.findById(repordId).orElse(null);
QuizSet quizSet = quizSetService.findQuizSet(report.getQuizSet().getId());
List<Quiz> correctQuiz = new ArrayList<>();
List<Quiz> incorrectQuiz = new ArrayList<>();
List<Answer> myAnswer = answerRepository.findByReport_Id(report.getId());
for (Answer answer : myAnswer) {
if (answer.isCorrect()) {
correctQuiz.add(quizRepository.findById(answer.getQuiz().getId()).orElse(null));
} else {
incorrectQuiz.add(quizRepository.findById(answer.getQuiz().getId()).orElse(null));
}
}
ReportDetailResponseDto dto = ReportDetailResponseDto.builder()
.title(quizSet.getTitle())
.testAt(report.getTestAt())
.allCount(report.getAllCount())
.correctCount(report.getCorrectCount())
.correctQuizzes(correctQuiz)
.incorrectQuizzes(incorrectQuiz)
.build();
return dto;
}
// @Override
// public List<ReportResultResponseDto> result(Long userId) throws SQLException {
// List<Report> myReport = reportRepository.findByUser_Id(userId);
//
//
// List<ReportResultResponseDto> responseDtos = new ArrayList<>();
//
// for (Report report : myReport) {
// int all = report.getAllCount();
// int correctCount = report.getCorrectCount();
// Long quizsetId = report.getQuizSet().getId();
// Date testAt = report.getTestAt();
//
// List<Quiz> correctQuiz = new ArrayList<>();
// List<Quiz> incorrectQuiz = new ArrayList<>();
//
// List<Answer> myAnswer = answerRepository.findByReport_Id(report.getId());
//
// for (Answer answer : myAnswer) {
// if (answer.isCorrect()) {
// correctQuiz.add(quizRepository.findById(answer.getQuiz().getId()).orElse(null));
// } else {
// incorrectQuiz.add(quizRepository.findById(answer.getQuiz().getId()).orElse(null));
//
// }
// }
//
//
// ReportResultResponseDto dto = ReportResultResponseDto.builder()
// .reportId(report.getId())
// .allCount(all)
// .correctCount(correctCount)
// .quizSetId(quizSetId)
// .testAt(testAt)
// .correctQuizzes(correctQuiz)
// .incorrectQuizzes(incorrectQuiz)
// .build();
//
// responseDtos.add(dto);
// }
// // 틀린문제
// // 내가 체크한 번호
// // 정답 번호
// }
@Override
public List<ReportListResponseDto> resultList(Long userId) throws SQLException {
List<Report> reportList = reportRepository.findByUser_Id(userId);
List<ReportListResponseDto> reportListResponseDtoList = new ArrayList<>();
for (Report report : reportList) {
QuizSet quizSet = quizSetService.findQuizSet(report.getQuizSet().getId());
ReportListResponseDto dto = ReportListResponseDto.builder()
.title(quizSet.getTitle()) // Assuming QuizSet has a method getTitle()
.date(report.getTestAt())
.reportId(report.getId())// Assuming QuizSet has a method getDate()
.build();
reportListResponseDtoList.add(dto);
}
return reportListResponseDtoList;
}
}

View File

@ -32,9 +32,9 @@ public class UserController {
private final JWTUtil jwtUtil;
@PostMapping("/join")
public ResponseEntity<String> join(@RequestBody RequestJoinDto requestJoinDto){
public ResponseEntity<String> join(@RequestBody RequestJoinDto requestJoinDto) {
if(userService.isUserIdExist(requestJoinDto.getUserId()))
if (userService.isUserIdExist(requestJoinDto.getUserId()))
return new ResponseEntity<>("아이디가 중복 됐습니다.", HttpStatus.CONFLICT);
userService.join(requestJoinDto);
@ -83,8 +83,8 @@ public class UserController {
userService.saveRefreshToken(loginUser.getId(), refreshToken);
resultMap.put("name",loginUser.getName());
resultMap.put("role",loginUser.getRole());
resultMap.put("name", loginUser.getName());
resultMap.put("role", loginUser.getRole());
resultMap.put("access-token", accessToken);
setCookies(response, refreshToken);
@ -108,7 +108,7 @@ public class UserController {
@Operation(summary = "Access Token 재발급", description = "만료된 access token 을 재발급 받는다.")
@PostMapping("/refresh")
public ResponseEntity<?> refreshToken(HttpServletRequest request,HttpServletResponse response) {
public ResponseEntity<?> refreshToken(HttpServletRequest request, HttpServletResponse response) {
Cookie[] cookies = request.getCookies();
String token = null;
if (cookies != null) {
@ -120,9 +120,9 @@ public class UserController {
}
}
try{
try {
jwtUtil.checkToken(token);
}catch (Exception e){
} catch (Exception e) {
throw new InvalidTokenException();
}
@ -140,7 +140,7 @@ public class UserController {
Map<String, Object> resultMap = new HashMap<>();
resultMap.put("access-token", accessToken);
userService.saveRefreshToken(userId,refreshToken);
userService.saveRefreshToken(userId, refreshToken);
setCookies(response, refreshToken);
@ -175,7 +175,7 @@ public class UserController {
}
private void setCookies(HttpServletResponse response, String refreshToken){
private void setCookies(HttpServletResponse response, String refreshToken) {
Cookie refreshCookie = new Cookie("refresh-token", refreshToken);
refreshCookie.setPath("/");
refreshCookie.setHttpOnly(true);