Skip to content

Commit

Permalink
BUG : supporImageUrl Null처리
Browse files Browse the repository at this point in the history
  • Loading branch information
JuseungL committed Aug 5, 2024
1 parent c371d22 commit 7ad6704
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ public interface SupportImageRepository extends JpaRepository<SupportImage, Long
@Query("SELECT si FROM SupportImage si WHERE si.supportPost.id = :supportPostId ORDER BY si.id ASC")
Optional<SupportImage> findFirstImageBySupportPostId(@Param("supportPostId") Long supportPostId);

default SupportImage findFirstImageBySupportPostIdOrThrow(@Param("supportPostId") Long supportPostId) {
return findFirstImageBySupportPostId(supportPostId)
.orElseThrow(() -> new NotFoundException(ErrorCode.POST_IMAGE_NOT_FOUND.getMessage()));
default SupportImage findFirstImageBySupportPostIdOrDefault(Long supportPostId) {
return findFirstImageBySupportPostId(supportPostId).orElse(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,12 @@ public PostTotalPagingResponse pagingSupportPost(int page, String q, List<String
.mapToInt(SupportRecord::getAmount)
.sum();
RecordProgressGetResponse supportRecordProgress = new RecordProgressGetResponse(totalPrice, supportedPrice);
String imageUrl = supportPost.getImages().isEmpty() ? null : fileService.getFileUrl(supportPost.getImages().get(0).getImageUrl());
return new PostPagingResponse(
supportPost.getId(),
supportPost.getTitle(),
supportPost.getExpirationDate(),
fileService.getFileUrl(supportPost.getImages().get(0).getImageUrl()),
imageUrl,
supportRecordProgress
);
})
Expand All @@ -264,7 +265,7 @@ public PostTotalPagingResponse pagingSupportPost(int page, String q, List<String

@Transactional(readOnly = true)
public PostTotalPagingResponse deadlineApproachingPosts(int page) {
PageRequest pageable = PageRequest.of(page, 4); // 페이지 크기를 4로 설정
PageRequest pageable = PageRequest.of(page, 4); // Set page size to 4
Page<SupportPost> supportPostPage = supportPostRepository.findByExpirationDateWithinSevenDays(pageable);

long totalElements = supportPostPage.getTotalElements();
Expand All @@ -275,17 +276,22 @@ public PostTotalPagingResponse deadlineApproachingPosts(int page) {

RecordProgressGetResponse supportRecordProgress = new RecordProgressGetResponse(totalPrice, supportedPrice);

// Attempt to find the first image, which may return null
SupportImage supportImage = supportImageRepository.findFirstImageBySupportPostIdOrDefault(supportPost.getId());
String imageUrl = (supportImage != null) ? fileService.getFileUrl(supportImage.getImageUrl()) : null; // Set to null if no image found

return new PostPagingResponse(
supportPost.getId(),
supportPost.getTitle(),
supportPost.getExpirationDate(),
fileService.getFileUrl(supportImageRepository.findFirstImageBySupportPostIdOrThrow(supportPost.getId()).getImageUrl()),
imageUrl,
supportRecordProgress
);
})
.collect(Collectors.toList());
int totalPages = supportPostPage.getTotalPages();
// 총 페이지 수와 게시글 목록으로 응답 객체 생성
// Create and return response object with total pages and post list
return new PostTotalPagingResponse((int) totalElements, totalPages, supportPosts);
}

}

0 comments on commit 7ad6704

Please sign in to comment.