refactor: Quiz 리팩토링

This commit is contained in:
kgc91747 2024-08-01 15:02:44 +09:00
parent 8c49670a89
commit 3a7204cfba
10 changed files with 41 additions and 48 deletions

View File

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

View File

@ -30,7 +30,7 @@ public class QuizController {
@PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestPart QuizSetCreateRequest quizSetCreateRequest
, @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());
@ -46,16 +46,12 @@ public class QuizController {
public ResponseEntity<?> getQuizzes(@PathVariable Long quizsetId) {
QuizSetResponse quizSet = quizSetService.findQuizSetResponse(quizsetId);
if (quizSet == null) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<>(quizSet, HttpStatus.OK);
}
@DeleteMapping("/{quizsetId}")
public ResponseEntity<?> deleteQuizSet(@RequestHeader("Authorization") String accessToken, @PathVariable Long quizsetId) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
public ResponseEntity<?> deleteQuizSet(@RequestHeader("Authorization") String accessToken, @PathVariable long quizsetId) {
long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
if (!quizSetService.deleteQuizSet(userId, quizsetId)) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
@ -66,7 +62,7 @@ public class QuizController {
@GetMapping
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);
}

View File

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

View File

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

View File

@ -17,7 +17,7 @@ public class Quiz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private long id;
@ManyToOne
@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.JsonManagedReference;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.*;
import java.util.ArrayList;
import java.util.List;
@ -13,11 +12,14 @@ import java.util.List;
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class QuizSet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private long id;
@ManyToOne
@JoinColumn(name = "user_id")
@ -29,7 +31,7 @@ public class QuizSet {
@OneToMany(mappedBy = "quizSet", orphanRemoval = true)
@JsonManagedReference
private List<Quiz> quizzes = new ArrayList<Quiz>();
private List<Quiz> quizzes;
public void addQuiz(Quiz quiz) {
this.quizzes.add(quiz);

View File

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

View File

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