diff --git a/src/main/java/team18/team18_be/slider/controller/SliderController.java b/src/main/java/team18/team18_be/slider/controller/SliderController.java new file mode 100644 index 0000000..ccb5d9a --- /dev/null +++ b/src/main/java/team18/team18_be/slider/controller/SliderController.java @@ -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> getSliderBanner() { + return ResponseEntity.ok().body(sliderService.getSliderImageUrls()); + } +} diff --git a/src/main/java/team18/team18_be/slider/dto/response/SliderImageResponse.java b/src/main/java/team18/team18_be/slider/dto/response/SliderImageResponse.java new file mode 100644 index 0000000..de88f89 --- /dev/null +++ b/src/main/java/team18/team18_be/slider/dto/response/SliderImageResponse.java @@ -0,0 +1,7 @@ +package team18.team18_be.slider.dto.response; + +public record SliderImageResponse( + String imageUrl +) { + +} diff --git a/src/main/java/team18/team18_be/slider/service/SliderService.java b/src/main/java/team18/team18_be/slider/service/SliderService.java new file mode 100644 index 0000000..e67eb78 --- /dev/null +++ b/src/main/java/team18/team18_be/slider/service/SliderService.java @@ -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 getSliderImageUrls() { + List 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; + } +} diff --git a/src/main/java/team18/team18_be/userInformation/service/UserInformationService.java b/src/main/java/team18/team18_be/userInformation/service/UserInformationService.java index 9fb1438..58959ef 100644 --- a/src/main/java/team18/team18_be/userInformation/service/UserInformationService.java +++ b/src/main/java/team18/team18_be/userInformation/service/UserInformationService.java @@ -6,6 +6,7 @@ import java.util.NoSuchElementException; import java.util.stream.Collectors; import org.apache.commons.lang3.RandomStringUtils; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import team18.team18_be.auth.entity.User; @@ -33,7 +34,8 @@ public class UserInformationService { private final AuthRepository authRepository; private final GcsUploader gcsUploader; private final FileUtil fileUtil; - + @Value("${company.default-logo-url}") + private String defaultLogoUrl; public UserInformationService(ForeignerInformationRepository foreignerInformationRepository, CompanyRepository companyRepository, SignRepository signRepository, @@ -48,11 +50,18 @@ public UserInformationService(ForeignerInformationRepository foreignerInformatio } public Long createCompany(CompanyRequest companyRequest, MultipartFile logoImage, User user) { - byte[] imageFile = fileUtil.safelyGetBytes(logoImage) - .orElseThrow(() -> new IllegalArgumentException("multipart 파일을 읽지 못하였습니다.")); - String storedFileName = gcsUploader.upload(imageFile, "companyLogo", - user.getId().toString() + logoImage.getOriginalFilename()) - .orElseThrow(() -> new NoSuchElementException("파일 업로드에 실패했습니다.")); + byte[] imageFile = null; + String storedFileName = null; + + if (logoImage.isEmpty()) { + storedFileName = defaultLogoUrl; + } else { + imageFile = fileUtil.safelyGetBytes(logoImage) + .orElseThrow(() -> new IllegalArgumentException("multipart 파일을 읽지 못하였습니다.")); + storedFileName = gcsUploader.upload(imageFile, "companyLogo", + user.getId().toString() + "Real" + logoImage.getOriginalFilename()) + .orElseThrow(() -> new NoSuchElementException("파일 업로드에 실패했습니다.")); + } Company company = new Company(companyRequest.name(), companyRequest.industryOccupation(), companyRequest.brand(), companyRequest.revenuePerYear(), storedFileName, user); Company savedCompany = companyRepository.save(company); @@ -72,7 +81,6 @@ private CompanyResponse createCompanyResponse(Company company) { return companyResponse; } - public Long fillInVisa(VisaRequest visaRequest, User user) { LocalDate visaGenerateDate = LocalDate.parse(visaRequest.visaGenerateDate()); LocalDate visaExpiryDate = visaGenerateDate.plusYears(10);