feat: quiz 사진 등록 추가, 퀴즈 타입 코드 변경

This commit is contained in:
kgc9007 2024-07-25 13:13:28 +09:00
parent 98643fea94
commit 60b875bb3e
19 changed files with 37 additions and 26 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

View File

@ -1,19 +1,20 @@
package com.edufocus.edufocus.quiz.controller; package com.edufocus.edufocus.quiz.controller;
import com.edufocus.edufocus.quiz.entity.*; 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.QuizService;
import com.edufocus.edufocus.quiz.service.QuizSetService; import com.edufocus.edufocus.quiz.service.QuizSetService;
import com.edufocus.edufocus.user.model.service.UserService;
import com.edufocus.edufocus.user.util.JWTUtil; import com.edufocus.edufocus.user.util.JWTUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList; import java.io.File;
import java.util.List; import java.io.IOException;
import java.util.UUID;
@RestController @RestController
@RequestMapping("/quiz") @RequestMapping("/quiz")
@ -25,21 +26,32 @@ public class QuizController {
private final QuizSetService quizSetService; private final QuizSetService quizSetService;
private final UserService userService;
private final JWTUtil jwtUtil; private final JWTUtil jwtUtil;
private final QuizRepository quizRepository;
@PostMapping @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestBody QuizSetCreateRequest quizSetCreateRequest) { public ResponseEntity<?> createQuizSet(@RequestHeader("Authorization") String accessToken, @RequestPart QuizSetCreateRequest quizSetCreateRequest
, @RequestPart(value = "image", required = false) MultipartFile image) throws IOException {
Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
String title = quizSetCreateRequest.getTitle(); String title = quizSetCreateRequest.getTitle();
String image = quizSetCreateRequest.getImage(); SetCreateRequest setCreateRequest = new SetCreateRequest(userId, title);
SetCreateRequest setCreateRequest = new SetCreateRequest(userId, title, image);
QuizSet quizSet = quizSetService.createQuizSet(setCreateRequest); QuizSet quizSet = quizSetService.createQuizSet(setCreateRequest);
if (image != null && !image.isEmpty()) {
String uid = UUID.randomUUID().toString();
String currentPath = "backend/src/main/resources/images/";
File checkPathFile = new File(currentPath);
if (!checkPathFile.exists()) {
checkPathFile.mkdirs();
}
File savingImage = new File(currentPath + uid + "_" + image.getOriginalFilename());
image.transferTo(savingImage.toPath());
String savePath = savingImage.toPath().toString();
quizSet.setImage(savePath);
}
for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) { for (QuizCreateRequest quizCreateRequest : quizSetCreateRequest.getQuizzes()) {
quizService.createQuiz(quizSet.getId(), quizCreateRequest); quizService.createQuiz(quizSet.getId(), quizCreateRequest);

View File

@ -1,5 +1,6 @@
package com.edufocus.edufocus.quiz.entity; package com.edufocus.edufocus.quiz.entity;
import com.edufocus.edufocus.user.model.entity.UserRole;
import com.fasterxml.jackson.annotation.JsonBackReference; import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*; import jakarta.persistence.*;
@ -31,8 +32,8 @@ public class Quiz {
@Column @Column
private String answer; private String answer;
@Column (name = "is_single") @Enumerated(EnumType.STRING)
private boolean isSingle; private QuizType quizType;
@Column @Column
private String image; private String image;

View File

@ -15,9 +15,7 @@ public class QuizCreateRequest {
private String answer; private String answer;
private boolean isSingle; private String quizType;
private String image;
private String choice1; private String choice1;

View File

@ -0,0 +1,5 @@
package com.edufocus.edufocus.quiz.entity;
public enum QuizType {
SINGLE, MULTIPLE
}

View File

@ -13,6 +13,4 @@ public class SetCreateRequest {
private String title; private String title;
private String image;
} }

View File

@ -3,14 +3,13 @@ package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.quiz.entity.Quiz; import com.edufocus.edufocus.quiz.entity.Quiz;
import com.edufocus.edufocus.quiz.entity.QuizCreateRequest; import com.edufocus.edufocus.quiz.entity.QuizCreateRequest;
import com.edufocus.edufocus.quiz.entity.QuizSet; import com.edufocus.edufocus.quiz.entity.QuizSet;
import com.edufocus.edufocus.quiz.entity.QuizType;
import com.edufocus.edufocus.quiz.repository.QuizRepository; import com.edufocus.edufocus.quiz.repository.QuizRepository;
import com.edufocus.edufocus.quiz.repository.QuizSetRepository; import com.edufocus.edufocus.quiz.repository.QuizSetRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
@Service @Service
@Transactional @Transactional
@RequiredArgsConstructor @RequiredArgsConstructor
@ -28,11 +27,11 @@ public class QuizServiceImpl implements QuizService {
.title(quizCreateRequest.getTitle()) .title(quizCreateRequest.getTitle())
.description(quizCreateRequest.getDescription()) .description(quizCreateRequest.getDescription())
.answer(quizCreateRequest.getAnswer()) .answer(quizCreateRequest.getAnswer())
.image(quizCreateRequest.getImage()) .quizType(QuizType.valueOf(quizCreateRequest.getQuizType()))
.quizSet(quizSet) .quizSet(quizSet)
.build(); .build();
if (!quiz.isSingle()) { if (!quiz.getQuizType().equals(QuizType.MULTIPLE)) {
quiz.setChoice1(quizCreateRequest.getChoice1()); quiz.setChoice1(quizCreateRequest.getChoice1());
quiz.setChoice2(quizCreateRequest.getChoice2()); quiz.setChoice2(quizCreateRequest.getChoice2());
quiz.setChoice3(quizCreateRequest.getChoice3()); quiz.setChoice3(quizCreateRequest.getChoice3());

View File

@ -17,7 +17,6 @@ public class QuizSetServiceImpl implements QuizSetService {
private final QuizSetRepository quizSetRepository; private final QuizSetRepository quizSetRepository;
private final UserRepository userRepository; private final UserRepository userRepository;
private final QuizRepository quizRepository;
@Override @Override
@ -29,7 +28,6 @@ public class QuizSetServiceImpl implements QuizSetService {
quizSet.setUser(user); quizSet.setUser(user);
quizSet.setTitle(setCreateRequest.getTitle()); quizSet.setTitle(setCreateRequest.getTitle());
quizSet.setImage(setCreateRequest.getImage());
return quizSetRepository.save(quizSet); return quizSetRepository.save(quizSet);
} }

View File

@ -45,7 +45,7 @@ public class WebConfiguration implements WebMvcConfigurer {
registry.addInterceptor(jwtInterceptor) registry.addInterceptor(jwtInterceptor)
.addPathPatterns("/**") // 모든 경로에 대해 인터셉터 적용 .addPathPatterns("/**") // 모든 경로에 대해 인터셉터 적용
.excludePathPatterns("/v3/api-docs/**","/swagger-resources/**","/webjars/**","/swagger-ui/**","/auth/**", "/board/**", "/user/**","/lecture/**","/qna/**"); // 인증 없이 접근 가능한 경로 설정 .excludePathPatterns("/v3/api-docs/**","/swagger-resources/**","/webjars/**","/swagger-ui/**","/auth/**", "/board/**", "/user/**","/lecture/**","/qna/**", "/quiz/**"); // 인증 없이 접근 가능한 경로 설정
///v3/api-docs/**, /swagger-resources/**, /webjars/** ///v3/api-docs/**, /swagger-resources/**, /webjars/**
} }