Skip to content

Commit

Permalink
Merge pull request #62 from Leets-Official/fix/#61-post-get
Browse files Browse the repository at this point in the history
Fix: post 조회 예외처리 추가
  • Loading branch information
codingmy authored Nov 11, 2024
2 parents 8219460 + ebb5a8e commit 72ce773
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package land.leets.Carrot.domain.post.repository;

import java.util.List;
import java.util.Optional;
import land.leets.Carrot.domain.post.entity.PostImage;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface PostImageRepository extends JpaRepository<PostImage, Long> {
@Query("SELECT p FROM PostImage p WHERE p.postSnapshot.id = :postsnapshotId")
List<PostImage> findByPostSnapshotId(@Param("postsnapshotId") Long postsnapshotId);
Optional<List<PostImage>> findByPostSnapshotId(@Param("postsnapshotId") Long postsnapshotId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,11 @@ public ResponseDto<PostResponse> getDetailPost(Long postId) {
.orElseThrow(() -> new PostException(LATEST_SNAPSHOT_NOT_FOUND));

List<String> imageList = postImageRepository.findByPostSnapshotId(postSnapshot.getId())
.stream()
.map(image -> image.getImageUrl()).collect(Collectors.toList());
.filter(list -> !list.isEmpty())
.map(list -> list.stream()
.map(imageRow -> imageRow.getImageUrl())
.collect(Collectors.toList()))
.orElse(null);

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

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


private String getFirstImageUrl(Long postSnapshotId) {
return postImageRepository.findByPostSnapshotId(postSnapshotId).get(0).getImageUrl();
return postImageRepository.findByPostSnapshotId(postSnapshotId)
.filter(list -> !list.isEmpty())
.map(list -> list.get(0).getImageUrl())
.orElse(null);
}


Expand Down

0 comments on commit 72ce773

Please sign in to comment.