Merge branch 'be/Quiz' into 'backend'

refactor: Quiz 리팩토링

See merge request s11-webmobile1-sub2/S11P12A701!10
This commit is contained in:
박정민 2024-08-01 16:46:15 +09:00
commit 4f64d316d8
10 changed files with 41 additions and 48 deletions

View File

@ -46,7 +46,7 @@ public class LectureServiceImpl implements LectureService {
.time(lectureCreateRequest.getTime()) .time(lectureCreateRequest.getTime())
.build(); .build();
if (image != null && !image.isEmpty()) { if (image != null) {
String uid = UUID.randomUUID().toString(); String uid = UUID.randomUUID().toString();
String imagePath = imagePathProperties.getPath(); String imagePath = imagePathProperties.getPath();

View File

@ -30,7 +30,7 @@ public class QuizController {
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestPart QuizSetCreateRequest quizSetCreateRequest public ResponseEntity<?> createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestPart QuizSetCreateRequest quizSetCreateRequest
, @RequestPart(value = "images", required = false) List<MultipartFile> images) throws IOException { , @RequestPart(value = "images", required = false) List<MultipartFile> images) throws IOException {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
QuizSet quizSet = quizSetService.createQuizSet(userId, quizSetCreateRequest.getTitle()); QuizSet quizSet = quizSetService.createQuizSet(userId, quizSetCreateRequest.getTitle());
@ -46,16 +46,12 @@ public class QuizController {
public ResponseEntity<?> getQuizzes(@PathVariable Long quizsetId) { public ResponseEntity<?> getQuizzes(@PathVariable Long quizsetId) {
QuizSetResponse quizSet = quizSetService.findQuizSetResponse(quizsetId); QuizSetResponse quizSet = quizSetService.findQuizSetResponse(quizsetId);
if (quizSet == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(quizSet, HttpStatus.OK); return new ResponseEntity<>(quizSet, HttpStatus.OK);
} }
@DeleteMapping("/{quizsetId}") @DeleteMapping("/{quizsetId}")
public ResponseEntity<?> deleteQuizSet(@RequestHeader("Authorization") String accessToken, @PathVariable Long quizsetId) { public ResponseEntity<?> deleteQuizSet(@RequestHeader("Authorization") String accessToken, @PathVariable long quizsetId) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
if (!quizSetService.deleteQuizSet(userId, quizsetId)) { if (!quizSetService.deleteQuizSet(userId, quizsetId)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND); return new ResponseEntity<>(HttpStatus.NOT_FOUND);
@ -66,7 +62,7 @@ public class QuizController {
@GetMapping @GetMapping
public ResponseEntity<?> getQuizSets(@RequestHeader("Authorization") String accessToken) { public ResponseEntity<?> getQuizSets(@RequestHeader("Authorization") String accessToken) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
return new ResponseEntity<>(quizSetService.findMyQuizSetResponses(userId), HttpStatus.OK); return new ResponseEntity<>(quizSetService.findMyQuizSetResponses(userId), HttpStatus.OK);
} }

View File

@ -14,7 +14,7 @@ public class Choice {
@Id @Id
@GeneratedValue @GeneratedValue
private Long id; private long id;
@ManyToOne @ManyToOne
@JoinColumn(name = "QuizId") @JoinColumn(name = "QuizId")

View File

@ -8,7 +8,7 @@ import lombok.*;
@AllArgsConstructor @AllArgsConstructor
public class MyQuizSetResponse { public class MyQuizSetResponse {
private Long quizSetId; private long quizSetId;
private String title; private String title;
} }

View File

@ -17,7 +17,7 @@ public class Quiz {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private long id;
@ManyToOne @ManyToOne
@JoinColumn(name = "quizset_id") @JoinColumn(name = "quizset_id")

View File

@ -4,8 +4,7 @@ import com.edufocus.edufocus.user.model.entity.User;
import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.Getter; import lombok.*;
import lombok.Setter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -13,11 +12,14 @@ import java.util.List;
@Entity @Entity
@Getter @Getter
@Setter @Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class QuizSet { public class QuizSet {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private long id;
@ManyToOne @ManyToOne
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
@ -29,7 +31,7 @@ public class QuizSet {
@OneToMany(mappedBy = "quizSet", orphanRemoval = true) @OneToMany(mappedBy = "quizSet", orphanRemoval = true)
@JsonManagedReference @JsonManagedReference
private List<Quiz> quizzes = new ArrayList<Quiz>(); private List<Quiz> quizzes;
public void addQuiz(Quiz quiz) { public void addQuiz(Quiz quiz) {
this.quizzes.add(quiz); this.quizzes.add(quiz);

View File

@ -7,5 +7,5 @@ import java.util.List;
public interface QuizSetRepository extends JpaRepository<QuizSet, Long> { public interface QuizSetRepository extends JpaRepository<QuizSet, Long> {
List<QuizSet> findQuizSetsByUserId(Long userId); List<QuizSet> findQuizSetsByUserId(long userId);
} }

View File

@ -10,15 +10,15 @@ import java.util.List;
@Service @Service
public interface QuizSetService { public interface QuizSetService {
QuizSet createQuizSet(Long userId, String title); QuizSet createQuizSet(long userId, String title);
void updateQuizSet(QuizSet quizSet); void updateQuizSet(QuizSet quizSet);
boolean deleteQuizSet(Long userId, Long quizSetId); boolean deleteQuizSet(long userId, long quizSetId);
QuizSet findQuizSet(Long quizSetId); QuizSet findQuizSet(long quizSetId);
QuizSetResponse findQuizSetResponse(Long quizSetId); QuizSetResponse findQuizSetResponse(long quizSetId);
List<MyQuizSetResponse> findMyQuizSetResponses(Long userId); List<MyQuizSetResponse> findMyQuizSetResponses(long userId);
} }

View File

@ -10,7 +10,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.NoSuchElementException;
@Service @Service
@Transactional @Transactional
@ -22,12 +22,13 @@ public class QuizSetServiceImpl implements QuizSetService {
private final UserRepository userRepository; private final UserRepository userRepository;
@Override @Override
public QuizSet createQuizSet(Long userId, String title) { public QuizSet createQuizSet(long userId, String title) {
QuizSet quizSet = new QuizSet(); User user = userRepository.findById(userId).orElseThrow(NoSuchElementException::new);
User user = userRepository.findById(userId).get(); QuizSet quizSet = QuizSet.builder()
quizSet.setUser(user); .user(user)
quizSet.setTitle(title); .title(title)
.build();
return quizSetRepository.save(quizSet); return quizSetRepository.save(quizSet);
} }
@ -38,10 +39,10 @@ public class QuizSetServiceImpl implements QuizSetService {
} }
@Override @Override
public boolean deleteQuizSet(Long userId, Long quizSetId) { public boolean deleteQuizSet(long userId, long quizSetId) {
Optional<QuizSet> quizSet = quizSetRepository.findById(quizSetId); QuizSet quizSet = quizSetRepository.findById(quizSetId).orElseThrow(NoSuchElementException::new);
if (quizSet.isEmpty() || userId != quizSet.get().getUser().getId()) { if (userId != quizSet.getUser().getId()) {
return false; return false;
} }
@ -50,22 +51,17 @@ public class QuizSetServiceImpl implements QuizSetService {
} }
@Override @Override
public QuizSet findQuizSet(Long quizSetId) { public QuizSet findQuizSet(long quizSetId) {
return quizSetRepository.findById(quizSetId).get(); return quizSetRepository.findById(quizSetId).orElseThrow(NoSuchElementException::new);
} }
@Override @Override
public QuizSetResponse findQuizSetResponse(Long quizSetId) { public QuizSetResponse findQuizSetResponse(long quizSetId) {
Optional<QuizSet> quizSet = quizSetRepository.findById(quizSetId); QuizSet quizSet = quizSetRepository.findById(quizSetId).orElseThrow(NoSuchElementException::new);
if (quizSet.isEmpty()) {
return null;
}
List<QuizResponse> quizResponses = new ArrayList<>(); List<QuizResponse> quizResponses = new ArrayList<>();
for (Quiz quiz : quizSet.get().getQuizzes()) { for (Quiz quiz : quizSet.getQuizzes()) {
QuizResponse quizResponse = new QuizResponse().builder() QuizResponse quizResponse = QuizResponse.builder()
.question(quiz.getQuestion()) .question(quiz.getQuestion())
.image(quiz.getImage()) .image(quiz.getImage())
.choices(quiz.getChoices()) .choices(quiz.getChoices())
@ -73,21 +69,20 @@ public class QuizSetServiceImpl implements QuizSetService {
quizResponses.add(quizResponse); quizResponses.add(quizResponse);
} }
QuizSetResponse quizSetResponse = new QuizSetResponse().builder() return QuizSetResponse.builder()
.title(quizSet.get().getTitle()) .title(quizSet.getTitle())
.quizzes(quizResponses) .quizzes(quizResponses)
.build(); .build();
return quizSetResponse;
} }
@Override @Override
public List<MyQuizSetResponse> findMyQuizSetResponses(Long userId) { public List<MyQuizSetResponse> findMyQuizSetResponses(long userId) {
List<QuizSet> quizSetList = quizSetRepository.findQuizSetsByUserId(userId); List<QuizSet> quizSetList = quizSetRepository.findQuizSetsByUserId(userId);
List<MyQuizSetResponse> myQuizSetResponses = new ArrayList<>(); List<MyQuizSetResponse> myQuizSetResponses = new ArrayList<>();
for (QuizSet quizSet : quizSetList) { for (QuizSet quizSet : quizSetList) {
MyQuizSetResponse myQuizSetResponse = new MyQuizSetResponse().builder() MyQuizSetResponse myQuizSetResponse = MyQuizSetResponse.builder()
.quizSetId(quizSet.getId()) .quizSetId(quizSet.getId())
.title(quizSet.getTitle()) .title(quizSet.getTitle())
.build(); .build();

View File

@ -55,7 +55,7 @@ public class ReportServiceImpl implements ReportService {
for (Quiz quiz : quizList) { for (Quiz quiz : quizList) {
for (AnswerInput answerInput : answerInputList) { for (AnswerInput answerInput : answerInputList) {
if (quiz.getId().equals(answerInput.getAnswerinputID())) { if (quiz.getId() == answerInput.getAnswerinputID()) {
if(quiz.getAnswer().equals(answerInput.getAnswer())) { if(quiz.getAnswer().equals(answerInput.getAnswer())) {
correctCount++; correctCount++;