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 new file mode 100644 index 0000000..20f82e1 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/controller/ReportController.java @@ -0,0 +1,36 @@ +package com.edufocus.edufocus.report.controller; + +import com.edufocus.edufocus.report.entity.Report; +import com.edufocus.edufocus.report.entity.ReportRequset; +import com.edufocus.edufocus.report.service.ReportService; +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 java.sql.SQLException; + +@RestController +@RequestMapping("/report") +@Slf4j +@RequiredArgsConstructor +public class ReportController{ + + + private final ReportService reportService; + + + + + @PostMapping("/submit") + public ResponseEntity submit(@RequestBody ReportRequset reportRequset) throws SQLException { + Report report = reportService.grading(reportRequset); + return new ResponseEntity<>(report,HttpStatus.CREATED); + + } + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/Answer.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/Answer.java new file mode 100644 index 0000000..3204f24 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/Answer.java @@ -0,0 +1,33 @@ +package com.edufocus.edufocus.report.entity; + + +import com.edufocus.edufocus.quiz.entity.Quiz; +import jakarta.persistence.*; +import lombok.*; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class Answer { + + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + private String userAnswer; + private boolean isCorrect; + + @ManyToOne + @JoinColumn(name= "report_id") + private Report report; + + @ManyToOne + @JoinColumn(name= "quiz_id") + private Quiz quiz; + + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/AnswerInput.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/AnswerInput.java new file mode 100644 index 0000000..c237616 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/AnswerInput.java @@ -0,0 +1,11 @@ +package com.edufocus.edufocus.report.entity; + +import lombok.Getter; + +@Getter +public class AnswerInput { + + Long answerinputID; + String answer; + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/QuizSubmissionDto.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/QuizSubmissionDto.java new file mode 100644 index 0000000..489d896 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/QuizSubmissionDto.java @@ -0,0 +1,6 @@ +package com.edufocus.edufocus.report.entity; + +public class QuizSubmissionDto { + private Long quizSetId; + private Long userId; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/Report.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/Report.java new file mode 100644 index 0000000..6a011d5 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/Report.java @@ -0,0 +1,35 @@ +package com.edufocus.edufocus.report.entity; + +import com.edufocus.edufocus.quiz.entity.QuizSet; +import com.edufocus.edufocus.user.model.entity.User; +import jakarta.persistence.*; +import lombok.*; + +import java.util.Date; + +@Entity +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Builder +public class Report { + + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private int allCount; + private int correctCount; + private Date testAt; + + @ManyToOne + @JoinColumn(name= "user_id") + private User user; + + @ManyToOne + @JoinColumn(name= "quizset_id") + private QuizSet quizSet; + +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/entity/ReportRequset.java b/backend/src/main/java/com/edufocus/edufocus/report/entity/ReportRequset.java new file mode 100644 index 0000000..788f87f --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/entity/ReportRequset.java @@ -0,0 +1,25 @@ +package com.edufocus.edufocus.report.entity; + +import lombok.Getter; +import lombok.Setter; + +import java.util.List; + +@Getter +@Setter +public class ReportRequset { + + + private Long userId; + private Long quizsetId; + List answerInputList; + + +// List a +// userID : +// quizSetId : +// answerList : [ +// { ans1 : 1}, +// {ans2 : 2} +// ] +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/repository/AnswerRepository.java b/backend/src/main/java/com/edufocus/edufocus/report/repository/AnswerRepository.java new file mode 100644 index 0000000..12f76ae --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/repository/AnswerRepository.java @@ -0,0 +1,7 @@ +package com.edufocus.edufocus.report.repository; + +import com.edufocus.edufocus.report.entity.Answer; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AnswerRepository extends JpaRepository { +} 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 new file mode 100644 index 0000000..dd5eca7 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/repository/ReportRepository.java @@ -0,0 +1,9 @@ +package com.edufocus.edufocus.report.repository; + +import com.edufocus.edufocus.report.entity.Report; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ReportRepository extends JpaRepository { +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/service/AnswerService.java b/backend/src/main/java/com/edufocus/edufocus/report/service/AnswerService.java new file mode 100644 index 0000000..2c4c65a --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/service/AnswerService.java @@ -0,0 +1,8 @@ +package com.edufocus.edufocus.report.service; + +import com.edufocus.edufocus.report.entity.Answer; + +public interface AnswerService { + + void save(Answer answer); +} diff --git a/backend/src/main/java/com/edufocus/edufocus/report/service/AnswerServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/report/service/AnswerServiceImpl.java new file mode 100644 index 0000000..b838846 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/service/AnswerServiceImpl.java @@ -0,0 +1,22 @@ +package com.edufocus.edufocus.report.service; + +import com.edufocus.edufocus.report.entity.Answer; +import com.edufocus.edufocus.report.repository.AnswerRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@Transactional +@RequiredArgsConstructor +public class AnswerServiceImpl implements AnswerService{ + + private final AnswerRepository answerRepository; + + @Override + public void save(Answer answer) { + + answerRepository.save(answer); + + } +} 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 new file mode 100644 index 0000000..89de34f --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportService.java @@ -0,0 +1,13 @@ +package com.edufocus.edufocus.report.service; + +import com.edufocus.edufocus.report.entity.Report; +import com.edufocus.edufocus.report.entity.ReportRequset; +import org.springframework.stereotype.Service; + +import java.sql.SQLException; + +@Service +public interface ReportService { + Report grading(ReportRequset reportRequset) 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 new file mode 100644 index 0000000..87f0af0 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/report/service/ReportServiceImpl.java @@ -0,0 +1,99 @@ +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.service.QuizService; +import com.edufocus.edufocus.quiz.service.QuizSetService; +import com.edufocus.edufocus.report.entity.Answer; +import com.edufocus.edufocus.report.entity.AnswerInput; +import com.edufocus.edufocus.report.entity.Report; +import com.edufocus.edufocus.report.entity.ReportRequset; +import com.edufocus.edufocus.report.repository.ReportRepository; +import com.edufocus.edufocus.user.model.entity.User; +import com.edufocus.edufocus.user.model.repository.UserRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +import java.sql.SQLException; +import java.util.Date; +import java.util.List; + +@Service +@Transactional +@RequiredArgsConstructor +public class ReportServiceImpl implements ReportService { + + private final QuizSetService quizSetService; + private final QuizService quizService; + + private final ReportRepository reportRepository; + private final QuizRepository quizRepository; + + private final AnswerService answerService; + private final UserRepository userRepository; + + + @Override + public Report grading(ReportRequset reportRequset) throws SQLException { + + + QuizSet quizSet = quizSetService.findQuizSet(reportRequset.getQuizsetId()); + + List quizList = quizSet.getQuizzes(); + List answerInputList = reportRequset.getAnswerInputList(); + + Report report = new Report(); + + Long reportNum = report.getId(); + int allCount= quizList.size(); + int correctCount =0 ; + + User testuser= userRepository.findById(reportRequset.getUserId()).orElse(null); + + for (Quiz quiz : quizList) { + for (AnswerInput answerInput : answerInputList) { + if (quiz.getId().equals(answerInput.getAnswerinputID())) { + + if(quiz.getAnswer().equals(answerInput.getAnswer())) { + correctCount++; + + Answer answer = Answer.builder() + .userAnswer(answerInput.getAnswer()) + .isCorrect(true) + .report(report) + .quiz(quiz) + .build(); + + } + else{ + Answer answer = Answer.builder() + .userAnswer(answerInput.getAnswer()) + .isCorrect(false) + .report(report) + .quiz(quiz) + .build(); + + + + } + + } + + } + } + + report = Report.builder() + .user(testuser) + .quizSet(quizSet) + .allCount(allCount) + .correctCount(correctCount) + .testAt(new Date()).build(); + + reportRepository.save(report); + return report; + + + } +}