From f6e94fd4d310c1c12b9cbf11aeed40aebe7a9bcd Mon Sep 17 00:00:00 2001 From: kgc9007 Date: Wed, 24 Jul 2024 16:08:28 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Lecture=20=EC=9D=B4=EB=AF=B8=EC=A7=80?= =?UTF-8?q?=20=EB=93=B1=EB=A1=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../lecture/controller/LectureController.java | 16 +++++++---- .../lecture/entity/LectureCreateRequest.java | 2 -- .../lecture/service/LectureService.java | 3 +- .../lecture/service/LectureServiceImpl.java | 28 +++++++++++++++---- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java b/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java index 5ba43cd..b1895d5 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/controller/LectureController.java @@ -5,17 +5,19 @@ import com.edufocus.edufocus.lecture.entity.LectureCreateRequest; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse; import com.edufocus.edufocus.lecture.entity.LectureDetailResponse; import com.edufocus.edufocus.lecture.service.LectureService; -import com.edufocus.edufocus.user.model.entity.User; -import com.edufocus.edufocus.user.model.service.UserService; -import com.edufocus.edufocus.user.model.service.UserServiceImpl; import com.edufocus.edufocus.user.util.JWTUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; +import java.io.File; +import java.io.IOException; import java.util.List; +import java.util.UUID; @RestController @RequestMapping("/lecture") @@ -26,8 +28,9 @@ public class LectureController { private final LectureService lectureService; private final JWTUtil jwtUtil; - @PostMapping - public ResponseEntity createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) { + @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity createLecture(@RequestHeader("Authorization") String accessToken, @RequestPart LectureCreateRequest lectureCreateRequest + , @RequestPart(value = "image", required = false) MultipartFile image) throws Exception { Long userId = Long.parseLong(jwtUtil.getUserId(accessToken)); Lecture lecture = lectureService.findLectureByTitle(lectureCreateRequest.getTitle()); @@ -36,7 +39,8 @@ public class LectureController { return new ResponseEntity<>(msg, HttpStatus.CONFLICT); } - lectureService.createLecture(userId, lectureCreateRequest); + lectureService.createLecture(userId, lectureCreateRequest, image); + String msg = new String("Lecture registered successfully"); return new ResponseEntity<>(msg, HttpStatus.CREATED); } diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java index 84c2181..d76afbe 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/entity/LectureCreateRequest.java @@ -15,8 +15,6 @@ public class LectureCreateRequest { private String description; - private String image; - private Date startDate; private Date endDate; diff --git a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java index 88fc735..962ed5d 100644 --- a/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java +++ b/backend/src/main/java/com/edufocus/edufocus/lecture/service/LectureService.java @@ -5,6 +5,7 @@ import com.edufocus.edufocus.lecture.entity.LectureCreateRequest; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse; import com.edufocus.edufocus.lecture.entity.LectureDetailResponse; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; import java.util.List; @@ -12,7 +13,7 @@ import java.util.List; @Service public interface LectureService { - void createLecture(long userId, LectureCreateRequest lectureCreateRequest); + void createLecture(long userId, LectureCreateRequest lectureCreateRequest, MultipartFile image) throws Exception; boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest); 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 56b33cf..73267b9 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 @@ -11,11 +11,17 @@ 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.ArrayList; import java.util.List; import java.util.Optional; +import java.util.UUID; @Builder @Service @@ -30,19 +36,34 @@ public class LectureServiceImpl implements LectureService { private final RegistrationRepository registrationRepository; @Override - public void createLecture(long userId, LectureCreateRequest lectureCreateRequest) { + public void createLecture(long userId, LectureCreateRequest lectureCreateRequest, MultipartFile image) throws IOException { User user = userRepository.findById(userId).get(); Lecture lecture = new Lecture().builder() .user(user) .title(lectureCreateRequest.getTitle()) .description(lectureCreateRequest.getDescription()) - .image(lectureCreateRequest.getImage()) .startDate(lectureCreateRequest.getStartDate()) .endDate(lectureCreateRequest.getEndDate()) .plan(lectureCreateRequest.getPlan()) .build(); + 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(); + + lecture.setImage(savePath); + } + lectureRepository.save(lecture); } @@ -60,9 +81,6 @@ public class LectureServiceImpl implements LectureService { if (lectureCreateRequest.getDescription() != null) { lecture.setDescription(lectureCreateRequest.getDescription()); } - if (lectureCreateRequest.getImage() != null) { - lecture.setImage(lectureCreateRequest.getImage()); - } if (lectureCreateRequest.getStartDate() != null) { lecture.setStartDate(lectureCreateRequest.getStartDate()); }