Skip to content

Commit

Permalink
feat : 퀴즈 상세 조회 + 퀴즈 삭제 api
Browse files Browse the repository at this point in the history
  • Loading branch information
Doncham committed May 25, 2024
1 parent 77e9d35 commit fc36595
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ public class QuizCreateDTO {
private String quizType;
private String problem;
private Integer answer;
private List<String> solutions;
private List<String> problems;
private String quizTitle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.Backend.shareNote.domain.Oraganization.DTOs.quizdto;

import lombok.Data;

@Data
public class QuizDeleteDTO {
private String organizationId;
private String noteId;
private String userId;
private String quizId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.Backend.shareNote.domain.Oraganization.DTOs.quizdto;

import lombok.Data;

@Data
public class QuizDetailReqDTO {
private String organizationId;
private String noteId;
private String userId;
private String quizId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.Backend.shareNote.domain.Oraganization.DTOs.quizdto;

import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@Builder
public class QuizDetailResDTO {
private String quizTitle;
private String quizType;
private String noteName; // 노트 이름
private List<String> problems;
private Integer correct;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.Backend.shareNote.domain.Oraganization.controller;

import com.Backend.shareNote.domain.Oraganization.DTOs.quizdto.QuizCreateDTO;
import com.Backend.shareNote.domain.Oraganization.DTOs.quizdto.QuizSearchDTO;
import com.Backend.shareNote.domain.Oraganization.DTOs.quizdto.QuizSolveDTO;
import com.Backend.shareNote.domain.Oraganization.DTOs.quizdto.*;
import com.Backend.shareNote.domain.Oraganization.service.QuizService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -29,4 +27,14 @@ public ResponseEntity<Object> createQuizSolutions(@RequestBody QuizSolveDTO quiz
public ResponseEntity<?> getQuiz(@PathVariable String organizationId, @PathVariable String noteId, @PathVariable String userId){
return quizService.getQuiz(organizationId, noteId, userId);
}

@PostMapping("/quiz/detail")
public ResponseEntity<?> getQuizDetail(@RequestBody QuizDetailReqDTO quizDetailReqDTO){
return quizService.getQuizDetail(quizDetailReqDTO);
}

@DeleteMapping("/quiz")
public ResponseEntity<?> deleteQuiz(@RequestBody QuizDeleteDTO quizDeleteDTO){
return quizService.deleteQuiz(quizDeleteDTO);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public static class Quiz {
// 답안
private int answer;
// 객관식 보기
private List<String> solutions;
private List<String> problems;
@CreatedDate
private LocalDateTime createdAt;

Expand All @@ -107,6 +107,7 @@ public static class Quiz {

// 닉네임
private String nickname;
private String quizTitle;

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.Backend.shareNote.domain.Oraganization.service;

import com.Backend.shareNote.domain.Oraganization.DTOs.quizdto.QuizCreateDTO;
import com.Backend.shareNote.domain.Oraganization.DTOs.quizdto.QuizSearchDTO;
import com.Backend.shareNote.domain.Oraganization.DTOs.quizdto.QuizSolveDTO;
import com.Backend.shareNote.domain.Oraganization.DTOs.quizdto.*;
import com.Backend.shareNote.domain.Oraganization.entity.Organization;
import com.Backend.shareNote.domain.Oraganization.repository.NoteRepository;
import com.Backend.shareNote.domain.Oraganization.repository.OrganizationRepository;
import com.Backend.shareNote.domain.Oraganization.repository.QuizRepository;
import com.Backend.shareNote.domain.User.entity.Users;
Expand All @@ -16,7 +13,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand Down Expand Up @@ -44,11 +40,12 @@ public ResponseEntity<Object> createQuiz(QuizCreateDTO quizCreateDTO) {
.quizType(quizCreateDTO.getQuizType())
.problem(quizCreateDTO.getProblem())
.answer(quizCreateDTO.getAnswer())
.solutions(quizCreateDTO.getSolutions())
.problems(quizCreateDTO.getProblems())
.correctUser(new ArrayList<String>())
.wrongUser(new ArrayList<String>())
.userId(quizCreateDTO.getUserId())
.nickname(user.getNickname())
.quizTitle(quizCreateDTO.getQuizTitle())
.build();


Expand Down Expand Up @@ -172,4 +169,91 @@ else if(quiz.getWrongUser().contains(userId)){
}

}

public ResponseEntity<?> getQuizDetail(QuizDetailReqDTO quizDetailReqDTO) {
try {
// organization 찾기
Organization organization = organizationRepository.findById(quizDetailReqDTO.getOrganizationId())
.orElseThrow(() -> new IllegalArgumentException("해당하는 organization이 없습니다."));

// note 찾기
Organization.Note note = organization.getNotes().stream()
.filter(n -> n.getId().equals(quizDetailReqDTO.getNoteId()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당하는 note가 없습니다."));

// quiz 찾기
Organization.Quiz quiz = note.getQuiz().stream()
.filter(q -> q.getId().equals(quizDetailReqDTO.getQuizId()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당하는 quiz가 없습니다."));

//
int corret;
if(quiz.getCorrectUser().contains(quizDetailReqDTO.getUserId())){
corret = 1;
}
else if(quiz.getWrongUser().contains(quizDetailReqDTO.getUserId())){
corret = 0;
}
else{
corret = -1;
}

QuizDetailResDTO dto = QuizDetailResDTO.builder()
.quizTitle(quiz.getProblem())
.quizType(quiz.getQuizType())
.noteName(note.getTitle())
.problems(quiz.getProblems())
.correct(corret)
.build();

return ResponseEntity.ok().body(dto);
}catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body("Invalid input: " + e.getMessage());
}
catch (Exception e) {
return ResponseEntity.badRequest().body("Unexpected error: " + e.getMessage());
}
}

public ResponseEntity<?> deleteQuiz(QuizDeleteDTO quizDeleteDTO) {
try {
// organization 찾기
Organization organization = organizationRepository.findById(quizDeleteDTO.getOrganizationId())
.orElseThrow(() -> new IllegalArgumentException("해당하는 organization이 없습니다."));

// note 찾기
Organization.Note note = organization.getNotes().stream()
.filter(n -> n.getId().equals(quizDeleteDTO.getNoteId()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당하는 note가 없습니다."));

// quiz 찾기
Organization.Quiz quiz = note.getQuiz().stream()
.filter(q -> q.getId().equals(quizDeleteDTO.getQuizId()))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("해당하는 quiz가 없습니다."));

// organization 멤버인지 확인하는 코드
if (!organization.getMembers().contains(quizDeleteDTO.getUserId())) {
throw new IllegalArgumentException("해당하는 organization의 멤버가 아닙니다.");
}

// 자기가 낸 문제인 경우만 삭제 가능
if (quiz.getUserId().equals(quizDeleteDTO.getUserId())) {
note.getQuiz().remove(quiz);
organizationRepository.save(organization);
} else{
throw new IllegalArgumentException("자신이 낸 문제만 삭제 가능합니다.");
}

return ResponseEntity.ok().body("퀴즈 삭제 성공");
}catch (IllegalArgumentException e) {
return ResponseEntity.badRequest().body("Invalid input: " + e.getMessage());
}
catch (Exception e) {
return ResponseEntity.badRequest().body("Unexpected error: " + e.getMessage());
}
}
}

0 comments on commit fc36595

Please sign in to comment.