Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

경매 이미지 url 처리에 방법 변경 #385

Merged
merged 2 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.ddang.ddang.image.application;

import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.application.exception.ImageNotFoundException;
import com.ddang.ddang.image.domain.AuctionImage;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.infrastructure.persistence.JpaAuctionImageRepository;
import com.ddang.ddang.image.infrastructure.persistence.JpaImageRepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -28,19 +27,25 @@ public class ImageService {

public Resource readProfileImage(final Long id) throws MalformedURLException {
final ProfileImage profileImage = imageRepository.findById(id)
.orElseThrow(() -> new ImageNotFoundException(
"지정한 이미지를 찾을 수 없습니다."
));
.orElse(null);

if (profileImage == null) {
return null;
}

final String fullPath = findFullPath(profileImage.getImage().getStoreName());

return new UrlResource(FILE_PROTOCOL_PREFIX + fullPath);
}

public Resource readAuctionImage(final Long id) throws MalformedURLException {
final AuctionImage auctionImage = auctionImageRepository.findById(id)
.orElseThrow(() -> new ImageNotFoundException(
"지정한 이미지를 찾을 수 없습니다."
));
.orElse(null);

if (auctionImage == null) {
return null;
}

final String fullPath = findFullPath(auctionImage.getImage().getStoreName());

return new UrlResource(FILE_PROTOCOL_PREFIX + fullPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package com.ddang.ddang.image.application;


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

import com.ddang.ddang.image.application.exception.ImageNotFoundException;
import com.ddang.ddang.image.domain.AuctionImage;
import com.ddang.ddang.image.domain.ProfileImage;
import com.ddang.ddang.image.infrastructure.persistence.JpaAuctionImageRepository;
Expand All @@ -17,6 +13,10 @@
import org.springframework.core.io.Resource;
import org.springframework.transaction.annotation.Transactional;

import java.net.MalformedURLException;

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Transactional
@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
Expand Down Expand Up @@ -47,14 +47,15 @@ class ProfileImageServiceTest {
}

@Test
void 지정한_아이디에_해당하는_이미지가_없는_경우_예외가_발생한다() {
void 지정한_아이디에_해당하는_이미지가_없는_경우_null을_반환한다() throws MalformedURLException {
// given
final Long invalidImageId = -999L;

// when & then
assertThatThrownBy(() -> imageService.readProfileImage(invalidImageId))
.isInstanceOf(ImageNotFoundException.class)
.hasMessage("지정한 이미지를 찾을 수 없습니다.");
// when
final Resource actual = imageService.readProfileImage(invalidImageId);

// then
assertThat(actual).isNull();
}

@Test
Expand All @@ -72,13 +73,14 @@ class ProfileImageServiceTest {
}

@Test
void 지정한_아이디에_해당하는_경매_이미지가_없는_경우_예외가_발생한다() {
void 지정한_아이디에_해당하는_경매_이미지가_없는_경우_null을_반환한다() throws MalformedURLException {
// given
final Long invalidAuctionImageId = -999L;

// when & then
assertThatThrownBy(() -> imageService.readAuctionImage(invalidAuctionImageId))
.isInstanceOf(ImageNotFoundException.class)
.hasMessage("지정한 이미지를 찾을 수 없습니다.");
// when
final Resource actual = imageService.readAuctionImage(invalidAuctionImageId);

// then
assertThat(actual).isNull();
}
}