Skip to content

Conversation

@bokyung95
Copy link
Collaborator

@bokyung95 bokyung95 commented Dec 4, 2022

  • s3 연동
  • 기본 이미지 지정
  • 반려동물 프로필 이미지 수정 API 작성
  • s3 업로드 테스트 코드 작성

Pet Entity 수정
Pet Controller, Service 생성
FailedUploadImageException : 이미지 업로드 실패시 예외
ImageNotFoundException : 이미지
파일이 존재하지 않을 시 예외
InvalidImageTypeException : 이미지 확장가 아닐 때
예외
PetNotFoundException : 반려동물 프로필이 존재하지 않을 때의 예외
@bokyung95 bokyung95 added the feature New feature or request label Dec 4, 2022
@bokyung95 bokyung95 self-assigned this Dec 4, 2022
@bokyung95 bokyung95 linked an issue Dec 4, 2022 that may be closed by this pull request
4 tasks
@bokyung95 bokyung95 changed the title Feat 5 Feat: 반려동물 프로필 사진 수정 Dec 4, 2022
Copy link
Collaborator

@naekang naekang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주말동안 고생 많으셨습니다~ 🙏


dir: test-dir
default:
image-url: http://127.0.0.1:8001/test-bucket/test-dir/4a95753d-1827-43c5-b9a0-2359186aedec.png
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트용 이미지 url임을 명시하기 위해 이름에 test를 붙이는 것도 좋을거같습니다!

String urlPath = imageUploadService.uploadPetImage(file);

// then
assertThat(urlPath.substring(0, urlPath.lastIndexOf("/") + 1)).isEqualTo("http://127.0.0.1:8001/test-bucket/test-dir/");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isEqualTo 메소드 내부를 path변수로 대체할 수 있지 않을까요?? 🤟

import static org.assertj.core.api.Assertions.assertThat;

@Import(AwsS3MockConfig.class)
@SpringBootTest
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SpringBootTest@ExtendWith(MockitoExtension.class) 이 두가지 방식이 있는데 Mock을 이용하는 방식을 택하는 것도 좋을거같습니다!
phantasmicmeans/develop-study#12

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재는 Mock으로 테스트하는 방법이 없어 추후에 더 찾아보고 수정하도록 하겠습니다:)


import org.springframework.web.multipart.MultipartFile;

public interface ImageUploadService {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pet 뿐만 아니라 Member에서도 사용할 수 있기 때문에 메소드 이름에 Pet을 빼는게 어떨까요?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PetService에서 ImageUploadService에 의존하여 image를 업로드하는 것을 따로 뺀 것은 좋은 것 같습니다. 하지만 둘 다 service 계층으로 정의해둘 경우 PetService에서 다른 service에 의존하는게 조금 어색해보입니다. service말고 그냥 component처럼 빈 등록을 하면 어떨까요?

@PatchMapping("/mypage/pets/{petId}/image")
public ResponseEntity<String> updatePetImage(@PathVariable("petId") Long id,
@RequestPart("file") MultipartFile multipartFile) {
if (multipartFile.isEmpty()) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

예외 처리를 Service 단에서 진행하는게 어떨까....ㅎ

private String dir;

@Override
public String uploadPetImage(MultipartFile multipartFile) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Transactional이 있어야 할 거 같습니다!

public class AwsS3MockConfig {

@Value("${cloud.aws.s3.bucket}")
public String bucket;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

혹시 public인 이유가 따로 있을까요?

Copy link
Collaborator Author

@bokyung95 bokyung95 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

리뷰사항 참고하여 수정하겠습니다:)

import static org.assertj.core.api.Assertions.assertThat;

@Import(AwsS3MockConfig.class)
@SpringBootTest
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재는 Mock으로 테스트하는 방법이 없어 추후에 더 찾아보고 수정하도록 하겠습니다:)

Copy link
Contributor

@younghoondoodoom younghoondoodoom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

주말에도 고생 많으셨습니다! 제 생각 코멘트 남겼으니 참고 부탁드립니다!


private final AmazonS3 amazonS3;

@Value("${cloud.aws.s3.bucket}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@value가 필드에 직접 들어가게 되어있고 Setter도 없으면 테스트하기 어려운 코드가 될 것 같습니다. 테스트 코드에서도 spring에 무조건 의존하게 될 것 같습니다. 혹시 생성자의 파라미터에 @value를 통해 받게 하도록 해서 테스트하기 쉽게 만드는 것이 어떨까요??

@Value("${cloud.aws.s3.bucket}")
private String bucket;

@Value("${dir}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위에 코멘트랑 똑같은 생각입니다!

@Override
public String createFileName(String filename) {
return UUID.randomUUID().toString().concat(getFileExtension(filename));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

내부적으로만 사용하는 메소드 같은데 인터페이스를 통해 public으로 열어놓은 이유가 있으실까요??

validateFileExtension(filename);

return filename.substring(filename.lastIndexOf("."));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위에 코멘트랑 똑같은 의견입니다!

fileValidate.add(".png");
fileValidate.add(".JPG");
fileValidate.add(".JPEG");
fileValidate.add(".PNG");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이미지 파일 확장자를 저장하는 리스트를 따로 상수로 빼던가 클래스를 분리 시키는게 좋을 것 같습니다! 확장자가 추가된다면 코드를 직접 수정해야하는 번거로움이 있을 것 같습니다!


import org.springframework.web.multipart.MultipartFile;

public interface ImageUploadService {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PetService에서 ImageUploadService에 의존하여 image를 업로드하는 것을 따로 뺀 것은 좋은 것 같습니다. 하지만 둘 다 service 계층으로 정의해둘 경우 PetService에서 다른 service에 의존하는게 조금 어색해보입니다. service말고 그냥 component처럼 빈 등록을 하면 어떨까요?

@Transactional
public class ImageUploadServiceImpl implements ImageUploadService {

private final AmazonS3 amazonS3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 클래스가 AmazonS3에 직접적으로 의존하고 있는 것 같습니다. 만약 이미지 업로드를 다른 방식으로 하게되면 해당 클래스를 직접 수정해야하는 상황이 발생할 것 같습니다. 중간에 인터페이스를 둔 클래스 분리를 통해 의존성을 숨기는게 어떨까요?

private final ImageUploadService imageUploadService;
private final PetRepository petRepository;

@Value("${default.image-url}")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위에 @value에 대한 의견과 같습니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

반려동물 프로필 사진 수정

5 participants