Merge branch 'be/Lecture' into 'backend'

feat: Lecture, Quiz 이미지 저장 위치 설정 추가

See merge request s11-webmobile1-sub2/S11P12A701!4
This commit is contained in:
박정민 2024-08-01 10:59:29 +09:00
commit cdf8dbc30e
5 changed files with 50 additions and 27 deletions

View File

@ -1,13 +1,15 @@
package com.edufocus.edufocus.global.config; package com.edufocus.edufocus.global.config;
import com.edufocus.edufocus.global.properties.ImagePathProperties;
import com.edufocus.edufocus.global.properties.RabbitMQProperties; import com.edufocus.edufocus.global.properties.RabbitMQProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@Configuration @Configuration
@EnableConfigurationProperties({ @EnableConfigurationProperties({
RabbitMQProperties.class RabbitMQProperties.class,
ImagePathProperties.class
}) })
public class PropertiesConfig { public class PropertiesConfig {
} }

View File

@ -0,0 +1,12 @@
package com.edufocus.edufocus.global.properties;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Getter
@RequiredArgsConstructor
@ConfigurationProperties(prefix = "image")
public class ImagePathProperties {
private final String path;
}

View File

@ -1,5 +1,6 @@
package com.edufocus.edufocus.lecture.service; package com.edufocus.edufocus.lecture.service;
import com.edufocus.edufocus.global.properties.ImagePathProperties;
import com.edufocus.edufocus.lecture.entity.*; import com.edufocus.edufocus.lecture.entity.*;
import com.edufocus.edufocus.lecture.repository.LectureRepository; import com.edufocus.edufocus.lecture.repository.LectureRepository;
import com.edufocus.edufocus.registration.entity.Registration; import com.edufocus.edufocus.registration.entity.Registration;
@ -9,23 +10,22 @@ import com.edufocus.edufocus.user.model.entity.User;
import com.edufocus.edufocus.user.model.entity.UserRole; import com.edufocus.edufocus.user.model.entity.UserRole;
import com.edufocus.edufocus.user.model.repository.UserRepository; import com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.Builder;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.List;
@Builder
@Service @Service
@Transactional @Transactional
@RequiredArgsConstructor @RequiredArgsConstructor
public class LectureServiceImpl implements LectureService { public class LectureServiceImpl implements LectureService {
private final ImagePathProperties imagePathProperties;
private final LectureRepository lectureRepository; private final LectureRepository lectureRepository;
private final UserRepository userRepository; private final UserRepository userRepository;
@ -34,9 +34,9 @@ public class LectureServiceImpl implements LectureService {
@Override @Override
public void createLecture(long userId, LectureCreateRequest lectureCreateRequest, MultipartFile image) throws IOException { public void createLecture(long userId, LectureCreateRequest lectureCreateRequest, MultipartFile image) throws IOException {
User user = userRepository.findById(userId).get(); User user = userRepository.findById(userId).orElse(null);
Lecture lecture = new Lecture().builder() Lecture lecture = Lecture.builder()
.user(user) .user(user)
.title(lectureCreateRequest.getTitle()) .title(lectureCreateRequest.getTitle())
.description(lectureCreateRequest.getDescription()) .description(lectureCreateRequest.getDescription())
@ -49,13 +49,14 @@ public class LectureServiceImpl implements LectureService {
if (image != null && !image.isEmpty()) { if (image != null && !image.isEmpty()) {
String uid = UUID.randomUUID().toString(); String uid = UUID.randomUUID().toString();
String currentPath = "backend/src/main/resources/images/"; String imagePath = imagePathProperties.getPath();
File checkPathFile = new File(currentPath);
File checkPathFile = new File(imagePath);
if (!checkPathFile.exists()) { if (!checkPathFile.exists()) {
checkPathFile.mkdirs(); checkPathFile.mkdirs();
} }
File savingImage = new File(currentPath + uid + "_" + image.getOriginalFilename()); File savingImage = new File(imagePath + "/" + uid + "_" + image.getOriginalFilename());
image.transferTo(savingImage.toPath()); image.transferTo(savingImage.toPath());
String savePath = savingImage.toPath().toString(); String savePath = savingImage.toPath().toString();
@ -67,12 +68,14 @@ public class LectureServiceImpl implements LectureService {
@Override @Override
public boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest) { public boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest) {
Lecture lecture = lectureRepository.findById(lectureId).get(); Optional<Lecture> findLecture = lectureRepository.findById(lectureId);
if (lecture.getUser().getId() != userId) { if (findLecture.isEmpty() || findLecture.get().getUser().getId() != userId) {
return false; return false;
} }
Lecture lecture = findLecture.get();
if (lectureCreateRequest.getTitle() != null) { if (lectureCreateRequest.getTitle() != null) {
lecture.setTitle(lectureCreateRequest.getTitle()); lecture.setTitle(lectureCreateRequest.getTitle());
} }
@ -98,14 +101,14 @@ public class LectureServiceImpl implements LectureService {
@Override @Override
public boolean deleteLecture(long userId, long lectureId) { public boolean deleteLecture(long userId, long lectureId) {
Optional<Lecture> lecture = lectureRepository.findById(lectureId); Optional<Lecture> findLecture = lectureRepository.findById(lectureId);
if (lecture.isEmpty()) { if (findLecture.isEmpty()) {
return false; return false;
} }
lecture = Optional.of(lecture.get()); Lecture lecture = findLecture.get();
if (lecture.get().getUser().getId() != userId) { if (lecture.getUser().getId() != userId) {
return false; return false;
} }
@ -162,7 +165,7 @@ public class LectureServiceImpl implements LectureService {
} }
} }
LectureDetailResponse lectureDetailResponse = new LectureDetailResponse().builder() return LectureDetailResponse.builder()
.id(lecture.get().getId()) .id(lecture.get().getId())
.title(lecture.get().getTitle()) .title(lecture.get().getTitle())
.description(lecture.get().getDescription()) .description(lecture.get().getDescription())
@ -176,7 +179,6 @@ public class LectureServiceImpl implements LectureService {
.status(userStatus) .status(userStatus)
.build(); .build();
return lectureDetailResponse;
} }
public List<LectureSearchResponse> findMyLecture(long userId) { public List<LectureSearchResponse> findMyLecture(long userId) {
@ -187,7 +189,7 @@ public class LectureServiceImpl implements LectureService {
if (user.getRole() == UserRole.ADMIN) { if (user.getRole() == UserRole.ADMIN) {
List<Lecture> lectureList = lectureRepository.findLecturesByUserId(userId); List<Lecture> lectureList = lectureRepository.findLecturesByUserId(userId);
for (Lecture lecture : lectureList) { for (Lecture lecture : lectureList) {
LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder() LectureSearchResponse lectureSearchResponse = LectureSearchResponse.builder()
.id(lecture.getId()) .id(lecture.getId())
.title(lecture.getTitle()) .title(lecture.getTitle())
.image(lecture.getImage()).build(); .image(lecture.getImage()).build();
@ -200,7 +202,7 @@ public class LectureServiceImpl implements LectureService {
for (Registration registration : registrationList) { for (Registration registration : registrationList) {
Lecture lecture = registration.getLecture(); Lecture lecture = registration.getLecture();
LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder() LectureSearchResponse lectureSearchResponse = LectureSearchResponse.builder()
.id(lecture.getId()) .id(lecture.getId())
.title(lecture.getTitle()) .title(lecture.getTitle())
.image(lecture.getImage()).build(); .image(lecture.getImage()).build();
@ -241,7 +243,7 @@ public class LectureServiceImpl implements LectureService {
@Override @Override
public boolean getState(Long lectureId) { public boolean getState(Long lectureId) {
Lecture lecture= lectureRepository.findById(lectureId).orElse(null); Lecture lecture = lectureRepository.findById(lectureId).orElse(null);
return lecture.isOnline(); return lecture.isOnline();
} }

View File

@ -1,5 +1,6 @@
package com.edufocus.edufocus.quiz.service; package com.edufocus.edufocus.quiz.service;
import com.edufocus.edufocus.global.properties.ImagePathProperties;
import com.edufocus.edufocus.quiz.entity.*; import com.edufocus.edufocus.quiz.entity.*;
import com.edufocus.edufocus.quiz.repository.QuizRepository; import com.edufocus.edufocus.quiz.repository.QuizRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
@ -19,11 +20,13 @@ import java.util.UUID;
@RequiredArgsConstructor @RequiredArgsConstructor
public class QuizServiceImpl implements QuizService { public class QuizServiceImpl implements QuizService {
private final ImagePathProperties imagePathProperties;
private final QuizRepository quizRepository; private final QuizRepository quizRepository;
@Override @Override
public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException { public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException {
Quiz quiz = new Quiz().builder() Quiz quiz = Quiz.builder()
.quizSet(quizSet) .quizSet(quizSet)
.question(quizCreateRequest.getQuestion()) .question(quizCreateRequest.getQuestion())
.answer(quizCreateRequest.getAnswer()) .answer(quizCreateRequest.getAnswer())
@ -32,7 +35,7 @@ public class QuizServiceImpl implements QuizService {
List<Choice> choices = new ArrayList<>(); List<Choice> choices = new ArrayList<>();
for (ChoiceCreateRequest choiceCreateRequest : quizCreateRequest.getChoices()) { for (ChoiceCreateRequest choiceCreateRequest : quizCreateRequest.getChoices()) {
Choice choice = new Choice().builder() Choice choice = Choice.builder()
.quiz(quiz) .quiz(quiz)
.num(choiceCreateRequest.getNum()) .num(choiceCreateRequest.getNum())
.content(choiceCreateRequest.getContent()) .content(choiceCreateRequest.getContent())
@ -45,13 +48,14 @@ public class QuizServiceImpl implements QuizService {
if (quizImage != null && !quizImage.isEmpty()) { if (quizImage != null && !quizImage.isEmpty()) {
String uid = UUID.randomUUID().toString(); String uid = UUID.randomUUID().toString();
String currentPath = "backend/src/main/resources/images/quizzes/"; String imagePath = imagePathProperties.getPath();
File checkPathFile = new File(currentPath);
File checkPathFile = new File(imagePath);
if (!checkPathFile.exists()) { if (!checkPathFile.exists()) {
checkPathFile.mkdirs(); checkPathFile.mkdirs();
} }
File savingImage = new File(currentPath + uid + "_" + quizImage.getOriginalFilename()); File savingImage = new File(imagePath + "/" + uid + "_" + quizImage.getOriginalFilename());
quizImage.transferTo(savingImage.toPath()); quizImage.transferTo(savingImage.toPath());
String savePath = savingImage.toPath().toString(); String savePath = savingImage.toPath().toString();

View File

@ -47,4 +47,7 @@ spring.mail.properties.mail.smtp.starttls.enable=true
spring.rabbitmq.host=${RABBITMQ_HOST} spring.rabbitmq.host=${RABBITMQ_HOST}
spring.rabbitmq.port=${RABBITMQ_PORT} spring.rabbitmq.port=${RABBITMQ_PORT}
spring.rabbitmq.username=${RABBITMQ_USERNAME} spring.rabbitmq.username=${RABBITMQ_USERNAME}
spring.rabbitmq.password=${RABBITMQ_PASSWORD} spring.rabbitmq.password=${RABBITMQ_PASSWORD}
image.path=${IMAGE_PATH}