Skip to content

Commit

Permalink
Merge pull request #81 from miinyeong/main
Browse files Browse the repository at this point in the history
퀴즈 생성 가능 여부API 생성, 퀴즈 답변 API 생성
  • Loading branch information
miinyeong authored Nov 14, 2023
2 parents 246b572 + 9d6ec03 commit 6380e3b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.onestep.server.controller.quiz;

import com.onestep.server.entity.quiz.QuizAnswerRequestDTO;
import com.onestep.server.entity.quiz.QuizRequestDTO;
import com.onestep.server.service.quiz.QuizService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
Expand All @@ -18,11 +17,21 @@ public class QuizController {
//퀴즈 생성
@PostMapping(value = "/quiz/create")
public String writeQuiz(@RequestBody QuizRequestDTO quizRequestDTO){
quizService.writeQuiz(quizRequestDTO);
quizService.writeQuiz(quizRequestDTO);
return quizRequestDTO.getUser_id()+"님이 작성한 퀴즈가 등록되었습니다.";
}

//퀴즈 등록 가능상태 확인
@GetMapping(value = "/quiz/canQuiz/{family_id}")
public Boolean canQuiz(@PathVariable String family_id){
return quizService.canQuiz(family_id);
}

//퀴즈 답변
// @PostMapping(value = "/quiz/answer")
// public String answerQuiz(@RequestBody )
//0 오답, 1 정답
@PostMapping(value = "/quiz/answer")
public String answerQuiz(@RequestBody QuizAnswerRequestDTO quizAnswerRequestDTO){
quizService.answerQuiz(quizAnswerRequestDTO);
return quizAnswerRequestDTO.getUser_id()+"님이 작성한 퀴즈 답변이 등록되었습니다.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import com.onestep.server.entity.QuizAnswer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.Optional;

public interface IQuizAnswerRepository extends JpaRepository<QuizAnswer, Long> {
@Query("SELECT q FROM QuizAnswer q WHERE q.quiz.quiz_id =:quiz_id and q.user.user_id =:user_id")
Optional<QuizAnswer> findQuizByUser(@Param("quiz_id") Long quiz_id, @Param("user_id") String user_id);
}
11 changes: 11 additions & 0 deletions src/main/java/com/onestep/server/repository/IQuizRepository.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package com.onestep.server.repository;

import com.onestep.server.entity.Family;
import com.onestep.server.entity.Letter;
import com.onestep.server.entity.Quiz;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;
import java.util.Optional;

public interface IQuizRepository extends JpaRepository<Quiz, Long> {

// 날짜로 퀴즈 등록 여부 찾기
@Query("SELECT q FROM Quiz q WHERE q.user.family.fam_id =:family_id and q.write_date =:date")
Optional<Quiz> findQuizByWriteDate(@Param("family_id") String family_id,@Param("date") Date date);
}
43 changes: 43 additions & 0 deletions src/main/java/com/onestep/server/service/quiz/QuizService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.onestep.server.entity.Family;
import com.onestep.server.entity.Quiz;
import com.onestep.server.entity.QuizAnswer;
import com.onestep.server.entity.User;
import com.onestep.server.entity.quiz.QuizAnswerDTO;
import com.onestep.server.entity.quiz.QuizAnswerRequestDTO;
import com.onestep.server.entity.quiz.QuizDTO;
import com.onestep.server.entity.quiz.QuizRequestDTO;
import com.onestep.server.repository.IQuizAnswerRepository;
Expand All @@ -27,6 +29,7 @@ public class QuizService {
private final IQuizRepository iQuizRepository;
private final IQuizAnswerRepository iQuizAnswerRepository;
private final IUserRepository iUserRepository;

public Quiz writeQuiz(QuizRequestDTO quizRequestDTO){
Optional<User> optionalUser = iUserRepository.findById(quizRequestDTO.getUser_id());
User user = optionalUser.get();
Expand Down Expand Up @@ -65,4 +68,44 @@ public Quiz writeQuiz(QuizRequestDTO quizRequestDTO){

return addQuiz;
}

//퀴즈 생성 가능 여부
public Boolean canQuiz(String family_id){
Boolean canQuiz = true;
Date writeDate = new Date();
LocalTime now = LocalTime.now();

if(now.getHour()<6){
Date dDate = new Date();
writeDate = new Date(dDate.getTime()+(1000*60*60*24*-1));
}
Optional<Quiz> optionalQuiz = iQuizRepository.findQuizByWriteDate(family_id,writeDate);
// 같은 날에 퀴즈를 생성 했을 시
if(optionalQuiz.isPresent()){
canQuiz = false;
}

return canQuiz;
}

//퀴즈 답변
public QuizAnswer answerQuiz(QuizAnswerRequestDTO quizAnswerRequestDTO){
Optional<QuizAnswer> optionalQuizAnswer = iQuizAnswerRepository.findQuizByUser(quizAnswerRequestDTO.getQuiz_id(),quizAnswerRequestDTO.getUser_id());
Optional<Quiz> quiz = iQuizRepository.findById(quizAnswerRequestDTO.getQuiz_id());
Integer answer = quiz.get().getQuiz_ans();

QuizAnswer quizAnswer = new QuizAnswer();
quizAnswer.setQuizAnswer_id(optionalQuizAnswer.get().getQuizAnswer_id());
quizAnswer.setQuiz(optionalQuizAnswer.get().getQuiz());
quizAnswer.setUser(optionalQuizAnswer.get().getUser());
quizAnswer.setQuiz_ans(quizAnswerRequestDTO.getQuiz_ans());

if(quizAnswerRequestDTO.getQuiz_ans().equals(answer)){
quizAnswer.setQuiz_state(1);
}else{
quizAnswer.setQuiz_state(0);
}

return iQuizAnswerRepository.save(quizAnswer);
}
}

0 comments on commit 6380e3b

Please sign in to comment.