From 36c5a3d21f0b7b4af023fdadc2adad2f136a11f8 Mon Sep 17 00:00:00 2001 From: Dev Uni Date: Thu, 30 Nov 2023 15:05:11 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20ModelAttribute=EB=A1=9C=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/application/image/ImageService.java | 27 ++++++++++++++----- .../moabam/api/domain/image/ImageResizer.java | 4 +-- .../{ResizedImage.java => NewImage.java} | 6 ++--- .../api/dto/room/CertifyRoomRequest.java | 13 +++++++++ .../api/dto/room/CertifyRoomsRequest.java | 12 +++++++++ .../api/presentation/RoomController.java | 21 +++++++-------- .../application/image/ImageServiceTest.java | 4 +-- 7 files changed, 63 insertions(+), 24 deletions(-) rename src/main/java/com/moabam/api/domain/image/{ResizedImage.java => NewImage.java} (85%) create mode 100644 src/main/java/com/moabam/api/dto/room/CertifyRoomRequest.java create mode 100644 src/main/java/com/moabam/api/dto/room/CertifyRoomsRequest.java diff --git a/src/main/java/com/moabam/api/application/image/ImageService.java b/src/main/java/com/moabam/api/application/image/ImageService.java index c2b4208f..adabbf27 100644 --- a/src/main/java/com/moabam/api/application/image/ImageService.java +++ b/src/main/java/com/moabam/api/application/image/ImageService.java @@ -1,5 +1,6 @@ package com.moabam.api.application.image; +import java.io.IOException; import java.util.ArrayList; import java.util.List; @@ -10,6 +11,8 @@ import com.moabam.api.domain.image.ImageName; import com.moabam.api.domain.image.ImageResizer; import com.moabam.api.domain.image.ImageType; +import com.moabam.api.domain.image.NewImage; +import com.moabam.api.dto.room.CertifyRoomsRequest; import com.moabam.api.infrastructure.s3.S3Manager; import lombok.RequiredArgsConstructor; @@ -22,7 +25,7 @@ public class ImageService { private final S3Manager s3Manager; @Transactional - public List uploadImages(List multipartFiles, ImageType imageType) { + public List uploadImages(List multipartFiles, ImageType imageType) { List result = new ArrayList<>(); @@ -38,6 +41,23 @@ public List uploadImages(List multipartFiles, ImageType i return result; } + public List getNewImages(CertifyRoomsRequest request) { + return request.certifyRoomsRequest().stream() + .map(c -> { + try { + return NewImage.of(c.routineId().toString(), c.image().getContentType(), c.image().getBytes()); + } catch (IOException e) { + throw new RuntimeException(e); + } + }) + .toList(); + } + + @Transactional + public void deleteImage(String imageUrl) { + s3Manager.deleteImage(imageUrl); + } + private ImageResizer toImageResizer(MultipartFile multipartFile, ImageType imageType) { ImageName imageName = ImageName.of(multipartFile, imageType); @@ -46,9 +66,4 @@ private ImageResizer toImageResizer(MultipartFile multipartFile, ImageType image .fileName(imageName.getFileName()) .build(); } - - @Transactional - public void deleteImage(String imageUrl) { - s3Manager.deleteImage(imageUrl); - } } diff --git a/src/main/java/com/moabam/api/domain/image/ImageResizer.java b/src/main/java/com/moabam/api/domain/image/ImageResizer.java index 5a7d65ba..82b2f1d4 100644 --- a/src/main/java/com/moabam/api/domain/image/ImageResizer.java +++ b/src/main/java/com/moabam/api/domain/image/ImageResizer.java @@ -116,7 +116,7 @@ private BufferedImage getBufferedImage() { } } - private ResizedImage toMultipartFile(byte[] bytes) { - return ResizedImage.of(fileName, image.getContentType(), bytes); + private NewImage toMultipartFile(byte[] bytes) { + return NewImage.of(fileName, image.getContentType(), bytes); } } diff --git a/src/main/java/com/moabam/api/domain/image/ResizedImage.java b/src/main/java/com/moabam/api/domain/image/NewImage.java similarity index 85% rename from src/main/java/com/moabam/api/domain/image/ResizedImage.java rename to src/main/java/com/moabam/api/domain/image/NewImage.java index d7568527..6367d63c 100644 --- a/src/main/java/com/moabam/api/domain/image/ResizedImage.java +++ b/src/main/java/com/moabam/api/domain/image/NewImage.java @@ -12,15 +12,15 @@ import lombok.RequiredArgsConstructor; @RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public class ResizedImage implements MultipartFile { +public class NewImage implements MultipartFile { private final String name; private final String contentType; private final long size; private final byte[] bytes; - public static ResizedImage of(String name, String contentType, byte[] bytes) { - return new ResizedImage(name, contentType, bytes.length, bytes); + public static NewImage of(String name, String contentType, byte[] bytes) { + return new NewImage(name, contentType, bytes.length, bytes); } @Override diff --git a/src/main/java/com/moabam/api/dto/room/CertifyRoomRequest.java b/src/main/java/com/moabam/api/dto/room/CertifyRoomRequest.java new file mode 100644 index 00000000..7ef3496a --- /dev/null +++ b/src/main/java/com/moabam/api/dto/room/CertifyRoomRequest.java @@ -0,0 +1,13 @@ +package com.moabam.api.dto.room; + +import org.springframework.web.multipart.MultipartFile; + +import lombok.Builder; + +@Builder +public record CertifyRoomRequest( + Long routineId, + MultipartFile image +) { + +} diff --git a/src/main/java/com/moabam/api/dto/room/CertifyRoomsRequest.java b/src/main/java/com/moabam/api/dto/room/CertifyRoomsRequest.java new file mode 100644 index 00000000..6c6338e1 --- /dev/null +++ b/src/main/java/com/moabam/api/dto/room/CertifyRoomsRequest.java @@ -0,0 +1,12 @@ +package com.moabam.api.dto.room; + +import java.util.List; + +import lombok.Builder; + +@Builder +public record CertifyRoomsRequest( + List certifyRoomsRequest +) { + +} diff --git a/src/main/java/com/moabam/api/presentation/RoomController.java b/src/main/java/com/moabam/api/presentation/RoomController.java index 66bb4bed..ffc3c52b 100644 --- a/src/main/java/com/moabam/api/presentation/RoomController.java +++ b/src/main/java/com/moabam/api/presentation/RoomController.java @@ -1,7 +1,7 @@ package com.moabam.api.presentation; import java.time.LocalDate; -import java.util.Map; +import java.util.List; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.DeleteMapping; @@ -12,16 +12,18 @@ import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RequestPart; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.multipart.MultipartFile; import com.moabam.api.application.image.ImageService; import com.moabam.api.application.room.CertificationService; import com.moabam.api.application.room.RoomService; import com.moabam.api.application.room.SearchService; +import com.moabam.api.domain.image.ImageType; +import com.moabam.api.domain.image.NewImage; import com.moabam.api.domain.room.RoomType; +import com.moabam.api.dto.room.CertifiedMemberInfo; +import com.moabam.api.dto.room.CertifyRoomsRequest; import com.moabam.api.dto.room.CreateRoomRequest; import com.moabam.api.dto.room.EnterRoomRequest; import com.moabam.api.dto.room.GetAllRoomsResponse; @@ -110,14 +112,11 @@ public RoomDetailsResponse getRoomDetails(@Auth AuthMember authMember, @PathVari @PostMapping("/{roomId}/certification") @ResponseStatus(HttpStatus.CREATED) public void certifyRoom(@Auth AuthMember authMember, @PathVariable("roomId") Long roomId, - @RequestPart(name = "file") Map multipartFiles) { - - log.info("multipartFiles Size = {}", multipartFiles.size()); - log.info(multipartFiles.toString()); - - // List imageUrls = imageService.uploadImages(multipartFiles, ImageType.CERTIFICATION); - // CertifiedMemberInfo info = certificationService.getCertifiedMemberInfo(authMember.id(), roomId, imageUrls); - // certificationService.certifyRoom(info); + CertifyRoomsRequest request) { + List images = imageService.getNewImages(request); + List imageUrls = imageService.uploadImages(images, ImageType.CERTIFICATION); + CertifiedMemberInfo info = certificationService.getCertifiedMemberInfo(authMember.id(), roomId, imageUrls); + certificationService.certifyRoom(info); } @PutMapping("/{roomId}/members/{memberId}/mandate") diff --git a/src/test/java/com/moabam/api/application/image/ImageServiceTest.java b/src/test/java/com/moabam/api/application/image/ImageServiceTest.java index d8cdcc79..ea2e67ed 100644 --- a/src/test/java/com/moabam/api/application/image/ImageServiceTest.java +++ b/src/test/java/com/moabam/api/application/image/ImageServiceTest.java @@ -16,7 +16,7 @@ import org.springframework.web.multipart.MultipartFile; import com.moabam.api.domain.image.ImageType; -import com.moabam.api.domain.image.ResizedImage; +import com.moabam.api.domain.image.NewImage; import com.moabam.api.infrastructure.s3.S3Manager; import com.moabam.support.fixture.RoomFixture; @@ -38,7 +38,7 @@ void image_resize_upload_success() { MockMultipartFile image1 = RoomFixture.makeMultipartFile1(); List images = List.of(image1); - given(s3Manager.uploadImage(anyString(), any(ResizedImage.class))).willReturn(image1.getName()); + given(s3Manager.uploadImage(anyString(), any(NewImage.class))).willReturn(image1.getName()); // when List result = imageService.uploadImages(images, imageType);