Merge pull request #77 from TeamBNBN/be/Quiz

feat: Quiz
This commit is contained in:
Jungmin 2024-07-22 13:10:11 +09:00 committed by GitHub
commit 10828ff11e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 67 additions and 44 deletions

View File

@ -1,17 +1,19 @@
package com.edufocus.edufocus.quiz.controller;
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.QuizSetService;
import com.edufocus.edufocus.user.model.service.UserService;
import com.edufocus.edufocus.user.util.JWTUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/quiz")
@ -25,10 +27,12 @@ public class QuizController {
private final UserService userService;
@PostMapping
public ResponseEntity<?> createQuizSet(@RequestBody QuizSetCreateRequest quizSetCreateRequest) {
private final JWTUtil jwtUtil;
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 image = quizSetCreateRequest.getImage();
@ -43,4 +47,11 @@ public class QuizController {
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;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.Getter;
import lombok.Setter;
import lombok.*;
@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Quiz {
@Id
@ -15,6 +19,7 @@ public class Quiz {
@ManyToOne
@JoinColumn(name = "quizset_id")
@JsonBackReference
private QuizSet quizSet;
@Column

View File

@ -3,7 +3,6 @@ package com.edufocus.edufocus.quiz.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Getter
@NoArgsConstructor
@ -14,10 +13,17 @@ public class QuizCreateRequest {
private String description;
private boolean isSingle;
private String answer;
private boolean isSingle;
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;
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;
@ -19,6 +21,7 @@ public class QuizSet {
@ManyToOne
@JoinColumn(name = "user_id")
@JsonBackReference
private User user;
@Column
@ -28,6 +31,7 @@ public class QuizSet {
private String image;
@OneToMany(mappedBy = "quizSet")
@JsonManagedReference
private List<Quiz> quizzes = new ArrayList<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 org.springframework.data.jpa.repository.JpaRepository;
public interface QuizRepository extends JpaRepository<Quiz, Long> {
}

View File

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

View File

@ -1,6 +1,5 @@
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.QuizCreateRequest;
import com.edufocus.edufocus.quiz.entity.QuizSet;
@ -10,6 +9,8 @@ import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
@Transactional
@RequiredArgsConstructor
@ -23,15 +24,20 @@ public class QuizServiceImpl implements QuizService {
public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) {
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());
quiz.setDescription(quizCreateRequest.getDescription());
quiz.setAnswer(quizCreateRequest.getAnswer());
quiz.setImage(quizCreateRequest.getImage());
quiz.setQuizSet(quizSet);
if (!quiz.isSingle()) {
quiz.setChoice1(quizCreateRequest.getChoice1());
quiz.setChoice2(quizCreateRequest.getChoice2());
quiz.setChoice3(quizCreateRequest.getChoice3());
quiz.setChoice4(quizCreateRequest.getChoice4());
}
quizRepository.save(quiz);
}
@ -46,4 +52,5 @@ public class QuizServiceImpl implements QuizService {
quizRepository.deleteById(quizId);
return true;
}
}

View File

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

View File

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