feat: Lecture 이미지 등록 추가

This commit is contained in:
kgc9007 2024-07-24 16:08:28 +09:00
parent b05733ec5e
commit f6e94fd4d3
4 changed files with 35 additions and 14 deletions

View File

@ -5,17 +5,19 @@ import com.edufocus.edufocus.lecture.entity.LectureCreateRequest;
import com.edufocus.edufocus.lecture.entity.LectureSearchResponse; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse; import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
import com.edufocus.edufocus.lecture.service.LectureService; 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 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.io.File;
import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.UUID;
@RestController @RestController
@RequestMapping("/lecture") @RequestMapping("/lecture")
@ -26,8 +28,9 @@ public class LectureController {
private final LectureService lectureService; private final LectureService lectureService;
private final JWTUtil jwtUtil; private final JWTUtil jwtUtil;
@PostMapping @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> createLecture(@RequestHeader("Authorization") String accessToken, @RequestBody LectureCreateRequest lectureCreateRequest) { 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)); Long userId = Long.parseLong(jwtUtil.getUserId(accessToken));
Lecture lecture = lectureService.findLectureByTitle(lectureCreateRequest.getTitle()); Lecture lecture = lectureService.findLectureByTitle(lectureCreateRequest.getTitle());
@ -36,7 +39,8 @@ public class LectureController {
return new ResponseEntity<>(msg, HttpStatus.CONFLICT); return new ResponseEntity<>(msg, HttpStatus.CONFLICT);
} }
lectureService.createLecture(userId, lectureCreateRequest); lectureService.createLecture(userId, lectureCreateRequest, image);
String msg = new String("Lecture registered successfully"); String msg = new String("Lecture registered successfully");
return new ResponseEntity<>(msg, HttpStatus.CREATED); return new ResponseEntity<>(msg, HttpStatus.CREATED);
} }

View File

@ -15,8 +15,6 @@ public class LectureCreateRequest {
private String description; private String description;
private String image;
private Date startDate; private Date startDate;
private Date endDate; private Date endDate;

View File

@ -5,6 +5,7 @@ import com.edufocus.edufocus.lecture.entity.LectureCreateRequest;
import com.edufocus.edufocus.lecture.entity.LectureSearchResponse; import com.edufocus.edufocus.lecture.entity.LectureSearchResponse;
import com.edufocus.edufocus.lecture.entity.LectureDetailResponse; import com.edufocus.edufocus.lecture.entity.LectureDetailResponse;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List; import java.util.List;
@ -12,7 +13,7 @@ import java.util.List;
@Service @Service
public interface LectureService { 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); boolean updateLecture(long userId, long lectureId, LectureCreateRequest lectureCreateRequest);

View File

@ -11,11 +11,17 @@ import com.edufocus.edufocus.user.model.repository.UserRepository;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import lombok.Builder; 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 java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.UUID;
@Builder @Builder
@Service @Service
@ -30,19 +36,34 @@ public class LectureServiceImpl implements LectureService {
private final RegistrationRepository registrationRepository; private final RegistrationRepository registrationRepository;
@Override @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(); User user = userRepository.findById(userId).get();
Lecture lecture = new Lecture().builder() Lecture lecture = new Lecture().builder()
.user(user) .user(user)
.title(lectureCreateRequest.getTitle()) .title(lectureCreateRequest.getTitle())
.description(lectureCreateRequest.getDescription()) .description(lectureCreateRequest.getDescription())
.image(lectureCreateRequest.getImage())
.startDate(lectureCreateRequest.getStartDate()) .startDate(lectureCreateRequest.getStartDate())
.endDate(lectureCreateRequest.getEndDate()) .endDate(lectureCreateRequest.getEndDate())
.plan(lectureCreateRequest.getPlan()) .plan(lectureCreateRequest.getPlan())
.build(); .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); lectureRepository.save(lecture);
} }
@ -60,9 +81,6 @@ public class LectureServiceImpl implements LectureService {
if (lectureCreateRequest.getDescription() != null) { if (lectureCreateRequest.getDescription() != null) {
lecture.setDescription(lectureCreateRequest.getDescription()); lecture.setDescription(lectureCreateRequest.getDescription());
} }
if (lectureCreateRequest.getImage() != null) {
lecture.setImage(lectureCreateRequest.getImage());
}
if (lectureCreateRequest.getStartDate() != null) { if (lectureCreateRequest.getStartDate() != null) {
lecture.setStartDate(lectureCreateRequest.getStartDate()); lecture.setStartDate(lectureCreateRequest.getStartDate());
} }