feat: Quiz

퀴즈셋 단위로 퀴즈 등록 추가
This commit is contained in:
kgc9007 2024-07-18 14:55:54 +09:00
parent 1b21ed4289
commit b9cd3de043
17 changed files with 259 additions and 19 deletions

View File

@ -1,7 +1,7 @@
package com.edufocus.edufocus.lecture.controller;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureRegist;
import com.edufocus.edufocus.lecture.entity.LectureCreateRequest;
import com.edufocus.edufocus.lecture.service.LectureService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -20,8 +20,8 @@ public class LectureController {
private final LectureService lectureService;
@PostMapping
public ResponseEntity<?> createLecture (@RequestBody long userId, @RequestBody LectureRegist lectureRegist) {
lectureService.createLecture(userId, lectureRegist);
public ResponseEntity<?> createLecture (@RequestBody long userId, @RequestBody LectureCreateRequest lectureCreateRequest) {
lectureService.createLecture(userId, lectureCreateRequest);
return new ResponseEntity<>(HttpStatus.CREATED);
}

View File

@ -10,7 +10,7 @@ import java.util.Date;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class LectureRegist {
public class LectureCreateRequest {
private String title;

View File

@ -1,7 +1,7 @@
package com.edufocus.edufocus.lecture.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureRegist;
import com.edufocus.edufocus.lecture.entity.LectureCreateRequest;
import org.springframework.stereotype.Service;
import java.util.List;
@ -10,7 +10,7 @@ import java.util.List;
@Service
public interface LectureService {
void createLecture(long userId, LectureRegist lectureRegist);
void createLecture(long userId, LectureCreateRequest lectureCreateRequest);
boolean deleteLecture(long userId, long LectureId);

View File

@ -1,7 +1,7 @@
package com.edufocus.edufocus.lecture.service;
import com.edufocus.edufocus.lecture.entity.Lecture;
import com.edufocus.edufocus.lecture.entity.LectureRegist;
import com.edufocus.edufocus.lecture.entity.LectureCreateRequest;
import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.repository.UserRepository;
@ -21,18 +21,18 @@ public class LectureServiceImpl implements LectureService {
private final UserRepository userRepository;
@Override
public void createLecture(long userId, LectureRegist lectureRegist) {
public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) {
User user = userRepository.findById(userId).get();
Lecture lecture = new Lecture();
lecture.setUser(user);
lecture.setTitle(lectureRegist.getTitle());
lecture.setDescription(lectureRegist.getDescription());
lecture.setStartDate(lectureRegist.getStartDate());
lecture.setEndDate(lectureRegist.getEndDate());
lecture.setPlan(lectureRegist.getPlan());
lecture.setTitle(lectureCreateRequest.getTitle());
lecture.setDescription(lectureCreateRequest.getDescription());
lecture.setStartDate(lectureCreateRequest.getStartDate());
lecture.setEndDate(lectureCreateRequest.getEndDate());
lecture.setPlan(lectureCreateRequest.getPlan());
lectureRepository.save(lecture);
}

View File

@ -1,4 +1,46 @@
package com.edufocus.edufocus.quiz.controller;
import com.edufocus.edufocus.quiz.entity.*;
import com.edufocus.edufocus.quiz.service.QuizService;
import com.edufocus.edufocus.quiz.service.QuizSetService;
import com.edufocus.edufocus.user.model.service.UserService;
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;
@RestController
@RequestMapping("/quiz")
@Slf4j
@RequiredArgsConstructor
public class QuizController {
private final QuizService quizService;
private final QuizSetService quizSetService;
private final UserService userService;
@PostMapping
public ResponseEntity<?> createQuizSet(@RequestBody QuizSetCreateRequest quizSetCreateRequest) {
Long userId = 1L;
String title = quizSetCreateRequest.getTitle();
String image = quizSetCreateRequest.getImage();
SetCreateRequest setCreateRequest = new SetCreateRequest(userId, title, image);
QuizSet quizSet = quizSetService.createQuizSet(setCreateRequest);
for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) {
quizService.createQuiz(quizSet.getId(), quizCreateRequest);
}
return new ResponseEntity<>(HttpStatus.CREATED);
}
}

View File

@ -0,0 +1,20 @@
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

@ -11,7 +11,7 @@ public class Quiz {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Long id;
@ManyToOne
@JoinColumn(name = "quizset_id")

View File

@ -0,0 +1,23 @@
package com.edufocus.edufocus.quiz.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class QuizCreateRequest {
private String title;
private String description;
private boolean isSingle;
private String answer;
private String image;
}

View File

@ -15,7 +15,7 @@ public class QuizSet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private Long id;
@ManyToOne
@JoinColumn(name = "user_id")

View File

@ -0,0 +1,22 @@
package com.edufocus.edufocus.quiz.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.List;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class QuizSetCreateRequest {
private Long UserId;
private String title;
private String image;
private List<QuizCreateRequest> quizzes;
}

View File

@ -0,0 +1,18 @@
package com.edufocus.edufocus.quiz.entity;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class SetCreateRequest {
private Long UserId;
private String title;
private String image;
}

View File

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

View File

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

View File

@ -1,4 +1,14 @@
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;
@Service
public interface QuizService {
void createQuiz(long quizSetId, QuizCreateRequest
QuizCreateRequest);
boolean deleteQuiz(long quizId);
}

View File

@ -1,4 +1,49 @@
package com.edufocus.edufocus.quiz.service;
public class QuizServiceImpl {
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;
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;
@Service
@Transactional
@RequiredArgsConstructor
public class QuizServiceImpl implements QuizService {
private final QuizRepository quizRepository;
private final QuizSetRepository quizSetRepository;
@Override
public void createQuiz(long quizSetId, QuizCreateRequest quizCreateRequest) {
QuizSet quizSet = quizSetRepository.findById(quizSetId).get();
Quiz quiz = new Quiz();
quiz.setTitle(quizCreateRequest.getTitle());
quiz.setDescription(quizCreateRequest.getDescription());
quiz.setAnswer(quizCreateRequest.getAnswer());
quiz.setImage(quizCreateRequest.getImage());
quiz.setQuizSet(quizSet);
quizRepository.save(quiz);
}
@Override
public boolean deleteQuiz(long quizId) {
// 유저 아이디 정보 조회 검증 로직 추가 예정
// jwt -> 로그인 유저 정보 조회
// quizId -> 퀴즈 정보 조회 -> 퀴즈셋 정보 조회
// 퀴즈셋 생성자와 로그인 유저의 id값이 일치하는지 확인 -> 불일치시 삭제 실패
quizRepository.deleteById(quizId);
return true;
}
}

View File

@ -1,4 +1,16 @@
package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.quiz.entity.SetCreateRequest;
import org.springframework.stereotype.Service;
@Service
public interface QuizSetService {
QuizSet createQuizSet(SetCreateRequest setCreateRequest);
void updateQuizSet(QuizSet quizSet);
void deleteQuizSet(long quizSetId);
}

View File

@ -1,4 +1,45 @@
package com.edufocus.edufocus.quiz.service;
public class QuizSetServiceImpl {
import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.quiz.entity.SetCreateRequest;
import com.edufocus.edufocus.quiz.repository.QuizSetRepository;
import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@Transactional
@RequiredArgsConstructor
public class QuizSetServiceImpl implements QuizSetService {
private final QuizSetRepository quizSetRepository;
private final UserRepository userRepository;
@Override
public QuizSet createQuizSet(SetCreateRequest setCreateRequest) {
QuizSet quizSet = new QuizSet();
User user = userRepository.findById(setCreateRequest.getUserId()).get();
quizSet.setUser(user);
quizSet.setTitle(setCreateRequest.getTitle());
quizSet.setImage(setCreateRequest.getImage());
return quizSetRepository.save(quizSet);
}
@Override
public void updateQuizSet(QuizSet quizSet) {
}
@Override
public void deleteQuizSet(long quizSetId) {
quizSetRepository.deleteById(quizSetId);
}
}