feat: report 학생,강사 조회 기능 추가

This commit is contained in:
박정민 2024-08-07 10:15:12 +09:00
parent 84e56206dc
commit e8c88f9b1b
9 changed files with 170 additions and 60 deletions

View File

@ -14,6 +14,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List; import java.util.List;
@RestController @RestController
@ -83,8 +84,9 @@ public class LectureController {
@GetMapping("/mylecture") @GetMapping("/mylecture")
public ResponseEntity<?> findMyLecture(@RequestHeader(value = "Authorization", required = false) String accessToken) { public ResponseEntity<?> findMyLecture(@RequestHeader(value = "Authorization", required = false) String accessToken) {
if (accessToken == null) { if (accessToken == null) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT); return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
} }
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));

View File

@ -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.vo.Report;
import com.edufocus.edufocus.report.entity.dto.ReportRequset; import com.edufocus.edufocus.report.entity.dto.ReportRequset;
import com.edufocus.edufocus.report.service.ReportService; 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 com.edufocus.edufocus.user.util.JWTUtil;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
@ -28,35 +33,74 @@ public class ReportController {
private final ReportService reportService; private final ReportService reportService;
private final JWTUtil jwtUtil; private final JWTUtil jwtUtil;
private final UserService userService;
private final UserRepository userRepository;
//SUBMIT 할떄 LECTURE ID 저장해놓기
@PostMapping("/submit/{quizsetId}") @PostMapping("/submit/{lectreId}/quizset/{quizsetId}")
public ResponseEntity<ReportResponse> submit(@PathVariable("quizsetId") Long quizestId, @RequestBody ReportRequset reportRequset, HttpServletRequest request) throws SQLException { public ResponseEntity<?> submit(@PathVariable("lectreId") Long lectureId, @PathVariable("quizsetId") Long quizestId, @RequestBody ReportRequset reportRequset, HttpServletRequest request) throws SQLException {
String token = request.getHeader("Authorization"); String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token)); 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); return new ResponseEntity<>(report, HttpStatus.CREATED);
} }
@GetMapping("/myreport/{id}")
public ResponseEntity<List<ReportListResponseDto>> reportList(@PathVariable Long id, HttpServletRequest request) throws SQLException { @GetMapping("/myreport")
public ResponseEntity<List<ReportListResponseDto>> myreport(HttpServletRequest request) throws SQLException {
String token = request.getHeader("Authorization"); String token = request.getHeader("Authorization");
Long userId = Long.parseLong(jwtUtil.getUserId(token)); Long userId = Long.parseLong(jwtUtil.getUserId(token));
List<ReportListResponseDto> reportList = reportService.resultList(userId); List<ReportListResponseDto> reportList = reportService.resultList(userId);
return new ResponseEntity<>(reportList, HttpStatus.CREATED); return new ResponseEntity<>(reportList, HttpStatus.CREATED);
} }
@GetMapping("/detailreport/{id}") @GetMapping("/myreportdetail/{id}")
public ResponseEntity<ReportDetailResponseDto> myReport(@PathVariable("id") Long reportId) throws SQLException { public ResponseEntity<ReportDetailResponseDto> myreportdetail(@PathVariable("id") Long reportId) throws SQLException {
ReportDetailResponseDto detailReport = reportService.reportDetail(reportId); ReportDetailResponseDto detailReport = reportService.reportDetail(reportId);
return new ResponseEntity<>(detailReport, HttpStatus.CREATED); return new ResponseEntity<>(detailReport, HttpStatus.CREATED);
} }
@GetMapping("/studentreport/{lecturid}")
public ResponseEntity<List<ReportListResponseDto>> studentreport(@PathVariable("lecturid") Long lectureId) throws SQLException {
List<ReportListResponseDto> reportList = reportService.studentResultList(lectureId);
return new ResponseEntity<>(reportList, HttpStatus.CREATED);
}
@GetMapping("/studentreportdetail/{id}")
public ResponseEntity<ReportDetailResponseDto> 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<ReportDetailResponseDto> (@PathVariable("id") Long reportId) throws SQLException {
//
// ReportDetailResponseDto detailReport = reportService.reportDetail(reportId);
// return new ResponseEntity<>(detailReport, HttpStatus.CREATED);
//
// } v 9
} }

