Skip to content

Commit

Permalink
Merge pull request #87 from miinyeong/main
Browse files Browse the repository at this point in the history
Feat: 퀴즈 결과 확인, 퀴즈 목록 확인 API 생성
  • Loading branch information
miinyeong authored Nov 14, 2023
2 parents c9dd1c1 + 173993c commit 6009471
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.onestep.server.controller.quiz;

import com.onestep.server.entity.Quiz;
import com.onestep.server.entity.QuizAnswer;
import com.onestep.server.entity.quiz.QuizAnswerCheckDTO;
import com.onestep.server.entity.quiz.QuizAnswerRequestDTO;
import com.onestep.server.entity.quiz.QuizListDTO;
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.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@Slf4j
Expand Down Expand Up @@ -34,4 +40,14 @@ public String answerQuiz(@RequestBody QuizAnswerRequestDTO quizAnswerRequestDTO)
quizService.answerQuiz(quizAnswerRequestDTO);
return quizAnswerRequestDTO.getUser_id()+"님이 작성한 퀴즈 답변이 등록되었습니다.";
}

//퀴즈 결과 확인
@GetMapping(value = "/quiz/checkAnswer/{quiz_id}")
public List<QuizAnswerCheckDTO> checkAnswer(@PathVariable Long quiz_id){
return quizService.checkAnswer(quiz_id);
}

//퀴즈 목록 확인
@GetMapping(value = "/quiz/quizList/{family_id}")
public List<QuizListDTO> quizList(@PathVariable String family_id){return quizService.quizList(family_id);}
}
2 changes: 2 additions & 0 deletions src/main/java/com/onestep/server/entity/QuizAnswer.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.onestep.server.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
Expand All @@ -22,6 +23,7 @@ public class QuizAnswer {
private Long quizAnswer_id;

@ManyToOne
@JsonIgnore
@JoinColumn(name="quiz_id")
private Quiz quiz;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.onestep.server.entity.quiz;


import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Setter
@Getter
@NoArgsConstructor
public class QuizAnswerCheckDTO {

private Long quizAnswer_id;
private String user_id;
private Integer quiz_ans;
private Integer quiz_state;
}
27 changes: 27 additions & 0 deletions src/main/java/com/onestep/server/entity/quiz/QuizListDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.onestep.server.entity.quiz;

import com.onestep.server.entity.QuizAnswer;
import com.onestep.server.entity.User;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

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


@Setter
@Getter
@NoArgsConstructor
public class QuizListDTO {
private Long quiz_id;
private String writer_id;
private String quiz_txt;
private String answer1;
private String answer2;
private String answer3;
private String answer4;
private Integer quiz_ans;
private Date write_date;
private List<QuizAnswerCheckDTO> quizAnswers;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.onestep.server.entity.Family;
import com.onestep.server.entity.Letter;
import com.onestep.server.entity.Quiz;
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;
Expand All @@ -16,4 +17,7 @@ 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);

@Query("select q from Quiz q where q.user.family.fam_id =:family_id")
List<Quiz> findQuizByFamily(@Param("family_id") String family_id);
}
61 changes: 57 additions & 4 deletions src/main/java/com/onestep/server/service/quiz/QuizService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
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.entity.quiz.*;
import com.onestep.server.repository.IQuizAnswerRepository;
import com.onestep.server.repository.IQuizRepository;
import com.onestep.server.repository.IUserRepository;
Expand All @@ -17,6 +14,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -108,4 +106,59 @@ public QuizAnswer answerQuiz(QuizAnswerRequestDTO quizAnswerRequestDTO){

return iQuizAnswerRepository.save(quizAnswer);
}

//퀴즈 확인
public List<QuizAnswerCheckDTO> checkAnswer(Long quiz_id){
Optional<Quiz> optionalQuiz = iQuizRepository.findById(quiz_id);
List<QuizAnswer> quizAnswers = optionalQuiz.get().getQuizAnswers();
List<QuizAnswerCheckDTO> quizAnswerCheck = new ArrayList<>();
for(QuizAnswer q : quizAnswers){
QuizAnswerCheckDTO quizAnswerCheckDTO = new QuizAnswerCheckDTO();

User answer = q.getUser();
quizAnswerCheckDTO.setQuizAnswer_id(q.getQuizAnswer_id());
quizAnswerCheckDTO.setUser_id(answer.getUser_id());
quizAnswerCheckDTO.setQuiz_ans(q.getQuiz_ans());
quizAnswerCheckDTO.setQuiz_state(q.getQuiz_state());

quizAnswerCheck.add(quizAnswerCheckDTO);

}
return quizAnswerCheck;
}

// 퀴즈 목록 확인
public List<QuizListDTO> quizList(String family_id){
List<Quiz> quizList = iQuizRepository.findQuizByFamily(family_id);
List<QuizListDTO> quizLists = new ArrayList<>();

for(Quiz q : quizList){
QuizListDTO quizListDTO = new QuizListDTO();
quizListDTO.setQuiz_id(q.getQuiz_id());
quizListDTO.setWriter_id(q.getUser().getUser_id());
quizListDTO.setQuiz_txt(q.getQuiz_txt());
quizListDTO.setAnswer1(q.getAnswer1());
quizListDTO.setAnswer2(q.getAnswer2());
quizListDTO.setAnswer3(q.getAnswer3());
quizListDTO.setAnswer4(q.getAnswer4());
quizListDTO.setQuiz_ans(q.getQuiz_ans());
quizListDTO.setWrite_date(q.getWrite_date());

List<QuizAnswer> quizAnswers = q.getQuizAnswers();
List<QuizAnswerCheckDTO> quizAnswerCheck = new ArrayList<>();
for (QuizAnswer qa : quizAnswers) {
QuizAnswerCheckDTO quizAnswerCheckDTO = new QuizAnswerCheckDTO();
User answer = qa.getUser();
quizAnswerCheckDTO.setQuizAnswer_id(qa.getQuizAnswer_id());
quizAnswerCheckDTO.setUser_id(answer.getUser_id());
quizAnswerCheckDTO.setQuiz_ans(qa.getQuiz_ans());
quizAnswerCheckDTO.setQuiz_state(qa.getQuiz_state());

quizAnswerCheck.add(quizAnswerCheckDTO);
}
quizListDTO.setQuizAnswers(quizAnswerCheck);
quizLists.add(quizListDTO);
}
return quizLists;
}
}

0 comments on commit 6009471

Please sign in to comment.