Merge pull request #82 from TeamBNBN/BE/useranswer

feat: user report, answer 채점기능
This commit is contained in:
김기창 2024-07-22 16:12:16 +09:00 committed by GitHub
commit 9499b06208
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 304 additions and 0 deletions

View File

@ -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<Report> submit(@RequestBody ReportRequset reportRequset) throws SQLException {
Report report = reportService.grading(reportRequset);
return new ResponseEntity<>(report,HttpStatus.CREATED);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@ -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<AnswerInput> answerInputList;
// List<answerDto> a
// userID :
// quizSetId :
// answerList : [
// { ans1 : 1},
// {ans2 : 2}
// ]
}

View File

@ -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<Answer,Long> {
}

View File

@ -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<Report, Long> {
}

View File

@ -0,0 +1,8 @@
package com.edufocus.edufocus.report.service;
import com.edufocus.edufocus.report.entity.Answer;
public interface AnswerService {
void save(Answer answer);
}

View File

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

View File

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

View File

@ -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<Quiz> quizList = quizSet.getQuizzes();
List<AnswerInput> 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;
}
}