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.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);
}

View File

@ -15,8 +15,6 @@ public class LectureCreateRequest {
private String description;
private String image;
private Date startDate;
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.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);

View File

@ -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());
}