View File

@ -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;
}

View File

@ -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<ChoiceDto> choices;
}

View File

@ -14,8 +14,8 @@ public class ReportDetailResponseDto {
private String title; private String title;
private int correctCount; private int correctCount;
private Date testAt; private Date testAt;
private List<Quiz> correctQuizzes; private List<QuizDto> correctQuizzes;
private List<Quiz> incorrectQuizzes; private List<QuizDto> incorrectQuizzes;
} }

View File

@ -11,4 +11,7 @@ public class ReportListResponseDto {
private String title; private String title;
private Date date; private Date date;
private Long reportId; private Long reportId;
private String name;
private int allCount;
private int correctCount;
} }

View File

@ -10,4 +10,6 @@ import java.util.List;
public interface ReportRepository extends JpaRepository<Report, Long> { public interface ReportRepository extends JpaRepository<Report, Long> {
List<Report> findByUser_Id(Long userId); List<Report> findByUser_Id(Long userId);
List<Report> findByLectureId(Long lectureId);
} }

View File

@ -11,10 +11,11 @@ import java.util.List;
@Service @Service
public interface ReportService { 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; ReportDetailResponseDto reportDetail(Long userId) throws SQLException;
List<ReportListResponseDto> resultList(Long userId) throws SQLException; List<ReportListResponseDto> resultList(Long userId) throws SQLException;
List<ReportListResponseDto> studentResultList(Long lectureId) throws SQLException;
} }

View File

@ -1,5 +1,6 @@
package com.edufocus.edufocus.report.service; 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.Quiz;
import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.quiz.repository.QuizRepository; 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 com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.checkerframework.checker.units.qual.C;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.sql.SQLException; import java.sql.SQLException;
@ -41,7 +43,7 @@ public class ReportServiceImpl implements ReportService {
@Override @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); QuizSet quizSet = quizSetService.findQuizSet(quizsetId);
List<Quiz> quizList = quizSet.getQuizzes(); List<Quiz> quizList = quizSet.getQuizzes();
@ -85,6 +87,7 @@ public class ReportServiceImpl implements ReportService {
.allCount(allCount) .allCount(allCount)
.correctCount(correctCount) .correctCount(correctCount)
.testAt(new Date()) .testAt(new Date())
.lectureId(lectureId)
.build(); .build();
@ -116,16 +119,49 @@ public class ReportServiceImpl implements ReportService {
QuizSet quizSet = quizSetService.findQuizSet(report.getQuizSet().getId()); QuizSet quizSet = quizSetService.findQuizSet(report.getQuizSet().getId());
List<Quiz> correctQuiz = new ArrayList<>(); List<QuizDto> correctQuiz = new ArrayList<>();
List<Quiz> incorrectQuiz = new ArrayList<>(); List<QuizDto> incorrectQuiz = new ArrayList<>();
List<Answer> myAnswer = answerRepository.findByReport_Id(report.getId()); List<Answer> myAnswer = answerRepository.findByReport_Id(report.getId());
for (Answer answer : myAnswer) { for (Answer answer : myAnswer) {
QuizDto quizDto;
Quiz quiz = quizRepository.findById(answer.getQuiz().getId()).orElse(null);
ArrayList<ChoiceDto> 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()) { 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 { } 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; 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 @Override
public List<ReportListResponseDto> resultList(Long userId) throws SQLException { public List<ReportListResponseDto> resultList(Long userId) throws SQLException {
@ -210,4 +202,30 @@ public class ReportServiceImpl implements ReportService {
} }
@Override
public List<ReportListResponseDto> studentResultList(Long lectureId) throws SQLException {
List<Report> reportList = reportRepository.findByLectureId(lectureId);
List<ReportListResponseDto> 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;
}
} }