Skip to content

Commit 72ce773

Browse files
authored
Merge pull request #62 from Leets-Official/fix/#61-post-get
Fix: post 조회 예외처리 추가
2 parents 8219460 + ebb5a8e commit 72ce773

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package land.leets.Carrot.domain.post.repository;
22

33
import java.util.List;
4+
import java.util.Optional;
45
import land.leets.Carrot.domain.post.entity.PostImage;
56
import org.springframework.data.jpa.repository.JpaRepository;
67
import org.springframework.data.jpa.repository.Query;
78
import org.springframework.data.repository.query.Param;
89

910
public interface PostImageRepository extends JpaRepository<PostImage, Long> {
1011
@Query("SELECT p FROM PostImage p WHERE p.postSnapshot.id = :postsnapshotId")
11-
List<PostImage> findByPostSnapshotId(@Param("postsnapshotId") Long postsnapshotId);
12+
Optional<List<PostImage>> findByPostSnapshotId(@Param("postsnapshotId") Long postsnapshotId);
1213
}

src/main/java/land/leets/Carrot/domain/post/service/PostService.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,11 @@ public ResponseDto<PostResponse> getDetailPost(Long postId) {
155155
.orElseThrow(() -> new PostException(LATEST_SNAPSHOT_NOT_FOUND));
156156

157157
List<String> imageList = postImageRepository.findByPostSnapshotId(postSnapshot.getId())
158-
.stream()
159-
.map(image -> image.getImageUrl()).collect(Collectors.toList());
158+
.filter(list -> !list.isEmpty())
159+
.map(list -> list.stream()
160+
.map(imageRow -> imageRow.getImageUrl())
161+
.collect(Collectors.toList()))
162+
.orElse(null);
160163

161164
String workType = getWorkTypeName(postSnapshot.getWorkTypeId());
162165
PostResponse postResponse = new PostResponse(postId, post.getCeo().getId(), post.getStoreName(),
@@ -253,7 +256,7 @@ public ResponseDto<GetAppliedListResponse> getAppliedList(Long userId) {
253256
.filter(postSnapshot -> postSnapshot.getPost().getPostId() == apply.getPost().getPostId())
254257
.map(postSnapshot -> new AppliedPost(apply.getPost().getPostId(), postSnapshot.getTitle(),
255258
postSnapshot.getPost().getStoreName(),
256-
postImageRepository.findByPostSnapshotId(postSnapshot.getId()).get(0).getImageUrl(),
259+
getFirstImageUrl(postSnapshot.getId()),
257260
apply.isRecruited(), postSnapshot.getPost().getStatus().equals("done"))))
258261
.collect(Collectors.toList());
259262

@@ -265,7 +268,10 @@ public ResponseDto<GetAppliedListResponse> getAppliedList(Long userId) {
265268

266269

267270
private String getFirstImageUrl(Long postSnapshotId) {
268-
return postImageRepository.findByPostSnapshotId(postSnapshotId).get(0).getImageUrl();
271+
return postImageRepository.findByPostSnapshotId(postSnapshotId)
272+
.filter(list -> !list.isEmpty())
273+
.map(list -> list.get(0).getImageUrl())
274+
.orElse(null);
269275
}
270276

271277

0 commit comments

Comments
 (0)