diff --git a/backend/src/main/java/com/edufocus/edufocus/global/config/PropertiesConfig.java b/backend/src/main/java/com/edufocus/edufocus/global/config/PropertiesConfig.java index 4c3323e..eb01984 100644 --- a/backend/src/main/java/com/edufocus/edufocus/global/config/PropertiesConfig.java +++ b/backend/src/main/java/com/edufocus/edufocus/global/config/PropertiesConfig.java @@ -1,13 +1,15 @@ package com.edufocus.edufocus.global.config; +import com.edufocus.edufocus.global.properties.ImagePathProperties; import com.edufocus.edufocus.global.properties.RabbitMQProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration; @Configuration @EnableConfigurationProperties({ - RabbitMQProperties.class + RabbitMQProperties.class, + ImagePathProperties.class }) public class PropertiesConfig { } diff --git a/backend/src/main/java/com/edufocus/edufocus/global/properties/ImagePathProperties.java b/backend/src/main/java/com/edufocus/edufocus/global/properties/ImagePathProperties.java new file mode 100644 index 0000000..77d3672 --- /dev/null +++ b/backend/src/main/java/com/edufocus/edufocus/global/properties/ImagePathProperties.java @@ -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; +} diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java index d3d15e9..4c1bc84 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureServiceImpl.java @@ -1,5 +1,6 @@ package com.edufocus.edufocus.lecture.service; +import com.edufocus.edufocus.global.properties.ImagePathProperties; import com.edufocus.edufocus.lecture.entity.*; import com.edufocus.edufocus.lecture.repository.LectureRepository; 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.repository.UserRepository; import jakarta.transaction.Transactional; -import lombok.Builder; import lombok.RequiredArgsConstructor; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.util.*; +import java.util.List; -@Builder @Service @Transactional @RequiredArgsConstructor public class LectureServiceImpl implements LectureService { + private final ImagePathProperties imagePathProperties; + private final LectureRepository lectureRepository; private final UserRepository userRepository; @@ -34,9 +34,9 @@ public class LectureServiceImpl implements LectureService { @Override 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) .title(lectureCreateRequest.getTitle()) .description(lectureCreateRequest.getDescription()) @@ -49,13 +49,14 @@ public class LectureServiceImpl implements LectureService { if (image != null && !image.isEmpty()) { String uid = UUID.randomUUID().toString(); - String currentPath = "backend/src/main/resources/images/"; - File checkPathFile = new File(currentPath); + String imagePath = imagePathProperties.getPath(); + + File checkPathFile = new File(imagePath); if (!checkPathFile.exists()) { checkPathFile.mkdirs(); } - File savingImage = new File(currentPath + uid + "_" + image.getOriginalFilename()); + File savingImage = new File(imagePath + "/" + uid + "_" + image.getOriginalFilename()); image.transferTo(savingImage.toPath()); String savePath = savingImage.toPath().toString(); @@ -67,12 +68,14 @@ public class LectureServiceImpl implements LectureService { @Override public boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest) { - Lecture lecture = lectureRepository.findById(lectureId).get(); + Optional findLecture = lectureRepository.findById(lectureId); - if (lecture.getUser().getId() != userId) { + if (findLecture.isEmpty() || findLecture.get().getUser().getId() != userId) { return false; } + Lecture lecture = findLecture.get(); + if (lectureCreateRequest.getTitle() != null) { lecture.setTitle(lectureCreateRequest.getTitle()); } @@ -98,14 +101,14 @@ public class LectureServiceImpl implements LectureService { @Override public boolean deleteLecture(long userId, long lectureId) { - Optional lecture = lectureRepository.findById(lectureId); + Optional findLecture = lectureRepository.findById(lectureId); - if (lecture.isEmpty()) { + if (findLecture.isEmpty()) { return false; } - lecture = Optional.of(lecture.get()); + Lecture lecture = findLecture.get(); - if (lecture.get().getUser().getId() != userId) { + if (lecture.getUser().getId() != userId) { return false; } @@ -162,7 +165,7 @@ public class LectureServiceImpl implements LectureService { } } - LectureDetailResponse lectureDetailResponse = new LectureDetailResponse().builder() + return LectureDetailResponse.builder() .id(lecture.get().getId()) .title(lecture.get().getTitle()) .description(lecture.get().getDescription()) @@ -176,7 +179,6 @@ public class LectureServiceImpl implements LectureService { .status(userStatus) .build(); - return lectureDetailResponse; } public List findMyLecture(long userId) { @@ -187,7 +189,7 @@ public class LectureServiceImpl implements LectureService { if (user.getRole() == UserRole.ADMIN) { List lectureList = lectureRepository.findLecturesByUserId(userId); for (Lecture lecture : lectureList) { - LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder() + LectureSearchResponse lectureSearchResponse = LectureSearchResponse.builder() .id(lecture.getId()) .title(lecture.getTitle()) .image(lecture.getImage()).build(); @@ -200,7 +202,7 @@ public class LectureServiceImpl implements LectureService { for (Registration registration : registrationList) { Lecture lecture = registration.getLecture(); - LectureSearchResponse lectureSearchResponse = new LectureSearchResponse().builder() + LectureSearchResponse lectureSearchResponse = LectureSearchResponse.builder() .id(lecture.getId()) .title(lecture.getTitle()) .image(lecture.getImage()).build(); @@ -241,7 +243,7 @@ public class LectureServiceImpl implements LectureService { @Override public boolean getState(Long lectureId) { - Lecture lecture= lectureRepository.findById(lectureId).orElse(null); + Lecture lecture = lectureRepository.findById(lectureId).orElse(null); return lecture.isOnline(); } diff --git a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java index b25adfb..fbb42e5 100644 --- a/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java +++ b/backend/src/main/java/com/edufocus/edufocus/quiz/service/QuizServiceImpl.java @@ -1,5 +1,6 @@ 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.QuizRepository; import jakarta.transaction.Transactional; @@ -19,11 +20,13 @@ import java.util.UUID; @RequiredArgsConstructor public class QuizServiceImpl implements QuizService { + private final ImagePathProperties imagePathProperties; + private final QuizRepository quizRepository; @Override public void createQuiz(QuizSet quizSet, QuizCreateRequest quizCreateRequest, MultipartFile quizImage) throws IOException { - Quiz quiz = new Quiz().builder() + Quiz quiz = Quiz.builder() .quizSet(quizSet) .question(quizCreateRequest.getQuestion()) .answer(quizCreateRequest.getAnswer()) @@ -32,7 +35,7 @@ public class QuizServiceImpl implements QuizService { List choices = new ArrayList<>(); for (ChoiceCreateRequest choiceCreateRequest : quizCreateRequest.getChoices()) { - Choice choice = new Choice().builder() + Choice choice = Choice.builder() .quiz(quiz) .num(choiceCreateRequest.getNum()) .content(choiceCreateRequest.getContent()) @@ -45,13 +48,14 @@ public class QuizServiceImpl implements QuizService { if (quizImage != null && !quizImage.isEmpty()) { String uid = UUID.randomUUID().toString(); - String currentPath = "backend/src/main/resources/images/quizzes/"; - File checkPathFile = new File(currentPath); + String imagePath = imagePathProperties.getPath(); + + File checkPathFile = new File(imagePath); if (!checkPathFile.exists()) { checkPathFile.mkdirs(); } - File savingImage = new File(currentPath + uid + "_" + quizImage.getOriginalFilename()); + File savingImage = new File(imagePath + "/" + uid + "_" + quizImage.getOriginalFilename()); quizImage.transferTo(savingImage.toPath()); String savePath = savingImage.toPath().toString(); diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 62d7538..9cc111a 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -47,4 +47,7 @@ spring.mail.properties.mail.smtp.starttls.enable=true spring.rabbitmq.host=${RABBITMQ_HOST} spring.rabbitmq.port=${RABBITMQ_PORT} spring.rabbitmq.username=${RABBITMQ_USERNAME} -spring.rabbitmq.password=${RABBITMQ_PASSWORD} \ No newline at end of file +spring.rabbitmq.password=${RABBITMQ_PASSWORD} + + +image.path=${IMAGE_PATH} \ No newline at end of file