Skip to content

Commit

Permalink
feat: 슬라이더 배너 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
minsu-cnu committed Nov 11, 2024
1 parent 893d5d8 commit 4c8ba75
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package team18.team18_be.slider.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import team18.team18_be.slider.dto.response.SliderImageResponse;
import team18.team18_be.slider.service.SliderService;

@Tag(name = "슬라이더", description = "슬라이더 관련 API")
@RestController
@RequestMapping("/api/slides")
public class SliderController {

private final SliderService sliderService;

public SliderController(SliderService sliderService) {
this.sliderService = sliderService;
}

@GetMapping
public ResponseEntity<List<SliderImageResponse>> getSliderBanner() {
return ResponseEntity.ok().body(sliderService.getSliderImageUrls());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package team18.team18_be.slider.dto.response;

public record SliderImageResponse(
String imageUrl
) {

}
39 changes: 39 additions & 0 deletions src/main/java/team18/team18_be/slider/service/SliderService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package team18.team18_be.slider.service;

import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import team18.team18_be.slider.dto.response.SliderImageResponse;

@Service
public class SliderService {

public static final String IMAGE = "image/";
private static final String DIRECTORY_IMAGES = "Slider";
private final Storage storage;
@Value("${gcp.bucket.name}")
private String BUCKET_NAME;

public SliderService(Storage storage) {
this.storage = storage;
}

public List<SliderImageResponse> getSliderImageUrls() {
List<SliderImageResponse> imageUrls = new ArrayList<>();
Bucket bucket = storage.get(BUCKET_NAME);

for (Blob blob : bucket.list(Storage.BlobListOption.prefix(DIRECTORY_IMAGES)).iterateAll()) {
if (!blob.isDirectory() && blob.getContentType() != null && blob.getContentType().startsWith(
IMAGE)) {
String imageUrl = blob.getMediaLink();
imageUrls.add(new SliderImageResponse(imageUrl));
}
}

return imageUrls;
}
}

0 comments on commit 4c8ba75

Please sign in to comment.