feat: Quiz 등록, 조회 요청 형식 변경 반영

This commit is contained in:
kgc9007 2024-07-29 17:54:27 +09:00
parent dc9a6ac3b8
commit 8dd5676a26
18 changed files with 196 additions and 96 deletions

View File

@ -33,28 +33,10 @@ public class QuizController {
, @RequestPart(value = "image", required = false) MultipartFile image) throws IOException {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
String title = quizSetCreateRequest.getTitle();
SetCreateRequest setCreateRequest = new SetCreateRequest(userId, title);
QuizSet quizSet = quizSetService.createQuizSet(setCreateRequest);
if (image != null && !image.isEmpty()) {
String uid = UUID.randomUUID().toString();
String currentPath = "backend/src/main/resources/images/";
File checkPathFile = new File(currentPath);
if (!checkPathFile.exists()) {
checkPathFile.mkdirs();
}
File savingImage = new File(currentPath + uid + "_" + image.getOriginalFilename());
image.transferTo(savingImage.toPath());
String savePath = savingImage.toPath().toString();
quizSet.setImage(savePath);
}
QuizSet quizSet = quizSetService.createQuizSet(userId, quizSetCreateRequest.getTitle());
for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) {
quizService.createQuiz(quizSet.getId(), quizCreateRequest);
quizService.createQuiz(quizSet, quizCreateRequest);
}
return new ResponseEntity<>(HttpStatus.CREATED);
@ -62,7 +44,7 @@ public class QuizController {
@GetMapping("/{quizsetId}")
public ResponseEntity<?> getQuizzes(@PathVariable Long quizsetId) {
QuizSet quizSet = quizSetService.findQuizSet(quizsetId);
QuizSetResponse quizSet = quizSetService.findQuizSetResponse(quizsetId);
return new ResponseEntity<>(quizSet, HttpStatus.OK);
}

View File

@ -0,0 +1,29 @@
package com.edufocus.edufocus.quiz.entity;
import com.fasterxml.jackson.annotation.JsonBackReference;
import jakarta.persistence.*;
import lombok.*;
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Choice {
@Id
@GeneratedValue
private Long id;
@ManyToOne
@JoinColumn(name = "QuizId")
@JsonBackReference
private Quiz quiz;
@Column
private int num;
@Column
private String content;
}

View File

@ -7,10 +7,9 @@ import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class SetCreateRequest {
public class ChoiceCreateRequest {
private Long UserId;
private String title;
private int num;
private String content;
}

View File

@ -1,11 +1,12 @@
package com.edufocus.edufocus.quiz.entity;
import com.edufocus.edufocus.user.model.entity.UserRole;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.*;
import java.util.List;
@Entity
@Getter
@Setter
@ -24,31 +25,17 @@ public class Quiz {
private QuizSet quizSet;
@Column
private String title;
@Column
private String description;
@Column
private String answer;
@Enumerated(EnumType.STRING)
private QuizType quizType;
private String question;
@Column
private String image;
@Column
private String choice1;
private String answer;
@Column
private String choice2;
@Column
private String choice3;
@Column
private String choice4;
@OneToMany(mappedBy = "quiz")
@JsonManagedReference
private List<Choice> choices;
public void setQuizSet(QuizSet quizSet) {
this.quizSet = quizSet;
@ -57,4 +44,12 @@ public class Quiz {
quizSet.getQuizzes().remove(this);
}
}
public void addChoice(Choice choice) {
this.choices.add(choice);
if (choice.getQuiz() != this) {
choice.setQuiz(this);
}
}
}

View File

@ -4,24 +4,16 @@ import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.List;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class QuizCreateRequest {
private String title;
private String description;
private String question;
private String answer;
private String quizType;
private String choice1;
private String choice2;
private String choice3;
private String choice4;
private List<ChoiceCreateRequest> choices;
}

View File

@ -0,0 +1,21 @@
package com.edufocus.edufocus.quiz.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.List;
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class QuizResponse {
private String question;
private String image;
private List<Choice> choices;
}

View File

@ -11,12 +11,8 @@ import java.util.List;
@AllArgsConstructor
public class QuizSetCreateRequest {
private Long UserId;
private String title;
private String image;
private List<QuizCreateRequest> quizzes;
}

View File

@ -0,0 +1,20 @@
package com.edufocus.edufocus.quiz.entity;
import lombok.*;
import java.util.ArrayList;
import java.util.List;
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class QuizSetResponse {
private String title;
private String image;
private List<QuizResponse> quizzes;
}

View File

@ -1,5 +0,0 @@
package com.edufocus.edufocus.quiz.entity;
public enum QuizType {
SINGLE, MULTIPLE
}

View File

@ -0,0 +1,8 @@
package com.edufocus.edufocus.quiz.repository;
import com.edufocus.edufocus.quiz.entity.Choice;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ChoiceRepository extends JpaRepository<Choice, Long> {
}

View File

@ -3,7 +3,6 @@ package com.edufocus.edufocus.quiz.repository;
import com.edufocus.edufocus.quiz.entity.Quiz;
import org.springframework.data.jpa.repository.JpaRepository;
public interface QuizRepository extends JpaRepository<Quiz, Long> {
}

View File

@ -0,0 +1,7 @@
package com.edufocus.edufocus.quiz.service;
import org.springframework.stereotype.Service;
@Service
public interface ChoiceService {
}

View File

@ -0,0 +1,25 @@
package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.Choice;
import com.edufocus.edufocus.quiz.entity.ChoiceCreateRequest;
import com.edufocus.edufocus.quiz.repository.ChoiceRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@Transactional
@RequiredArgsConstructor
public class ChoiceServiceImpl implements ChoiceService {
private final ChoiceRepository choiceRepository;
public void createChoice(ChoiceCreateRequest choiceCreateRequest) {
Choice choice = new Choice().builder()
.num(choiceCreateRequest.getNum())
.content(choiceCreateRequest.getContent())
.build();
choiceRepository.save(choice);
}
}

View File

@ -1,6 +1,7 @@
package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.QuizCreateRequest;
import com.edufocus.edufocus.quiz.entity.QuizSet;
import org.springframework.stereotype.Service;
import java.util.List;
@ -8,8 +9,7 @@ import java.util.List;
@Service
public interface QuizService {
void createQuiz(long quizSetId, QuizCreateRequest
QuizCreateRequest);
void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest);
boolean deleteQuiz(long quizId);
}

View File

@ -1,43 +1,50 @@
package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.Quiz;
import com.edufocus.edufocus.quiz.entity.QuizCreateRequest;
import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.quiz.entity.QuizType;
import com.edufocus.edufocus.quiz.entity.*;
import com.edufocus.edufocus.quiz.repository.ChoiceRepository;
import com.edufocus.edufocus.quiz.repository.QuizRepository;
import com.edufocus.edufocus.quiz.repository.QuizSetRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
@RequiredArgsConstructor
public class QuizServiceImpl implements QuizService {
private final ChoiceRepository choiceRepository;
private final QuizRepository quizRepository;
private final QuizSetRepository quizSetRepository;
@Override
public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) {
QuizSet quizSet = quizSetRepository.findById(quizSetId).get();
public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest) {
Quiz quiz = new Quiz().builder()
.title(quizCreateRequest.getTitle())
.description(quizCreateRequest.getDescription())
.answer(quizCreateRequest.getAnswer())
.quizType(QuizType.valueOf(quizCreateRequest.getQuizType()))
.quizSet(quizSet)
.question(quizCreateRequest.getQuestion())
.answer(quizCreateRequest.getAnswer())
.build();
if (!quiz.getQuizType().equals(QuizType.MULTIPLE)) {
quiz.setChoice1(quizCreateRequest.getChoice1());
quiz.setChoice2(quizCreateRequest.getChoice2());
quiz.setChoice3(quizCreateRequest.getChoice3());
quiz.setChoice4(quizCreateRequest.getChoice4());
List<Choice> choices = new ArrayList<>();
for (ChoiceCreateRequest choiceCreateRequest : quizCreateRequest.getChoices()) {
Choice choice = new Choice().builder()
.quiz(quiz)
.num(choiceCreateRequest.getNum())
.content(choiceCreateRequest.getContent())
.build();
choices.add(choice);
choiceRepository.save(choice);
}
quiz.setChoices(choices);
quizRepository.save(quiz);
}

View File

@ -1,17 +1,20 @@
package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.quiz.entity.SetCreateRequest;
import com.edufocus.edufocus.quiz.entity.QuizSetResponse;
import org.springframework.stereotype.Service;
@Service
public interface QuizSetService {
QuizSet createQuizSet(SetCreateRequest setCreateRequest);
QuizSet createQuizSet(Long userId, String title);
void updateQuizSet(QuizSet quizSet);
void deleteQuizSet(long quizSetId);
QuizSet findQuizSet(long quizSetId);
QuizSetResponse findQuizSetResponse(long quizSetId);
}

View File

@ -9,6 +9,9 @@ import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
@Transactional
@RequiredArgsConstructor
@ -18,16 +21,13 @@ public class QuizSetServiceImpl implements QuizSetService {
private final UserRepository userRepository;
@Override
public QuizSet createQuizSet(SetCreateRequest setCreateRequest) {
public QuizSet createQuizSet(Long userId, String title) {
QuizSet quizSet = new QuizSet();
User user = userRepository.findById(setCreateRequest.getUserId()).get();
User user = userRepository.findById(userId).get();
quizSet.setUser(user);
quizSet.setTitle(setCreateRequest.getTitle());
quizSet.setTitle(title);
return quizSetRepository.save(quizSet);
}
@ -46,4 +46,28 @@ public class QuizSetServiceImpl implements QuizSetService {
public QuizSet findQuizSet(long quizSetId) {
return quizSetRepository.findById(quizSetId).get();
}
@Override
public QuizSetResponse findQuizSetResponse(long quizSetId) {
QuizSet quizSet = findQuizSet(quizSetId);
List<QuizResponse> quizResponses = new ArrayList<>();
for (Quiz quiz : quizSet.getQuizzes()) {
QuizResponse quizResponse = new QuizResponse().builder()
.question(quiz.getQuestion())
.choices(quiz.getChoices())
.build();
quizResponses.add(quizResponse);
}
QuizSetResponse quizSetResponse = new QuizSetResponse().builder()
.title(quizSet.getTitle())
.image(quizSet.getImage())
.quizzes(quizResponses)
.build();
return quizSetResponse;
}
}

View File

@ -26,11 +26,9 @@ public class RegistrationController {
Long lectureId = map.get("lectureId");
if (!registrationServiceImpl.createRegistration(userId, lectureId)) {
String msg = new String("Duplicated Registration");
return new ResponseEntity<>(msg, HttpStatus.CONFLICT);
return new ResponseEntity<>(HttpStatus.CONFLICT);
}
String msg = new String("registration successful");
return new ResponseEntity<>(HttpStatus.CREATED);
}