Skip to content

Commit

Permalink
Merge pull request #214 from fourix4/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
TaeHoon0 authored Jul 28, 2024
2 parents 4a351cd + 7094e82 commit f8ec5cb
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@ public class ManagerController {
private final ManagerService managerService;

@PostMapping("/manager")
public Response saveStudyCafe(@RequestParam String cafeName, @RequestParam AddressRequestDto address,
@RequestParam String openingHours, @RequestParam String closedHours,
@RequestParam String closedDay, @RequestParam String cafePhone,
@RequestParam Integer seats, @RequestParam RoomInfoRequestDto roomInfo,
@RequestParam List<UsageFeeRequestDto> usageFee, @RequestParam MultipartFile titleImage,
@RequestParam List<MultipartFile> multipleImages, @RequestParam MultipartFile seatChartImage) {
ManagerRequestDto managerRequestDto = new ManagerRequestDto(
cafeName, address, openingHours, closedHours, closedDay, cafePhone, seats, roomInfo, usageFee, titleImage, multipleImages, seatChartImage);
public Response saveStudyCafe(@RequestPart(value = "data") ManagerRequestDto managerRequestDto,
@RequestPart(value = "title_image", required = false) MultipartFile titleImage,
@RequestPart(value = "multiple_images", required = false) List<MultipartFile> multipleImages) {

managerService.saveStudyCafe(managerRequestDto);
managerService.saveStudyCafe(managerRequestDto, titleImage, multipleImages);
System.out.println(managerRequestDto.getClosedDay());
System.out.println(managerRequestDto.getCafeName());
System.out.println(managerRequestDto.getSeats());
Expand All @@ -41,19 +36,15 @@ public Response saveStudyCafe(@RequestParam String cafeName, @RequestParam Addre
}

@PatchMapping("/manager")
public Response updateStudyCafe(@RequestPart(value = "data") ManagerRequestDto requestDto,
public Response updateStudyCafe(@RequestPart(value = "data") ManagerRequestDto managerRequestDto,
@RequestPart(value = "title_image", required = false) MultipartFile titleImage,
@RequestPart(value = "multiple_images", required = false) List<MultipartFile> multipleImages,
@RequestPart(value = "seat_chart_image", required = false) MultipartFile seatChartImage) {
ManagerRequestDto managerRequestDto = new ManagerRequestDto(
requestDto, titleImage, multipleImages, seatChartImage);

@RequestPart(value = "multiple_images", required = false) List<MultipartFile> multipleImages) {
System.out.println(managerRequestDto.getCafePhone());
System.out.println(managerRequestDto.getCafeName());
System.out.println(managerRequestDto.getAddress().getCity());
System.out.println(managerRequestDto.getRoomInfo().getCancelAvailableTime());

managerService.updateStudyCafe(managerRequestDto);
managerService.updateStudyCafe(managerRequestDto, titleImage, multipleImages);
return Response.success();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ public class ManagerRequestDto {
private Integer seats;
private RoomInfoRequestDto roomInfo;
private List<UsageFeeRequestDto> usageFee;
private MultipartFile titleImage;
private List<MultipartFile> multipleImages;
private MultipartFile seatChartImage;

public ManagerRequestDto(ManagerRequestDto requestDto, MultipartFile titleImage, List<MultipartFile> multipleImages,
MultipartFile seatChartImage) {
Expand All @@ -38,8 +35,5 @@ public ManagerRequestDto(ManagerRequestDto requestDto, MultipartFile titleImage,
this.seats = requestDto.getSeats();
this.roomInfo = requestDto.getRoomInfo();
this.usageFee = requestDto.getUsageFee();
this.titleImage = titleImage;
this.multipleImages = multipleImages;
this.seatChartImage = seatChartImage;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,38 +25,31 @@ public class CafeImageService {
private final CafeImageRepository cafeImageRepository;
private final S3Service s3Service;

public void saveCafeImages(MultipartFile thumbnail, MultipartFile seatChart, List<MultipartFile> multipleImages, StudyCafe studyCafe) {
public void saveCafeImages(MultipartFile thumbnail, List<MultipartFile> multipleImages, StudyCafe studyCafe) {
String thumbnailUrl = s3Service.upload(thumbnail);
CafeImage cafeThumbnailImage = new CafeImage(ImageType.thumbnail, thumbnailUrl, studyCafe);
cafeImageRepository.save(cafeThumbnailImage);

String seatChartUrl = s3Service.upload(seatChart);
CafeImage cafeSeatChartImage = new CafeImage(ImageType.seatingChart, seatChartUrl, studyCafe);
cafeImageRepository.save(cafeSeatChartImage);

for(MultipartFile imageFile : multipleImages) {
String imageFileUrl = s3Service.upload(imageFile);
CafeImage cafeImage = new CafeImage(ImageType.cafeImage, imageFileUrl, studyCafe);
cafeImageRepository.save(cafeImage);
}
}

public void deleteCafeImages(MultipartFile thumbnail, MultipartFile seatChart, List<MultipartFile> multipleImages, StudyCafe studyCafe) {
public void deleteCafeImages(MultipartFile thumbnail, List<MultipartFile> multipleImages, StudyCafe studyCafe) {
List<CafeImage> images = cafeImageRepository.findAllByStudyCafe_CafeId(studyCafe.getCafeId());
String thumbnailUrl = "";
String seatChartUrl = "";
List<String> multipleImageUrls = new ArrayList<>();

for(CafeImage image : images) {
switch (image.getImageType()) {
case thumbnail -> thumbnailUrl = image.getCafeImage();
case seatingChart -> seatChartUrl = image.getCafeImage();
case cafeImage -> multipleImageUrls.add(image.getCafeImage());
}
}

if(thumbnail != null) s3Service.deleteImageFromS3(thumbnailUrl);
if(seatChart != null) s3Service.deleteImageFromS3(seatChartUrl);
if(multipleImages != null) {
for(String url : multipleImageUrls) s3Service.deleteImageFromS3(url);
}
Expand Down
16 changes: 5 additions & 11 deletions src/main/java/com/example/CatchStudy/service/ManagerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class ManagerService {
private final CafeImageRepository cafeImageRepository;

@Transactional
public void saveStudyCafe(ManagerRequestDto managerRequestDto) {
public void saveStudyCafe(ManagerRequestDto managerRequestDto, MultipartFile thumbnail, List<MultipartFile> multipleImages) {

long userId = usersService.getCurrentUserId();
Users user = usersRepository.findByUserId(userId).orElseThrow(() -> new CatchStudyException(ErrorCode.USER_NOT_FOUND));
Expand Down Expand Up @@ -65,14 +65,11 @@ public void saveStudyCafe(ManagerRequestDto managerRequestDto) {
}

// 이미지 저장
MultipartFile thumbnail = managerRequestDto.getTitleImage(); // 썸네일
MultipartFile seatChart = managerRequestDto.getSeatChartImage(); // 좌석배치도
List<MultipartFile> multipleImages = managerRequestDto.getMultipleImages(); // 카페 이미지
cafeImageService.saveCafeImages(thumbnail, seatChart, multipleImages, savedStudyCafe);
cafeImageService.saveCafeImages(thumbnail, multipleImages, savedStudyCafe);
}

@Transactional
public void updateStudyCafe(ManagerRequestDto managerRequestDto) {
public void updateStudyCafe(ManagerRequestDto managerRequestDto, MultipartFile thumbnail, List<MultipartFile> multipleImages) {
long userId = usersService.getCurrentUserId();
LocalTime openingHours = LocalTime.parse(managerRequestDto.getOpeningHours(), DateTimeFormatter.ofPattern("HH:mm"));
LocalTime closedHours = LocalTime.parse(managerRequestDto.getOpeningHours(), DateTimeFormatter.ofPattern("HH:mm"));
Expand All @@ -97,11 +94,8 @@ public void updateStudyCafe(ManagerRequestDto managerRequestDto) {
seatService.saveSeat(Integer.toString(i), studyCafe);
}

MultipartFile thumbnail = managerRequestDto.getTitleImage();
MultipartFile seatChart = managerRequestDto.getSeatChartImage();
List<MultipartFile> multipleImages = managerRequestDto.getMultipleImages();
cafeImageService.deleteCafeImages(thumbnail, seatChart, multipleImages, studyCafe);
cafeImageService.saveCafeImages(thumbnail, seatChart, multipleImages, studyCafe);
cafeImageService.deleteCafeImages(thumbnail, multipleImages, studyCafe);
cafeImageService.saveCafeImages(thumbnail, multipleImages, studyCafe);
}

public ManagerResponseDto getStudyCafe() {
Expand Down

0 comments on commit f8ec5cb

Please sign in to comment.