Merge branch 'Be/ReportSet' into 'backend'

feat : reportSet 기능 추가

See merge request s11-webmobile1-sub2/S11P12A701!97
This commit is contained in:
김기창 2024-08-08 09:22:44 +09:00
commit 1abd73f745
26 changed files with 284 additions and 342 deletions

View File

@ -55,7 +55,7 @@ public class Board {
private Lecture lecture; private Lecture lecture;
@OneToMany(mappedBy = "board") @OneToMany(mappedBy = "board", cascade = CascadeType.ALL)
private List<Comment> comments; private List<Comment> comments;
public ResponseBoardSummaryDto makeSummaryDto(){ public ResponseBoardSummaryDto makeSummaryDto(){

View File

@ -28,7 +28,7 @@ public class QuizSet {
@Column @Column
private String title; private String title;
@OneToMany(mappedBy = "quizSet", orphanRemoval = true) @OneToMany(mappedBy = "quizSet", orphanRemoval = true, fetch = FetchType.EAGER)
@JsonManagedReference @JsonManagedReference
private List<Quiz> quizzes; private List<Quiz> quizzes;

View File

@ -1,10 +1,15 @@
package com.edufocus.edufocus.registration.entity; package com.edufocus.edufocus.registration.entity;
import com.edufocus.edufocus.lecture.entity.Lecture; import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.report.entity.vo.Report;
import com.edufocus.edufocus.report.entity.vo.ReportSet;
import com.edufocus.edufocus.user.model.entity.vo.User; import com.edufocus.edufocus.user.model.entity.vo.User;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import java.util.UUID;
@Entity @Entity
@Getter @Getter
@Setter @Setter
@ -27,4 +32,18 @@ public class Registration {
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
private RegistrationStatus status; private RegistrationStatus status;
public boolean isAccepted(){
return status == RegistrationStatus.ACCEPTED;
}
public Report makeReport(ReportSet reportSet, QuizSet quizSet, long lectureId){
return Report.builder()
.allCount(0)
.correctCount(-1)
.lectureId(lectureId)
.user(user)
.build();
}
} }

View File

@ -1,6 +1,7 @@
package com.edufocus.edufocus.registration.repository; package com.edufocus.edufocus.registration.repository;
import com.edufocus.edufocus.registration.entity.Registration; import com.edufocus.edufocus.registration.entity.Registration;
import com.edufocus.edufocus.registration.entity.RegistrationStatus;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
@ -16,4 +17,6 @@ public interface RegistrationRepository extends JpaRepository<Registration, Long
List<Registration> findAllNotAcceptedByLectureId(@Param("lectureId") Long lectureId); List<Registration> findAllNotAcceptedByLectureId(@Param("lectureId") Long lectureId);
Registration findByUserIdAndLectureId(Long userId, Long lectureId); Registration findByUserIdAndLectureId(Long userId, Long lectureId);
List<Registration> findByLectureIdAndStatus(Long lectureId, RegistrationStatus status);
} }

View File

@ -1,19 +1,10 @@
package com.edufocus.edufocus.report.controller; package com.edufocus.edufocus.report.controller;
import com.edufocus.edufocus.report.entity.dto.ReportDetailResponseDto; import com.edufocus.edufocus.report.entity.dto.*;
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.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.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 lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -21,8 +12,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID;
@RestController @RestController
@RequestMapping("/report") @RequestMapping("/report")
@ -34,73 +25,61 @@ 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 UserService userService;
private final UserRepository userRepository;
//SUBMIT 할떄 LECTURE ID 저장해놓기
@PostMapping("/submit/{lectreId}/quizset/{quizsetId}") @PostMapping("/submit/quizSet/{reportSetId}")
public ResponseEntity<?> submit(@PathVariable("lectreId") Long lectureId, @PathVariable("quizsetId") Long quizestId, @RequestBody ReportRequset reportRequset, HttpServletRequest request) throws SQLException { public ResponseEntity<?> submit(@PathVariable("reportSetId") UUID reportSetId, @RequestBody ReportRequest reportRequest, HttpServletRequest request) {
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) {
if (userService.isTeacher(userId))
return new ResponseEntity<>("강사는 퀴즈제출을 할수 없습니다", HttpStatus.FORBIDDEN); return new ResponseEntity<>("강사는 퀴즈제출을 할수 없습니다", HttpStatus.FORBIDDEN);
} reportService.grade(userId, reportSetId, reportRequest);
ReportResponse report = reportService.grading(userId, quizestId, reportRequset, lectureId);
return new ResponseEntity<>(report, HttpStatus.CREATED); return new ResponseEntity<>(HttpStatus.OK);
} }
@GetMapping("/myreport") @GetMapping("/student/{lectureId}")
public ResponseEntity<List<ReportListResponseDto>> myreport(HttpServletRequest request) throws SQLException { public ResponseEntity<List<ReportResponse>> searchMyReport(@PathVariable long lectureId, HttpServletRequest request) {
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<ReportResponse> reportResponses = reportService.findReports(lectureId, userId);
List<ReportListResponseDto> reportList = reportService.resultList(userId); if(reportResponses.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
return new ResponseEntity<>(reportList, HttpStatus.CREATED);
return new ResponseEntity<>(reportResponses, HttpStatus.OK);
} }
@GetMapping("/myreportdetail/{id}") @GetMapping("/reportDetail/{reportId}")
public ResponseEntity<ReportDetailResponseDto> myreportdetail(@PathVariable("id") Long reportId) throws SQLException { public ResponseEntity<ReportDetailResponseDto> searchDetailReport(@PathVariable("reportId") long reportId){
ReportDetailResponseDto detailReport = reportService.reportDetail(reportId); ReportDetailResponseDto detailReport = reportService.reportDetail(reportId);
return new ResponseEntity<>(detailReport, HttpStatus.CREATED);
return new ResponseEntity<>(detailReport, HttpStatus.OK);
} }
@GetMapping("/studentreport/{lecturid}") @GetMapping("/teacher/reportSet/{lectureId}")
public ResponseEntity<List<ReportListResponseDto>> studentreport(@PathVariable("lecturid") Long lectureId) throws SQLException { public ResponseEntity<List<ReportSetResponse>> searchReportSets(@PathVariable("lectureId") long lectureId){
List<ReportSetResponse> reportSetResponses = reportService.findReportSets(lectureId);
if(reportSetResponses.isEmpty())
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
List<ReportListResponseDto> reportList = reportService.studentResultList(lectureId); return new ResponseEntity<>(reportSetResponses, HttpStatus.OK);
return new ResponseEntity<>(reportList, HttpStatus.CREATED);
} }
@GetMapping("/studentreportdetail/{id}") @GetMapping("/teacher/report/{reportSetId}")
public ResponseEntity<ReportDetailResponseDto> studentdetailreport(@PathVariable("id") Long reportId) throws SQLException { public ResponseEntity<?> searchReports(@PathVariable("reportSetId") UUID reportSetId){
List<ReportResponse> reportResponses = reportService.findReports(reportSetId);
ReportDetailResponseDto detailReport = reportService.reportDetail(reportId); if(reportResponses.isEmpty())
return new ResponseEntity<>(detailReport, HttpStatus.CREATED); return new ResponseEntity<>(HttpStatus.NO_CONTENT);
return new ResponseEntity<>(reportResponses, HttpStatus.OK);
} }
// 강좌에 대한 퀴즈셋
// 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

@ -1,12 +0,0 @@
package com.edufocus.edufocus.report.entity.dto;
import lombok.Getter;
@Getter
public class AnswerInput {
String answer;
}

View File

@ -13,15 +13,9 @@ import java.util.List;
@Data @Data
@Builder @Builder
public class QuizDto { public class QuizDto {
private long id; private long id;
private String question; private String question;
private String image; private String image;
private String answer; private String answer;
private String userAnswer; private String userAnswer;
private List<ChoiceDto> choices; private List<ChoiceDto> choices;

View File

@ -1,6 +0,0 @@
package com.edufocus.edufocus.report.entity.dto;
public class QuizSubmissionDto {
private Long quizSetId;
private Long userId;
}

View File

@ -4,6 +4,7 @@ import com.edufocus.edufocus.quiz.entity.Quiz;
import lombok.Builder; import lombok.Builder;
import lombok.Data; import lombok.Data;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -13,7 +14,7 @@ public class ReportDetailResponseDto {
private int allCount; private int allCount;
private String title; private String title;
private int correctCount; private int correctCount;
private Date testAt; private LocalDateTime testAt;
private List<QuizDto> correctQuizzes; private List<QuizDto> correctQuizzes;
private List<QuizDto> incorrectQuizzes; private List<QuizDto> incorrectQuizzes;

View File

@ -1,17 +0,0 @@
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;
private String name;
private int allCount;
private int correctCount;
}

View File

@ -7,7 +7,7 @@ import java.util.List;
@Getter @Getter
@Setter @Setter
public class ReportRequset { public class ReportRequest {
List<String> answer; List<String> answer;

View File

@ -1,26 +1,20 @@
package com.edufocus.edufocus.report.entity.dto; package com.edufocus.edufocus.report.entity.dto;
import com.edufocus.edufocus.report.entity.vo.Answer;
import lombok.Builder; import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List;
@Getter @Getter
@Setter @Setter
@Builder @Builder
public class ReportResponse { public class ReportResponse {
private long reportId;
private Long quizesetId; private String name;
private Long userId;
private String title; private String title;
private int allCount; private int allCount;
private int correctCount; private int correctCount;
private Date testAt; private LocalDateTime date;
} }

View File

@ -0,0 +1,18 @@
package com.edufocus.edufocus.report.entity.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
import java.util.UUID;
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReportSetResponse {
private UUID reportSetId;
private String quizSetTitle;
private LocalDateTime testAt;
}

View File

@ -28,5 +28,4 @@ public class Answer {
@JoinColumn(name = "quiz_id") @JoinColumn(name = "quiz_id")
private Quiz quiz; private Quiz quiz;
} }

View File

@ -1,11 +1,15 @@
package com.edufocus.edufocus.report.entity.vo; package com.edufocus.edufocus.report.entity.vo;
import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.report.entity.dto.ReportResponse;
import com.edufocus.edufocus.user.model.entity.vo.User; import com.edufocus.edufocus.user.model.entity.vo.User;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import org.springframework.data.annotation.CreatedDate;
import java.time.LocalDateTime;
import java.util.Date; import java.util.Date;
import java.util.List;
@Entity @Entity
@Getter @Getter
@ -14,16 +18,19 @@ import java.util.Date;
@AllArgsConstructor @AllArgsConstructor
@Builder @Builder
public class Report { public class Report {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
private int allCount; private int allCount;
private int correctCount; private int correctCount;
private Date testAt;
@CreatedDate
private LocalDateTime testAt;
private Long lectureId; private Long lectureId;
@ManyToOne @ManyToOne
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
private User user; private User user;
@ -32,4 +39,21 @@ public class Report {
@JoinColumn(name = "quizset_id") @JoinColumn(name = "quizset_id")
private QuizSet quizSet; private QuizSet quizSet;
@ManyToOne
@JoinColumn(name = "reportset_id")
private ReportSet reportSet;
@OneToMany(mappedBy = "report", cascade = CascadeType.ALL)
private List<Answer> answers;
public ReportResponse makeReportResponse(){
return ReportResponse.builder()
.reportId(id)
.name(user.getName())
.title(quizSet.getTitle())
.allCount(allCount)
.correctCount(correctCount)
.date(testAt)
.build();
}
} }

View File

@ -0,0 +1,54 @@
package com.edufocus.edufocus.report.entity.vo;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.report.entity.dto.ReportSetResponse;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.data.annotation.CreatedDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.UUID;
@Entity
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ReportSet {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(
name = "UUID",
strategy = "org.hibernate.id.UUIDGenerator"
)
private UUID id;
@CreatedDate
private LocalDateTime createAt;
@OneToMany(mappedBy = "reportSet", cascade = CascadeType.ALL)
private List<Report> reports;
@ManyToOne
@JoinColumn(name = "lecture_id")
private Lecture lecture;
@ManyToOne
@JoinColumn(name = "quizSet_id")
private QuizSet quizSet;
public ReportSetResponse makeReportSetResponse() {
return ReportSetResponse.builder()
.reportSetId(id)
.quizSetTitle(quizSet.getTitle())
.testAt(createAt)
.build();
}
}

View File

@ -5,11 +5,17 @@ import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import java.util.List; import java.util.List;
import java.util.UUID;
@Repository @Repository
public interface ReportRepository extends JpaRepository<Report, Long> { public interface ReportRepository extends JpaRepository<Report, Long> {
List<Report> findByUser_Id(Long userId); List<Report> findByUserId(long userId);
List<Report> findByLectureId(Long lectureId); List<Report> findByLectureId(long lectureId);
Report findByReportSetIdAndUserId(UUID reportSetId, long userId);
List<Report> findByReportSetId(UUID reportSetId);
List<Report> findByLectureIdAndUserId(long lectureId, long userId);
} }

View File

@ -0,0 +1,13 @@
package com.edufocus.edufocus.report.repository;
import com.edufocus.edufocus.report.entity.vo.ReportSet;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.UUID;
@Repository
public interface ReportSetRepository extends JpaRepository<ReportSet, UUID> {
List<ReportSet> findByLectureId(long userId);
}

View File

@ -1,21 +1,23 @@
package com.edufocus.edufocus.report.service; package com.edufocus.edufocus.report.service;
import com.edufocus.edufocus.report.entity.dto.ReportDetailResponseDto; import com.edufocus.edufocus.report.entity.dto.*;
import com.edufocus.edufocus.report.entity.dto.ReportListResponseDto;
import com.edufocus.edufocus.report.entity.dto.ReportResponse;
import com.edufocus.edufocus.report.entity.dto.ReportRequset;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.List; import java.util.List;
import java.util.UUID;
@Service @Service
public interface ReportService { public interface ReportService {
ReportResponse grading(Long userId, Long quizesetId, ReportRequset reportRequset, Long lectureId) throws SQLException; void grade(long userId, UUID reportSetId, ReportRequest reportRequest);
ReportDetailResponseDto reportDetail(Long userId) throws SQLException; ReportDetailResponseDto reportDetail(long userId);
List<ReportListResponseDto> resultList(Long userId) throws SQLException; List<ReportSetResponse> findReportSets(long lectureId);
List<ReportListResponseDto> studentResultList(Long lectureId) throws SQLException; List<ReportResponse> findReports(UUID reportSetId);
List<ReportResponse> findReports(long lectureId, long userid);
UUID initReportSet(long lectureId, long quizSetId);
} }

View File

@ -1,122 +1,96 @@
package com.edufocus.edufocus.report.service; package com.edufocus.edufocus.report.service;
import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.quiz.entity.Choice; 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;
import com.edufocus.edufocus.quiz.repository.QuizSetRepository; 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.quiz.service.QuizSetService;
import com.edufocus.edufocus.registration.entity.Registration;
import com.edufocus.edufocus.registration.entity.RegistrationStatus;
import com.edufocus.edufocus.registration.repository.RegistrationRepository;
import com.edufocus.edufocus.report.entity.dto.*; import com.edufocus.edufocus.report.entity.dto.*;
import com.edufocus.edufocus.report.entity.vo.Answer; import com.edufocus.edufocus.report.entity.vo.Answer;
import com.edufocus.edufocus.report.entity.vo.Report; import com.edufocus.edufocus.report.entity.vo.Report;
import com.edufocus.edufocus.report.entity.vo.ReportSet;
import com.edufocus.edufocus.report.repository.AnswerRepository; import com.edufocus.edufocus.report.repository.AnswerRepository;
import com.edufocus.edufocus.report.repository.ReportRepository; import com.edufocus.edufocus.report.repository.ReportRepository;
import com.edufocus.edufocus.report.repository.ReportSetRepository;
import com.edufocus.edufocus.user.model.entity.vo.User; 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;
import java.util.ArrayList; import java.util.*;
import java.util.Date;
import java.util.List;
@Service @Service
@Transactional @Transactional
@RequiredArgsConstructor @RequiredArgsConstructor
public class ReportServiceImpl implements ReportService { public class ReportServiceImpl implements ReportService {
private final QuizSetService quizSetService;
private final QuizService quizService;
private final ReportRepository reportRepository; private final ReportRepository reportRepository;
private final QuizRepository quizRepository; private final QuizRepository quizRepository;
private final RegistrationRepository registrationRepository;
private final AnswerRepository answerRepository; private final AnswerRepository answerRepository;
private final AnswerService answerService; private final ReportSetRepository reportSetRepository;
private final UserRepository userRepository; private final LectureRepository lectureRepository;
private final QuizSetRepository quizSetRepository; private final QuizSetRepository quizSetRepository;
@Override @Override
public ReportResponse grading(Long userId, Long quizsetId, ReportRequset reportRequset, Long lectureId) throws SQLException { public void grade(long userId, UUID reportSetId, ReportRequest reportRequest){
QuizSet quizSet = quizSetService.findQuizSet(quizsetId); ReportSet reportSet = reportSetRepository.findById(reportSetId).orElseThrow(NoSuchElementException::new);
List<Quiz> quizList = quizSet.getQuizzes();
List<String> answerInputList = reportRequset.getAnswer(); Report report = reportRepository.findByReportSetIdAndUserId(reportSetId, userId);
List<Quiz> quizList = report.getQuizSet().getQuizzes();
List<String> answerInputList = reportRequest.getAnswer();
List<Answer> answerList = new ArrayList<>(); List<Answer> answerList = new ArrayList<>();
int allCount = quizList.size();
int correctCount = 0; int correctCount = 0;
User testuser = userRepository.findById(userId).orElse(null);
for (int idx = 0; idx < answerInputList.size(); idx++) { for (int idx = 0; idx < answerInputList.size(); idx++) {
Quiz quiz = quizList.get(idx); Quiz quiz = quizList.get(idx);
String inputAnswer = answerInputList.get(idx); String inputAnswer = answerInputList.get(idx);
boolean isCollect;
Answer answer; Answer answer;
if (quiz.getAnswer().equals(inputAnswer)) { if (quiz.getAnswer().equals(inputAnswer)) {
correctCount++; correctCount++;
answer = Answer.builder() isCollect = true;
.userAnswer(inputAnswer)
.isCorrect(true)
.report(null)
.quiz(quiz)
.build();
} else { } else {
isCollect = false;
}
answer = Answer.builder() answer = Answer.builder()
.userAnswer(inputAnswer) .userAnswer(inputAnswer)
.isCorrect(false) .isCorrect(isCollect)
.report(null) .report(report)
.quiz(quiz) .quiz(quiz)
.build(); .build();
}
answerList.add(answer); answerList.add(answer);
} }
Report report = Report.builder() report.setAllCount(quizList.size());
.user(testuser) report.setCorrectCount(correctCount);
.quizSet(quizSet) report.setReportSet(reportSet);
.allCount(allCount)
.correctCount(correctCount)
.testAt(new Date())
.lectureId(lectureId)
.build();
reportRepository.save(report); reportRepository.save(report);
answerRepository.saveAll(answerList);
for (Answer answer : answerList) {
answer.setReport(report);
answerRepository.save(answer);
}
ReportResponse reportResponse = ReportResponse.builder()
.quizesetId(quizSet.getId())
.userId(testuser.getId())
.title(quizSet.getTitle())
.allCount(allCount)
.correctCount(correctCount)
.testAt(new Date())
.build();
return reportResponse;
} }
@Override @Override
public ReportDetailResponseDto reportDetail(Long repordId) { public ReportDetailResponseDto reportDetail(long reportId) {
Report report = reportRepository.findById(repordId).orElse(null); Report report = reportRepository.findById(reportId).orElseThrow(NoSuchElementException::new);
QuizSet quizSet = quizSetService.findQuizSet(report.getQuizSet().getId()); QuizSet quizSet = quizSetRepository.findById(report.getQuizSet().getId()).orElseThrow(NoSuchElementException::new);
List<QuizDto> correctQuiz = new ArrayList<>(); List<QuizDto> correctQuiz = new ArrayList<>();
List<QuizDto> incorrectQuiz = new ArrayList<>(); List<QuizDto> incorrectQuiz = new ArrayList<>();
@ -176,56 +150,54 @@ public class ReportServiceImpl implements ReportService {
return dto; return dto;
} }
@Override @Override
public List<ReportListResponseDto> resultList(Long userId) throws SQLException { public List<ReportSetResponse> findReportSets(long lectureId) {
List<ReportSet> reportSets = reportSetRepository.findByLectureId(lectureId);
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;
return reportSets.stream()
.map(ReportSet::makeReportSetResponse)
.toList();
} }
@Override @Override
public List<ReportListResponseDto> studentResultList(Long lectureId) throws SQLException { public List<ReportResponse> findReports(UUID reportSetId) {
List<Report> reports = reportRepository.findByReportSetId(reportSetId);
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 reports.stream()
.map(Report::makeReportResponse)
.toList();
} }
return reportListResponseDtoList; @Override
public List<ReportResponse> findReports(long lectureId, long userId) {
List<Report> reports = reportRepository.findByLectureIdAndUserId(lectureId, userId);
return reports.stream()
.map(Report::makeReportResponse)
.toList();
}
@Override
public UUID initReportSet(long lectureId, long quizSetId) {
List<Registration> registrations = registrationRepository.findByLectureIdAndStatus(lectureId, RegistrationStatus.ACCEPTED);
ReportSet reportSet = ReportSet.builder()
.lecture(lectureRepository.getReferenceById(lectureId))
.quizSet(quizSetRepository.getReferenceById(quizSetId))
.build();
reportSetRepository.save(reportSet);
QuizSet quizSet = quizSetRepository.getReferenceById(quizSetId);
List<Report> reports = registrations.stream()
.filter(Registration::isAccepted)
.map((Registration registration)-> registration.makeReport(reportSet, quizSet, lectureId))
.toList();
reportRepository.saveAll(reports);
return reportSet.getId();
} }
} }

View File

@ -28,5 +28,6 @@ public interface UserService {
boolean isEmailExist(String email); boolean isEmailExist(String email);
void changeForgottenPassword(String email, String newPassword); void changeForgottenPassword(String email, String newPassword);
} }

View File

@ -5,6 +5,7 @@ import com.edufocus.edufocus.user.model.entity.dto.InfoDto;
import com.edufocus.edufocus.user.model.entity.dto.PasswordDto; import com.edufocus.edufocus.user.model.entity.dto.PasswordDto;
import com.edufocus.edufocus.user.model.entity.dto.RequestJoinDto; import com.edufocus.edufocus.user.model.entity.dto.RequestJoinDto;
import com.edufocus.edufocus.user.model.entity.vo.User; import com.edufocus.edufocus.user.model.entity.vo.User;
import com.edufocus.edufocus.user.model.entity.vo.UserRole;
import com.edufocus.edufocus.user.model.exception.UserException; import com.edufocus.edufocus.user.model.exception.UserException;
import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.model.repository.UserRepository;
import com.edufocus.edufocus.user.util.PasswordUtils; import com.edufocus.edufocus.user.util.PasswordUtils;
@ -157,5 +158,8 @@ public class UserServiceImpl implements UserService {
userRepository.save(user); userRepository.save(user);
} }
@Override
public boolean isTeacher(long id){
return userRepository.findById(id).orElseThrow(IllegalArgumentException::new).getRole() == UserRole.ADMIN;
}
} }

View File

@ -2,40 +2,25 @@ package com.edufocus.edufocus.ws.controller;
import com.edufocus.edufocus.global.constant.RabbitMQConstant; import com.edufocus.edufocus.global.constant.RabbitMQConstant;
import com.edufocus.edufocus.user.util.JWTUtil; import com.edufocus.edufocus.report.service.ReportService;
import com.edufocus.edufocus.ws.entity.dto.MessageDto; import com.edufocus.edufocus.ws.entity.dto.MessageDto;
import com.edufocus.edufocus.ws.entity.dto.QuizDto; import com.edufocus.edufocus.ws.entity.dto.QuizDto;
import com.edufocus.edufocus.ws.entity.dto.ChatUserDto;
import com.edufocus.edufocus.ws.service.ChatService;
import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.event.EventListener;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.messaging.handler.annotation.DestinationVariable; import org.springframework.messaging.handler.annotation.DestinationVariable;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.messaging.SessionConnectedEvent;
import org.springframework.web.socket.messaging.SessionDisconnectEvent;
import org.springframework.web.socket.messaging.SessionSubscribeEvent;
import org.springframework.web.socket.messaging.SessionUnsubscribeEvent;
import java.util.List; import java.util.UUID;
@RestController @RestController
public class ChatController { public class ChatController {
RabbitTemplate rabbitTemplate; RabbitTemplate rabbitTemplate;
ReportService reportService;
public ChatController(RabbitTemplate rabbitTemplate){ public ChatController(RabbitTemplate rabbitTemplate, ReportService reportService){
this.rabbitTemplate = rabbitTemplate; this.rabbitTemplate = rabbitTemplate;
this.reportService = reportService;
} }
@MessageMapping("chat.message.{lectureId}") @MessageMapping("chat.message.{lectureId}")
@ -47,6 +32,10 @@ public class ChatController {
@MessageMapping("chat.quiz.{lectureId}") @MessageMapping("chat.quiz.{lectureId}")
public void quizStart(@DestinationVariable long lectureId, QuizDto quizDto){ public void quizStart(@DestinationVariable long lectureId, QuizDto quizDto){
UUID reportSetId = reportService.initReportSet(lectureId, quizDto.getQuizSetId());
quizDto.setReportSetId(reportSetId);
rabbitTemplate.convertAndSend(RabbitMQConstant.CHAT_EXCHANGE.getConstant(), rabbitTemplate.convertAndSend(RabbitMQConstant.CHAT_EXCHANGE.getConstant(),
RabbitMQConstant.ROUTING_KEY_PREFIX.getConstant() + lectureId, RabbitMQConstant.ROUTING_KEY_PREFIX.getConstant() + lectureId,
quizDto); quizDto);

View File

@ -4,9 +4,12 @@ package com.edufocus.edufocus.ws.entity.dto;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import java.util.UUID;
@Getter @Getter
@Setter @Setter
public class QuizDto { public class QuizDto {
long userId; long userId;
long quizSetId; long quizSetId;
UUID ReportSetId;
} }

View File

@ -1,15 +0,0 @@
package com.edufocus.edufocus.ws.service;
import com.edufocus.edufocus.ws.entity.dto.ChatUserDto;
import java.util.List;
public interface ChatService {
public void saveChatUserInfo(long userId, long channelId, String sessionId);
public ChatUserDto getChatUserInfo(long userId);
public ChatUserDto getChatUserInfo(String sessionId);
public List<ChatUserDto> findChatUsers(long lectureId);
public boolean checkTeacher(ChatUserDto chatUser);
public void closeChatRoom(long chatRoomId);
public void deleteChatUserInfo(long userId);
}

View File

@ -1,83 +0,0 @@
package com.edufocus.edufocus.ws.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.user.model.entity.vo.User;
import com.edufocus.edufocus.user.model.repository.UserRepository;
import com.edufocus.edufocus.ws.entity.dto.ChatUserDto;
import com.edufocus.edufocus.ws.entity.vo.ChatUser;
import com.edufocus.edufocus.ws.repository.ChatUserRepository;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class ChatServiceImpl implements ChatService{
private final ChatUserRepository chatUserRepository;
private final UserRepository userRepository;
private final LectureRepository lectureRepository;
public ChatServiceImpl(ChatUserRepository chatUserRepository, UserRepository userRepository, LectureRepository lectureRepository) {
this.chatUserRepository = chatUserRepository;
this.userRepository = userRepository;
this.lectureRepository = lectureRepository;
}
@Override
public void saveChatUserInfo(long userId, long lectureId, String sessionId) {
User user = userRepository.getReferenceById(userId);
Lecture lecture = lectureRepository.getReferenceById(lectureId);
ChatUser chatUser = ChatUser.builder()
.user(user)
.lecture(lecture)
.sessionId(sessionId)
.build();
chatUserRepository.save(chatUser);
}
@Override
public ChatUserDto getChatUserInfo(long userId) {
ChatUser chatUser = chatUserRepository.findById(userId).orElseThrow(IllegalArgumentException::new);
return chatUser.makeChatUserDto();
}
@Override
public ChatUserDto getChatUserInfo(String sessionId) {
ChatUser chatUser = chatUserRepository.findBySessionId(sessionId);
return chatUser.makeChatUserDto();
}
@Override
public List<ChatUserDto> findChatUsers(long lectureId) {
return chatUserRepository.findByLectureId(lectureId)
.stream()
.map(ChatUser::makeChatUserDto)
.collect(Collectors.toList());
}
@Override
public boolean checkTeacher(ChatUserDto chatUser) {
Lecture lecture = lectureRepository.findByIdAndUserId(chatUser.getLectureId(), chatUser.getUserId());
return lecture != null;
}
@Override
public void closeChatRoom(long lectureId) {
chatUserRepository.deleteByLectureId(lectureId);
}
@Override
public void deleteChatUserInfo(long userId) {
ChatUser chatUser = chatUserRepository.getReferenceById(userId);
chatUserRepository.delete(chatUser);
}
}