Skip to content

Commit

Permalink
feat: 숙소 Id에 해당하는 이미지들을 리스트로 반환하는 기능 구현 #4
Browse files Browse the repository at this point in the history
  • Loading branch information
Gyeongtaek12345 committed Jun 13, 2024
1 parent 50662ee commit 7365d57
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package com.team01.airdnb.image;

import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ImageRepository extends JpaRepository<Image, Long> {
Optional<List<Image>> findByAccommodationId(Long accommodationId);
}
21 changes: 19 additions & 2 deletions BE/airdnb/src/main/java/com/team01/airdnb/image/ImageService.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.team01.airdnb.image;

import com.team01.airdnb.image.dto.ImageListResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import org.springframework.stereotype.Service;

@Service
Expand All @@ -10,7 +14,20 @@ public ImageService(ImageRepository imageRepository) {
this.imageRepository = imageRepository;
}

public void save(Image image) {
imageRepository.save(image);
public List<Image> getImage(Long id) {
return imageRepository.findByAccommodationId(id)
.orElseThrow(() -> new NoSuchElementException("해당하는 어메니티가 존재하지 않습니다"));
}

public List<ImageListResponse> findByAccommodationId(Long id) {
List<Image> images = getImage(id);
List<ImageListResponse> imageListResponses = new ArrayList<>();

for (Image image : images) {
imageListResponses.add(ImageListResponse.builder()
.imagePath(image.getImagePath())
.build());
}
return imageListResponses;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package com.team01.airdnb.image.dto;

public class ImageListResponse {
import lombok.Builder;

@Builder
public record ImageListResponse(
String imagePath
) {

}

0 comments on commit 7365d57

Please sign in to comment.