Skip to content

Commit

Permalink
Merge pull request #145 from kakao-tech-campus-2nd-step3/Slider-10
Browse files Browse the repository at this point in the history
[์Šฌ๋ผ์ด๋”] ์Šฌ๋ผ์ด๋” ๋ฐฐ๋„ˆ API ๊ตฌํ˜„
  • Loading branch information
minsu-cnu authored Nov 11, 2024
2 parents 893d5d8 + 4c8ba75 commit 8629310
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 8629310

Please sign in to comment.