Merge pull request #140 from TeamBNBN/be/Quiz

feat: Quiz 등록, 조회 요청 형식 변경 반영
This commit is contained in:
Jungmin 2024-07-30 14:33:46 +09:00 committed by GitHub
commit 3fc1f37b1a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
18 changed files with 253 additions and 123 deletions

View File

@ -12,9 +12,8 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.List;
@RestController @RestController
@RequestMapping("/quiz") @RequestMapping("/quiz")
@ -30,31 +29,14 @@ 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 = "image", required = false) MultipartFile image) 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));
String title = quizSetCreateRequest.getTitle(); QuizSet quizSet = quizSetService.createQuizSet(userId, 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);
}
int imageIdx = 0;
for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) { for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) {
quizService.createQuiz(quizSet.getId(), quizCreateRequest); quizService.createQuiz(quizSet, quizCreateRequest, images.get(imageIdx++));
} }
return new ResponseEntity<>(HttpStatus.CREATED); return new ResponseEntity<>(HttpStatus.CREATED);
@ -62,8 +44,30 @@ public class QuizController {
@GetMapping("/{quizsetId}") @GetMapping("/{quizsetId}")
public ResponseEntity<?> getQuizzes(@PathVariable Long quizsetId) { public ResponseEntity<?> getQuizzes(@PathVariable Long quizsetId) {
QuizSet quizSet = quizSetService.findQuizSet(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}")
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);
}
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
@GetMapping
public ResponseEntity<?> getQuizSets(@RequestHeader("Authorization") String accessToken) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
return new ResponseEntity<>(quizSetService.findMyQuizSetResponses(userId), 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 @Getter
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class SetCreateRequest { public class ChoiceCreateRequest {
private Long UserId; private int num;
private String title;
private String content;
} }

View File

@ -0,0 +1,14 @@
package com.edufocus.edufocus.quiz.entity;
import lombok.*;
@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MyQuizSetResponse {
private Long quizSetId;
private String title;
}

View File

@ -1,11 +1,12 @@
package com.edufocus.edufocus.quiz.entity; 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.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
import java.util.List;
@Entity @Entity
@Getter @Getter
@Setter @Setter
@ -24,31 +25,17 @@ public class Quiz {
private QuizSet quizSet; private QuizSet quizSet;
@Column @Column
private String title; private String question;
@Column
private String description;
@Column
private String answer;
@Enumerated(EnumType.STRING)
private QuizType quizType;
@Column @Column
private String image; private String image;
@Column @Column
private String choice1; private String answer;
@Column @OneToMany(mappedBy = "quiz", orphanRemoval = true, cascade = CascadeType.ALL)
private String choice2; @JsonManagedReference
private List<Choice> choices;
@Column
private String choice3;
@Column
private String choice4;
public void setQuizSet(QuizSet quizSet) { public void setQuizSet(QuizSet quizSet) {
this.quizSet = quizSet; this.quizSet = quizSet;
@ -57,4 +44,12 @@ public class Quiz {
quizSet.getQuizzes().remove(this); 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.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.util.List;
@Getter @Getter
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public class QuizCreateRequest { public class QuizCreateRequest {
private String title; private String question;
private String description;
private String answer; private String answer;
private String quizType; private List<ChoiceCreateRequest> choices;
private String choice1;
private String choice2;
private String choice3;
private String choice4;
} }

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

@ -27,12 +27,9 @@ public class QuizSet {
@Column @Column
private String title; private String title;
@Column @OneToMany(mappedBy = "quizSet", orphanRemoval = true)
private String image;
@OneToMany(mappedBy = "quizSet")
@JsonManagedReference @JsonManagedReference
private List<Quiz> quizzes = new ArrayList<Quiz>(); private List<Quiz> quizzes = new ArrayList<Quiz>();
public void addQuiz(Quiz quiz) { public void addQuiz(Quiz quiz) {
this.quizzes.add(quiz); this.quizzes.add(quiz);

View File

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

View File

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

View File

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

View File

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

View File

@ -3,6 +3,9 @@ package com.edufocus.edufocus.quiz.repository;
import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.quiz.entity.QuizSet;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface QuizSetRepository extends JpaRepository<QuizSet, Long> { public interface QuizSetRepository extends JpaRepository<QuizSet, Long> {
List<QuizSet> findQuizSetsByUserId(Long userId);
} }

View File

@ -1,15 +1,16 @@
package com.edufocus.edufocus.quiz.service; package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; import com.edufocus.edufocus.quiz.entity.QuizCreateRequest;
import com.edufocus.edufocus.quiz.entity.QuizSet;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.List; import java.util.List;
@Service @Service
public interface QuizService { public interface QuizService {
void createQuiz(long quizSetId, QuizCreateRequest void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException;
QuizCreateRequest);
boolean deleteQuiz(long quizId);
} }

View File

@ -1,14 +1,18 @@
package com.edufocus.edufocus.quiz.service; package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.Quiz; import com.edufocus.edufocus.quiz.entity.*;
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.repository.QuizRepository; import com.edufocus.edufocus.quiz.repository.QuizRepository;
import com.edufocus.edufocus.quiz.repository.QuizSetRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service @Service
@Transactional @Transactional
@ -17,39 +21,45 @@ public class QuizServiceImpl implements QuizService {
private final QuizRepository quizRepository; private final QuizRepository quizRepository;
private final QuizSetRepository quizSetRepository;
@Override @Override
public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) { public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException {
QuizSet quizSet = quizSetRepository.findById(quizSetId).get();
Quiz quiz = new Quiz().builder() Quiz quiz = new Quiz().builder()
.title(quizCreateRequest.getTitle())
.description(quizCreateRequest.getDescription())
.answer(quizCreateRequest.getAnswer())
.quizType(QuizType.valueOf(quizCreateRequest.getQuizType()))
.quizSet(quizSet) .quizSet(quizSet)
.question(quizCreateRequest.getQuestion())
.answer(quizCreateRequest.getAnswer())
.build(); .build();
if (!quiz.getQuizType().equals(QuizType.MULTIPLE)) { List<Choice> choices = new ArrayList<>();
quiz.setChoice1(quizCreateRequest.getChoice1());
quiz.setChoice2(quizCreateRequest.getChoice2()); for (ChoiceCreateRequest choiceCreateRequest : quizCreateRequest.getChoices()) {
quiz.setChoice3(quizCreateRequest.getChoice3()); Choice choice = new Choice().builder()
quiz.setChoice4(quizCreateRequest.getChoice4()); .quiz(quiz)
.num(choiceCreateRequest.getNum())
.content(choiceCreateRequest.getContent())
.build();
choices.add(choice);
}
quiz.setChoices(choices);
if (quizImage != null && !quizImage.isEmpty()) {
String uid = UUID.randomUUID().toString();
String currentPath = "backend/src/main/resources/images/quizzes/";
File checkPathFile = new File(currentPath);
if (!checkPathFile.exists()) {
checkPathFile.mkdirs();
}
File savingImage = new File(currentPath + uid + "_" + quizImage.getOriginalFilename());
quizImage.transferTo(savingImage.toPath());
String savePath = savingImage.toPath().toString();
quiz.setImage(savePath);
} }
quizRepository.save(quiz); quizRepository.save(quiz);
} }
@Override
public boolean deleteQuiz(long quizId) {
// 유저 아이디 정보 조회 검증 로직 추가 예정
// jwt -> 로그인 유저 정보 조회
// quizId -> 퀴즈 정보 조회 -> 퀴즈셋 정보 조회
// 퀴즈셋 생성자와 로그인 유저의 id값이 일치하는지 확인 -> 불일치시 삭제 실패
quizRepository.deleteById(quizId);
return true;
}
} }

View File

@ -1,17 +1,24 @@
package com.edufocus.edufocus.quiz.service; package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.MyQuizSetResponse;
import com.edufocus.edufocus.quiz.entity.QuizSet; 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; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
public interface QuizSetService { public interface QuizSetService {
QuizSet createQuizSet(SetCreateRequest setCreateRequest); QuizSet createQuizSet(Long userId, String title);
void updateQuizSet(QuizSet quizSet); void updateQuizSet(QuizSet quizSet);
void deleteQuizSet(long quizSetId); boolean deleteQuizSet(Long userId, Long quizSetId);
QuizSet findQuizSet(long quizSetId); QuizSet findQuizSet(Long quizSetId);
QuizSetResponse findQuizSetResponse(Long quizSetId);
List<MyQuizSetResponse> findMyQuizSetResponses(Long userId);
} }

View File

@ -1,7 +1,6 @@
package com.edufocus.edufocus.quiz.service; package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.*; import com.edufocus.edufocus.quiz.entity.*;
import com.edufocus.edufocus.quiz.repository.QuizRepository;
import com.edufocus.edufocus.quiz.repository.QuizSetRepository; import com.edufocus.edufocus.quiz.repository.QuizSetRepository;
import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.model.repository.UserRepository;
@ -9,6 +8,10 @@ import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service @Service
@Transactional @Transactional
@RequiredArgsConstructor @RequiredArgsConstructor
@ -18,16 +21,13 @@ public class QuizSetServiceImpl implements QuizSetService {
private final UserRepository userRepository; private final UserRepository userRepository;
@Override @Override
public QuizSet createQuizSet(SetCreateRequest setCreateRequest) { public QuizSet createQuizSet(Long userId, String title) {
QuizSet quizSet = new QuizSet(); QuizSet quizSet = new QuizSet();
User user = userRepository.findById(setCreateRequest.getUserId()).get(); User user = userRepository.findById(userId).get();
quizSet.setUser(user); quizSet.setUser(user);
quizSet.setTitle(title);
quizSet.setTitle(setCreateRequest.getTitle());
return quizSetRepository.save(quizSet); return quizSetRepository.save(quizSet);
} }
@ -38,12 +38,65 @@ public class QuizSetServiceImpl implements QuizSetService {
} }
@Override @Override
public void deleteQuizSet(long quizSetId) { public boolean deleteQuizSet(Long userId, Long quizSetId) {
Optional<QuizSet> quizSet = quizSetRepository.findById(quizSetId);
if (quizSet.isEmpty() || userId != quizSet.get().getUser().getId()) {
return false;
}
quizSetRepository.deleteById(quizSetId); quizSetRepository.deleteById(quizSetId);
return true;
} }
@Override @Override
public QuizSet findQuizSet(long quizSetId) { public QuizSet findQuizSet(Long quizSetId) {
return quizSetRepository.findById(quizSetId).get(); return quizSetRepository.findById(quizSetId).get();
} }
@Override
public QuizSetResponse findQuizSetResponse(Long quizSetId) {
Optional<QuizSet> quizSet = quizSetRepository.findById(quizSetId);
if (quizSet.isEmpty()) {
return null;
}
List<QuizResponse> quizResponses = new ArrayList<>();
for (Quiz quiz : quizSet.get().getQuizzes()) {
QuizResponse quizResponse = new QuizResponse().builder()
.question(quiz.getQuestion())
.image(quiz.getImage())
.choices(quiz.getChoices())
.build();
quizResponses.add(quizResponse);
}
QuizSetResponse quizSetResponse = new QuizSetResponse().builder()
.title(quizSet.get().getTitle())
.quizzes(quizResponses)
.build();
return quizSetResponse;
}
@Override
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()
.quizSetId(quizSet.getId())
.title(quizSet.getTitle())
.build();
myQuizSetResponses.add(myQuizSetResponse);
}
return myQuizSetResponses;
}
} }

View File

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