feat: Quiz

퀴즈 등록, 조회 기능 추가
This commit is contained in:
kgc9007 2024-07-22 13:06:51 +09:00
parent 16fc14a19d
commit 65d43acb91
10 changed files with 67 additions and 44 deletions

View File

@ -1,17 +1,19 @@
package com.edufocus.edufocus.quiz.controller; package com.edufocus.edufocus.quiz.controller;
import com.edufocus.edufocus.quiz.entity.*; import com.edufocus.edufocus.quiz.entity.*;
import com.edufocus.edufocus.quiz.repository.QuizRepository;
import com.edufocus.edufocus.quiz.service.QuizService; import com.edufocus.edufocus.quiz.service.QuizService;
import com.edufocus.edufocus.quiz.service.QuizSetService; import com.edufocus.edufocus.quiz.service.QuizSetService;
import com.edufocus.edufocus.user.model.service.UserService; import com.edufocus.edufocus.user.model.service.UserService;
import com.edufocus.edufocus.user.util.JWTUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import java.util.ArrayList;
import org.springframework.web.bind.annotation.RestController; import java.util.List;
@RestController @RestController
@RequestMapping("/quiz") @RequestMapping("/quiz")
@ -25,10 +27,12 @@ public class QuizController {
private final UserService userService; private final UserService userService;
@PostMapping private final JWTUtil jwtUtil;
public ResponseEntity<?> createQuizSet(@RequestBody QuizSetCreateRequest quizSetCreateRequest) { private final QuizRepository quizRepository;
Long userId = 1L; @PostMapping
public ResponseEntity<?> createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestBody QuizSetCreateRequest quizSetCreateRequest) {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
String title = quizSetCreateRequest.getTitle(); String title = quizSetCreateRequest.getTitle();
String image = quizSetCreateRequest.getImage(); String image = quizSetCreateRequest.getImage();
@ -43,4 +47,11 @@ public class QuizController {
return new ResponseEntity<>(HttpStatus.CREATED); return new ResponseEntity<>(HttpStatus.CREATED);
} }
@GetMapping("/{quizsetId}")
public ResponseEntity<?> getQuizzes(@PathVariable Long quizsetId) {
QuizSet quizSet = quizSetService.findQuizSet(quizsetId);
return new ResponseEntity<>(quizSet, HttpStatus.OK);
}
} }

View File

@ -1,20 +0,0 @@
package com.edufocus.edufocus.quiz.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class MutipleQuizCreateRequest extends QuizCreateRequest {
private String choice1;
private String choice2;
private String choice3;
private String choice4;
}

View File

