From e8c88f9b1bd4b87310c6921b65e740f759a917fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EB=B0=95=EC=A0=95=EB=AF=BC?= Date: Wed, 7 Aug 2024 10:15:12 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20report=20=ED=95=99=EC=83=9D,=EA=B0=95?= =?UTF-8?q?=EC=82=AC=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lecture/controller/LectureController.java | 4 +- .../report/controller/ReportController.java | 58 +++++++-- .../edufocus/report/entity/dto/ChoiceDto.java | 12 ++ .../edufocus/report/entity/dto/QuizDto.java | 28 +++++ .../entity/dto/ReportDetailResponseDto.java | 4 +- .../entity/dto/ReportListResponseDto.java | 3 + .../report/repository/ReportRepository.java | 2 + .../report/service/ReportService.java | 3 +- .../report/service/ReportServiceImpl.java | 116 ++++++++++-------- 9 files changed, 170 insertions(+), 60 deletions(-) create mode 100644 backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ChoiceDto.java create mode 100644 backend/src/main/java/com/edufocus/edufocus/report/entity/dto/QuizDto.java diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java b/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java index 6e04639..90e8e0b 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java @@ -14,6 +14,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; +import java.util.ArrayList; import java.util.List; @RestController @@ -83,8 +84,9 @@ public class LectureController { @GetMapping("/mylecture") public ResponseEntity findMyLecture(@RequestHeader(value = "Authorization", required = false) String accessToken) { + if (accessToken == null) { - return new ResponseEntity<>(HttpStatus.NO_CONTENT); + return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK); } Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); diff --git a/backend/src/main/java/com/edufocus/edufocus/report/controller/ReportController.java b/backend/src/main/java/com/edufocus/edufocus/report/controller/ReportController.java index bbae94c..9c789bf 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/controller/ReportController.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/controller/ReportController.java @@ -6,6 +6,11 @@ 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.model.entity.vo.User; +import com.edufocus.edufocus.user.model.entity.vo.UserRole; +import com.edufocus.edufocus.user.model.repository.UserRepository; +import com.edufocus.edufocus.user.model.service.UserService; +import com.edufocus.edufocus.user.model.service.UserServiceImpl; import com.edufocus.edufocus.user.util.JWTUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -28,35 +33,74 @@ public class ReportController { private final ReportService reportService; private final JWTUtil jwtUtil; + private final UserService userService; + private final UserRepository userRepository; + //SUBMIT 할떄 LECTURE ID 저장해놓기 - @PostMapping("/submit/{quizsetId}") - public ResponseEntity submit(@PathVariable("quizsetId") Long quizestId, @RequestBody ReportRequset reportRequset, HttpServletRequest request) throws SQLException { + @PostMapping("/submit/{lectreId}/quizset/{quizsetId}") + public ResponseEntity submit(@PathVariable("lectreId") Long lectureId, @PathVariable("quizsetId") Long quizestId, @RequestBody ReportRequset reportRequset, HttpServletRequest request) throws SQLException { String token = request.getHeader("Authorization"); Long userId = Long.parseLong(jwtUtil.getUserId(token)); + User findUser = userRepository.findById(userId).orElse(null); + if (findUser.getRole() == UserRole.ADMIN) { + + return new ResponseEntity<>("강사는 퀴즈제출을 할수 없습니다", HttpStatus.FORBIDDEN); + + } + ReportResponse report = reportService.grading(userId, quizestId, reportRequset, lectureId); - ReportResponse report = reportService.grading(userId, quizestId, reportRequset); return new ResponseEntity<>(report, HttpStatus.CREATED); } - @GetMapping("/myreport/{id}") - public ResponseEntity> reportList(@PathVariable Long id, HttpServletRequest request) throws SQLException { + + @GetMapping("/myreport") + public ResponseEntity> myreport(HttpServletRequest request) throws SQLException { String token = request.getHeader("Authorization"); Long userId = Long.parseLong(jwtUtil.getUserId(token)); + + List reportList = reportService.resultList(userId); return new ResponseEntity<>(reportList, HttpStatus.CREATED); } - @GetMapping("/detailreport/{id}") - public ResponseEntity myReport(@PathVariable("id") Long reportId) throws SQLException { + @GetMapping("/myreportdetail/{id}") + public ResponseEntity myreportdetail(@PathVariable("id") Long reportId) throws SQLException { ReportDetailResponseDto detailReport = reportService.reportDetail(reportId); return new ResponseEntity<>(detailReport, HttpStatus.CREATED); } + @GetMapping("/studentreport/{lecturid}") + public ResponseEntity> studentreport(@PathVariable("lecturid") Long lectureId) throws SQLException { + + + List reportList = reportService.studentResultList(lectureId); + + return new ResponseEntity<>(reportList, HttpStatus.CREATED); + + } + + @GetMapping("/studentreportdetail/{id}") + public ResponseEntity studentdetailreport(@PathVariable("id") Long reportId) throws SQLException { + + ReportDetailResponseDto detailReport = reportService.reportDetail(reportId); + return new ResponseEntity<>(detailReport, HttpStatus.CREATED); + + } + + // 강좌에 대한 퀴즈셋 +// lecture id가 똑같은 report들 제목 + 작성자 + 맞틀 개수 미리 보여주기 +// @GetMapping("/detailreport/{id}") +// public ResponseEntity (@PathVariable("id") Long reportId) throws SQLException { +// +// ReportDetailResponseDto detailReport = reportService.reportDetail(reportId); +// return new ResponseEntity<>(detailReport, HttpStatus.CREATED); +// +// } v 9 } diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ChoiceDto.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ChoiceDto.java new file mode 100644 index 0000000..c28bbc2 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ChoiceDto.java @@ -0,0 +1,12 @@ +package com.edufocus.edufocus.report.entity.dto; + +import lombok.Builder; +import lombok.Data; +import lombok.Setter; + +@Data +@Builder +public class ChoiceDto { + int num; + String content; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/QuizDto.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/QuizDto.java new file mode 100644 index 0000000..350e906 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/QuizDto.java @@ -0,0 +1,28 @@ +package com.edufocus.edufocus.report.entity.dto; + +import com.edufocus.edufocus.quiz.entity.Choice; +import com.edufocus.edufocus.quiz.entity.QuizSet; +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonManagedReference; +import jakarta.persistence.*; +import lombok.Builder; +import lombok.Data; + +import java.util.List; + +@Data +@Builder +public class QuizDto { + + + private long id; + + + private String question; + + private String image; + + private String answer; + private String userAnswer; + private List choices; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ReportDetailResponseDto.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ReportDetailResponseDto.java index 20458df..a86b4e7 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ReportDetailResponseDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ReportDetailResponseDto.java @@ -14,8 +14,8 @@ public class ReportDetailResponseDto { private String title; private int correctCount; private Date testAt; - private List correctQuizzes; - private List incorrectQuizzes; + private List correctQuizzes; + private List incorrectQuizzes; } \ No newline at end of file diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ReportListResponseDto.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ReportListResponseDto.java index 02536d7..94fc3e2 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ReportListResponseDto.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/dto/ReportListResponseDto.java @@ -11,4 +11,7 @@ public class ReportListResponseDto { private String title; private Date date; private Long reportId; + private String name; + private int allCount; + private int correctCount; } \ No newline at end of file diff --git a/backend/src/main/java/com/edufocus/edufocus/report/repository/ReportRepository.java b/backend/src/main/java/com/edufocus/edufocus/report/repository/ReportRepository.java index fd69fb5..66d2fb9 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/repository/ReportRepository.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/repository/ReportRepository.java @@ -10,4 +10,6 @@ import java.util.List; public interface ReportRepository extends JpaRepository { List findByUser_Id(Long userId); + List findByLectureId(Long lectureId); + } diff --git a/backend/src/main/java/com/edufocus/edufocus/report/service/ReportService.java b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportService.java index 10c08d7..7edcc4a 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/service/ReportService.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportService.java @@ -11,10 +11,11 @@ import java.util.List; @Service public interface ReportService { - ReportResponse grading(Long userId, Long quizesetId, ReportRequset reportRequset) throws SQLException; + ReportResponse grading(Long userId, Long quizesetId, ReportRequset reportRequset, Long lectureId) throws SQLException; ReportDetailResponseDto reportDetail(Long userId) throws SQLException; List resultList(Long userId) throws SQLException; + List studentResultList(Long lectureId) throws SQLException; } diff --git a/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java index f2666bc..5543795 100644 --- a/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java @@ -1,5 +1,6 @@ package com.edufocus.edufocus.report.service; +import com.edufocus.edufocus.quiz.entity.Choice; import com.edufocus.edufocus.quiz.entity.Quiz; import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.quiz.repository.QuizRepository; @@ -15,6 +16,7 @@ import com.edufocus.edufocus.user.model.entity.vo.User; import com.edufocus.edufocus.user.model.repository.UserRepository; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; +import org.checkerframework.checker.units.qual.C; import org.springframework.stereotype.Service; import java.sql.SQLException; @@ -41,7 +43,7 @@ public class ReportServiceImpl implements ReportService { @Override - public ReportResponse grading(Long userId, Long quizsetId, ReportRequset reportRequset) throws SQLException { + public ReportResponse grading(Long userId, Long quizsetId, ReportRequset reportRequset, Long lectureId) throws SQLException { QuizSet quizSet = quizSetService.findQuizSet(quizsetId); List quizList = quizSet.getQuizzes(); @@ -85,6 +87,7 @@ public class ReportServiceImpl implements ReportService { .allCount(allCount) .correctCount(correctCount) .testAt(new Date()) + .lectureId(lectureId) .build(); @@ -116,16 +119,49 @@ public class ReportServiceImpl implements ReportService { QuizSet quizSet = quizSetService.findQuizSet(report.getQuizSet().getId()); - List correctQuiz = new ArrayList<>(); - List incorrectQuiz = new ArrayList<>(); + List correctQuiz = new ArrayList<>(); + List incorrectQuiz = new ArrayList<>(); List myAnswer = answerRepository.findByReport_Id(report.getId()); for (Answer answer : myAnswer) { + QuizDto quizDto; + Quiz quiz = quizRepository.findById(answer.getQuiz().getId()).orElse(null); + + ArrayList choiceDtos = new ArrayList<>(); + + for (Choice c : quiz.getChoices()) { + ChoiceDto choiceDto = null; + choiceDto = choiceDto.builder() + .num(c.getNum()) + .content(c.getContent()) + .build(); + choiceDtos.add(choiceDto); + + } if (answer.isCorrect()) { - correctQuiz.add(quizRepository.findById(answer.getQuiz().getId()).orElse(null)); + + quizDto = QuizDto.builder() + .id(quiz.getId()) + .question(quiz.getQuestion()) + .image(quiz.getImage()) + .question(quiz.getQuestion()) + .answer(quiz.getAnswer()) + .userAnswer(answer.getUserAnswer()) + .choices(choiceDtos) + .build(); + correctQuiz.add(quizDto); } else { - incorrectQuiz.add(quizRepository.findById(answer.getQuiz().getId()).orElse(null)); + quizDto = QuizDto.builder() + .id(quiz.getId()) + .question(quiz.getQuestion()) + .image(quiz.getImage()) + .question(quiz.getQuestion()) + .answer(quiz.getAnswer()) + .userAnswer(answer.getUserAnswer()) + .choices(choiceDtos) + .build(); + incorrectQuiz.add(quizDto); } } @@ -141,50 +177,6 @@ public class ReportServiceImpl implements ReportService { return dto; } -// @Override -// public List result(Long userId) throws SQLException { -// List myReport = reportRepository.findByUser_Id(userId); -// -// -// List 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 correctQuiz = new ArrayList<>(); -// List incorrectQuiz = new ArrayList<>(); -// -// List 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 resultList(Long userId) throws SQLException { @@ -210,4 +202,30 @@ public class ReportServiceImpl implements ReportService { } + @Override + public List studentResultList(Long lectureId) throws SQLException { + + List reportList = reportRepository.findByLectureId(lectureId); + + List reportListResponseDtoList = new ArrayList<>(); + + + for (Report report : reportList) { + + System.out.println(report.toString()); + QuizSet quizSet = quizSetService.findQuizSet(report.getQuizSet().getId()); + ReportListResponseDto dto = ReportListResponseDto.builder() + .title(quizSet.getTitle()) + .date(report.getTestAt()) + .reportId(report.getId()) + .allCount(report.getAllCount()) + .correctCount(report.getCorrectCount()) + .build(); + reportListResponseDtoList.add(dto); + + } + + return reportListResponseDtoList; + + } }