feat: Quiz 수정 추가
This commit is contained in:
parent
65a26f74d6
commit
f29ca87ee7
@ -13,7 +13,9 @@ import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/quiz")
|
||||
@ -42,13 +44,57 @@ public class QuizController {
|
||||
return new ResponseEntity<>(HttpStatus.CREATED);
|
||||
}
|
||||
|
||||
@GetMapping("/{quizsetId}")
|
||||
@GetMapping("/student/{quizsetId}")
|
||||
public ResponseEntity<?> getQuizzes(@PathVariable Long quizsetId) {
|
||||
QuizSetResponse quizSet = quizSetService.findQuizSetResponse(quizsetId);
|
||||
|
||||
return new ResponseEntity<>(quizSet, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@GetMapping("/teacher/{quizsetId}")
|
||||
public ResponseEntity<?> getQuizzesIncludeAnswer(@RequestHeader("Authorization") String accessToken, @PathVariable Long quizsetId) {
|
||||
long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
|
||||
|
||||
QuizSet quizSet = quizSetService.findQuizSet(quizsetId);
|
||||
if (quizSet.getUser().getId() != userId) {
|
||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(quizSet, HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PutMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
|
||||
public ResponseEntity<?> updateQuizSet(@RequestHeader("Authorization") String accessToken, @RequestPart QuizSetUpdateRequest quizSetUpdateRequest
|
||||
, @RequestPart(value = "images", required = false) List<MultipartFile> images) throws IOException {
|
||||
long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
|
||||
|
||||
QuizSet quizset = quizSetService.findQuizSet(quizSetUpdateRequest.getId());
|
||||
if (userId != quizset.getUser().getId()) {
|
||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
quizSetService.updateQuizSet(quizSetUpdateRequest.getId(), quizSetUpdateRequest.getTitle());
|
||||
|
||||
Map<Long, Boolean> quizUpdatedCheckMap = new HashMap<>();
|
||||
for (Quiz quiz : quizset.getQuizzes()) {
|
||||
quizUpdatedCheckMap.put(quiz.getId(), false);
|
||||
}
|
||||
int imageIdx = 0;
|
||||
for (QuizUpdateRequest quizUpdateRequest : quizSetUpdateRequest.getQuizzes()) {
|
||||
quizService.updateQuiz(quizUpdateRequest, images.get(imageIdx++));
|
||||
|
||||
quizUpdatedCheckMap.put(quizUpdateRequest.getId(), true);
|
||||
}
|
||||
|
||||
for (Long quizId : quizUpdatedCheckMap.keySet()) {
|
||||
if (!quizUpdatedCheckMap.get(quizId)) {
|
||||
quizService.removeQuiz(quizId);
|
||||
}
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{quizsetId}")
|
||||
public ResponseEntity<?> deleteQuizSet(@RequestHeader("Authorization") String accessToken, @PathVariable long quizsetId) {
|
||||
long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
|
||||
|
@ -0,0 +1,20 @@
|
||||
package com.edufocus.edufocus.quiz.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class QuizSetUpdateRequest {
|
||||
|
||||
private long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private List<QuizUpdateRequest> quizzes;
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.edufocus.edufocus.quiz.entity;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Getter
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class QuizUpdateRequest {
|
||||
|
||||
private long id;
|
||||
|
||||
private String question;
|
||||
|
||||
private String answer;
|
||||
|
||||
private List<ChoiceCreateRequest> choices;
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.edufocus.edufocus.quiz.repository;
|
||||
|
||||
import com.edufocus.edufocus.quiz.entity.Choice;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface ChoiceRepository extends JpaRepository<Choice, Long> {
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
package com.edufocus.edufocus.quiz.service;
|
||||
|
||||
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.entity.QuizUpdateRequest;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
@ -13,4 +15,9 @@ public interface QuizService {
|
||||
|
||||
void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException;
|
||||
|
||||
void updateQuiz(QuizUpdateRequest quizUpdateRequest, MultipartFile quizImage) throws IOException;
|
||||
|
||||
Quiz findQuiz(long quizId);
|
||||
|
||||
void removeQuiz(long quizId);
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.edufocus.edufocus.quiz.service;
|
||||
|
||||
import com.edufocus.edufocus.global.properties.ImagePathProperties;
|
||||
import com.edufocus.edufocus.quiz.entity.*;
|
||||
import com.edufocus.edufocus.quiz.repository.ChoiceRepository;
|
||||
import com.edufocus.edufocus.quiz.repository.QuizRepository;
|
||||
import jakarta.transaction.Transactional;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
@ -12,6 +13,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.UUID;
|
||||
|
||||
|
||||
@ -24,6 +26,8 @@ public class QuizServiceImpl implements QuizService {
|
||||
|
||||
private final QuizRepository quizRepository;
|
||||
|
||||
private final ChoiceRepository choiceRepository;
|
||||
|
||||
@Override
|
||||
public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException {
|
||||
Quiz quiz = Quiz.builder()
|
||||
@ -66,4 +70,67 @@ public class QuizServiceImpl implements QuizService {
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void updateQuiz(QuizUpdateRequest quizUpdateRequest, MultipartFile quizImage) throws IOException {
|
||||
Quiz quiz = quizRepository.findById(quizUpdateRequest.getId()).orElseThrow(NoSuchElementException::new);
|
||||
|
||||
quiz.setQuestion(quizUpdateRequest.getQuestion());
|
||||
quiz.setAnswer(quizUpdateRequest.getAnswer());
|
||||
|
||||
quiz.getChoices().clear();
|
||||
quizRepository.save(quiz);
|
||||
|
||||
for (ChoiceCreateRequest choiceCreateRequest : quizUpdateRequest.getChoices()) {
|
||||
Choice choice = Choice.builder()
|
||||
.quiz(quiz)
|
||||
.num(choiceCreateRequest.getNum())
|
||||
.content(choiceCreateRequest.getContent())
|
||||
.build();
|
||||
|
||||
quiz.addChoice(choice);
|
||||
}
|
||||
|
||||
if (quizImage != null && !quizImage.isEmpty()) {
|
||||
if (quiz.getImage() != null) {
|
||||
File file = new File(quiz.getImage());
|
||||
file.delete();
|
||||
}
|
||||
|
||||
String uid = UUID.randomUUID().toString();
|
||||
|
||||
String imagePath = imagePathProperties.getPath();
|
||||
|
||||
File checkPathFile = new File(imagePath);
|
||||
if (!checkPathFile.exists()) {
|
||||
checkPathFile.mkdirs();
|
||||
}
|
||||
|
||||
File savingImage = new File(imagePath + "/" + uid + "_" + quizImage.getOriginalFilename());
|
||||
quizImage.transferTo(savingImage.toPath());
|
||||
String savePath = savingImage.toPath().toString();
|
||||
|
||||
quiz.setImage(savePath);
|
||||
}
|
||||
|
||||
quizRepository.save(quiz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Quiz findQuiz(long quizId) {
|
||||
return quizRepository.findById(quizId).orElseThrow(NoSuchElementException::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeQuiz(long quizId) {
|
||||
Quiz quiz = quizRepository.findById(quizId).orElseThrow(NoSuchElementException::new);
|
||||
|
||||
String image = quiz.getImage();
|
||||
if (image != null) {
|
||||
File file = new File(quiz.getImage());
|
||||
file.delete();
|
||||
quiz.setImage(null);
|
||||
}
|
||||
|
||||
quizRepository.delete(quiz);
|
||||
}
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ public interface QuizSetService {
|
||||
|
||||
QuizSet createQuizSet(long userId, String title);
|
||||
|
||||
void updateQuizSet(QuizSet quizSet);
|
||||
void updateQuizSet(long quizSetId, String title);
|
||||
|
||||
boolean deleteQuizSet(long userId, long quizSetId);
|
||||
|
||||
|
@ -34,8 +34,11 @@ public class QuizSetServiceImpl implements QuizSetService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateQuizSet(QuizSet quizSet) {
|
||||
public void updateQuizSet(long quizSetId, String title) {
|
||||
QuizSet quizSet = quizSetRepository.findById(quizSetId).orElseThrow(NoSuchElementException::new);
|
||||
|
||||
quizSet.setTitle(title);
|
||||
quizSetRepository.save(quizSet);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -17,7 +17,7 @@ jwt.salt=${SALT}
|
||||
|
||||
# Access Token ?? ?? (??? ??)
|
||||
#jwt.access-token.expiretime=3600000
|
||||
jwt.access-token.expiretime=300
|
||||
jwt.access-token.expiretime=360000
|
||||
|
||||
# Refresh Token ?? ?? (??? ??)
|
||||
jwt.refresh-token.expiretime=50400000
|
||||
|
Loading…
Reference in New Issue
Block a user