Skip to content

Commit

Permalink
fix: ModelAttribute로 방식 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
Shin-Jae-Yoon committed Nov 30, 2023
1 parent 95d0256 commit 36c5a3d
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 24 deletions.
27 changes: 21 additions & 6 deletions src/main/java/com/moabam/api/application/image/ImageService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.moabam.api.application.image;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -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;
Expand All @@ -22,7 +25,7 @@ public class ImageService {
private final S3Manager s3Manager;

@Transactional
public List<String> uploadImages(List<MultipartFile> multipartFiles, ImageType imageType) {
public List<String> uploadImages(List<? extends MultipartFile> multipartFiles, ImageType imageType) {

List<String> result = new ArrayList<>();

Expand All @@ -38,6 +41,23 @@ public List<String> uploadImages(List<MultipartFile> multipartFiles, ImageType i
return result;
}

public List<NewImage> 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);

Expand All @@ -46,9 +66,4 @@ private ImageResizer toImageResizer(MultipartFile multipartFile, ImageType image
.fileName(imageName.getFileName())
.build();
}

@Transactional
public void deleteImage(String imageUrl) {
s3Manager.deleteImage(imageUrl);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/moabam/api/domain/image/ImageResizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/com/moabam/api/dto/room/CertifyRoomRequest.java
Original file line number Diff line number Diff line change
@@ -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
) {

}
12 changes: 12 additions & 0 deletions src/main/java/com/moabam/api/dto/room/CertifyRoomsRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.moabam.api.dto.room;

import java.util.List;

import lombok.Builder;

@Builder
public record CertifyRoomsRequest(
List<CertifyRoomRequest> certifyRoomsRequest
) {

}
21 changes: 10 additions & 11 deletions src/main/java/com/moabam/api/presentation/RoomController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<String, MultipartFile> multipartFiles) {

log.info("multipartFiles Size = {}", multipartFiles.size());
log.info(multipartFiles.toString());

// List<String> imageUrls = imageService.uploadImages(multipartFiles, ImageType.CERTIFICATION);
// CertifiedMemberInfo info = certificationService.getCertifiedMemberInfo(authMember.id(), roomId, imageUrls);
// certificationService.certifyRoom(info);
CertifyRoomsRequest request) {
List<NewImage> images = imageService.getNewImages(request);
List<String> imageUrls = imageService.uploadImages(images, ImageType.CERTIFICATION);
CertifiedMemberInfo info = certificationService.getCertifiedMemberInfo(authMember.id(), roomId, imageUrls);
certificationService.certifyRoom(info);
}

@PutMapping("/{roomId}/members/{memberId}/mandate")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,7 +38,7 @@ void image_resize_upload_success() {
MockMultipartFile image1 = RoomFixture.makeMultipartFile1();
List<MultipartFile> 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<String> result = imageService.uploadImages(images, imageType);
Expand Down

0 comments on commit 36c5a3d

Please sign in to comment.