@ -1,12 +1,16 @@
package com.edufocus.edufocus.quiz.entity; package com.edufocus.edufocus.quiz.entity;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.Getter; import lombok.*;
import lombok.Setter;
@Entity @Entity
@Getter @Getter
@Setter @Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Quiz { public class Quiz {
@Id @Id
@ -15,6 +19,7 @@ public class Quiz {
@ManyToOne @ManyToOne
@JoinColumn(name = "quizset_id") @JoinColumn(name = "quizset_id")
@JsonBackReference
private QuizSet quizSet; private QuizSet quizSet;
@Column @Column

View File

@ -3,7 +3,6 @@ package com.edufocus.edufocus.quiz.entity;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.ToString;
@Getter @Getter
@NoArgsConstructor @NoArgsConstructor
@ -14,10 +13,17 @@ public class QuizCreateRequest {
private String description; private String description;
private boolean isSingle;
private String answer; private String answer;
private boolean isSingle;
private String image; private String image;
private String choice1;
private String choice2;
private String choice3;
private String choice4;
} }

View File

@ -1,6 +1,8 @@
package com.edufocus.edufocus.quiz.entity; package com.edufocus.edufocus.quiz.entity;
import com.edufocus.edufocus.user.model.entity.User; import com.edufocus.edufocus.user.model.entity.User;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -19,6 +21,7 @@ public class QuizSet {
@ManyToOne @ManyToOne
@JoinColumn(name = "user_id") @JoinColumn(name = "user_id")
@JsonBackReference
private User user; private User user;
@Column @Column
@ -28,6 +31,7 @@ public class QuizSet {
private String image; private String image;
@OneToMany(mappedBy = "quizSet") @OneToMany(mappedBy = "quizSet")
@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) {

View File

@ -3,5 +3,7 @@ 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

@ -1,9 +1,10 @@
package com.edufocus.edufocus.quiz.service; package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.MutipleQuizCreateRequest;
import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; import com.edufocus.edufocus.quiz.entity.QuizCreateRequest;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
public interface QuizService { public interface QuizService {

View File

@ -1,6 +1,5 @@
package com.edufocus.edufocus.quiz.service; package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.MutipleQuizCreateRequest;
import com.edufocus.edufocus.quiz.entity.Quiz; import com.edufocus.edufocus.quiz.entity.Quiz;
import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; import com.edufocus.edufocus.quiz.entity.QuizCreateRequest;
import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.quiz.entity.QuizSet;
@ -10,6 +9,8 @@ import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
@Transactional @Transactional
@RequiredArgsConstructor @RequiredArgsConstructor
@ -23,15 +24,20 @@ public class QuizServiceImpl implements QuizService {
public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) { public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) {
QuizSet quizSet = quizSetRepository.findById(quizSetId).get(); QuizSet quizSet = quizSetRepository.findById(quizSetId).get();
Quiz quiz = new Quiz(); Quiz quiz = new Quiz().builder()
.title(quizCreateRequest.getTitle())
.description(quizCreateRequest.getDescription())
.answer(quizCreateRequest.getAnswer())
.image(quizCreateRequest.getImage())
.quizSet(quizSet)
.build();
quiz.setTitle(quizCreateRequest.getTitle()); if (!quiz.isSingle()) {
quiz.setDescription(quizCreateRequest.getDescription()); quiz.setChoice1(quizCreateRequest.getChoice1());
quiz.setAnswer(quizCreateRequest.getAnswer()); quiz.setChoice2(quizCreateRequest.getChoice2());
quiz.setImage(quizCreateRequest.getImage()); quiz.setChoice3(quizCreateRequest.getChoice3());
quiz.setChoice4(quizCreateRequest.getChoice4());
}
quiz.setQuizSet(quizSet);
quizRepository.save(quiz); quizRepository.save(quiz);
} }
@ -46,4 +52,5 @@ public class QuizServiceImpl implements QuizService {
quizRepository.deleteById(quizId); quizRepository.deleteById(quizId);
return true; return true;
} }
} }

View File

@ -13,4 +13,5 @@ public interface QuizSetService {
void deleteQuizSet(long quizSetId); void deleteQuizSet(long quizSetId);
QuizSet findQuizSet(long quizSetId);
} }

View File

@ -1,7 +1,7 @@
package com.edufocus.edufocus.quiz.service; package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.quiz.entity.*;
import com.edufocus.edufocus.quiz.entity.SetCreateRequest; 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;
@ -17,6 +17,7 @@ public class QuizSetServiceImpl implements QuizSetService {
private final QuizSetRepository quizSetRepository; private final QuizSetRepository quizSetRepository;
private final UserRepository userRepository; private final UserRepository userRepository;
private final QuizRepository quizRepository;
@Override @Override
@ -42,4 +43,9 @@ public class QuizSetServiceImpl implements QuizSetService {
public void deleteQuizSet(long quizSetId) { public void deleteQuizSet(long quizSetId) {
quizSetRepository.deleteById(quizSetId); quizSetRepository.deleteById(quizSetId);
} }
@Override
public QuizSet findQuizSet(long quizSetId) {
return quizSetRepository.findById(quizSetId).get();
}
} }