Skip to content

Commit

Permalink
Update ImageCrudServiceImpl.java
Browse files Browse the repository at this point in the history
  • Loading branch information
jinu0328 committed Jan 5, 2025
1 parent 33626ca commit 07d2b26
Showing 1 changed file with 19 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@

import com.gulbi.Backend.domain.rental.product.code.ImageErrorCode;
import com.gulbi.Backend.domain.rental.product.dto.ProductImageDto;
import com.gulbi.Backend.domain.rental.product.dto.ProductImageDtoCollection;
import com.gulbi.Backend.domain.rental.product.dto.product.request.ProductImageDeleteRequestDto;
import com.gulbi.Backend.domain.rental.product.dto.product.update.ProductMainImageUpdateDto;
import com.gulbi.Backend.domain.rental.product.entity.Product;
import com.gulbi.Backend.domain.rental.product.exception.ImageException;
import com.gulbi.Backend.domain.rental.product.factory.ImageFactory;
import com.gulbi.Backend.domain.rental.product.repository.ImageRepository;
import com.gulbi.Backend.domain.rental.product.service.product.crud.ProductCrudService;
import com.gulbi.Backend.domain.rental.product.vo.image.ImageCollection;
import com.gulbi.Backend.domain.rental.product.vo.image.ImageUrl;
import com.gulbi.Backend.domain.rental.product.vo.image.ImageUrlCollection;
import com.gulbi.Backend.domain.rental.product.dto.ProductImageDtoCollection;
import com.gulbi.Backend.domain.rental.product.vo.image.ImageCollection;
import com.gulbi.Backend.domain.rental.product.vo.image.ProductImageCollection;
import com.gulbi.Backend.global.util.FileSender;
import com.gulbi.Backend.global.util.S3Uploader;
import jakarta.persistence.PersistenceException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ImageCrudServiceImpl implements ImageCrudService {

private final ImageRepository imageRepository;
private final ProductCrudService productCrudService;
private final FileSender fileSender;
private final S3Uploader s3Uploader;

@Override
public void registerImagesWithProduct(ImageUrlCollection imageUrlCollection, Product product) {
Expand All @@ -42,18 +41,17 @@ public void registerImagesWithProduct(ImageUrlCollection imageUrlCollection, Pro

@Override
public ImageUrlCollection uploadImagesToS3(ProductImageCollection productImageCollection) {
try{
try {
List<ImageUrl> imageUrlList = new ArrayList<>();
for (MultipartFile file : productImageCollection.getProductImages()){
ImageUrl imageUrl = ImageUrl.of(fileSender.sendFile(file));
for (MultipartFile file : productImageCollection.getProductImages()) {
ImageUrl imageUrl = ImageUrl.of(s3Uploader.uploadFile(file, "images"));
imageUrlList.add(imageUrl);
}
return ImageUrlCollection.of(imageUrlList);
}catch (ImageException e){ // 추후 센더에 예외 생기면 센더에서 예외 호출 예정
} catch (ImageException e) { // 추후 센더에 예외 생기면 센더에서 예외 호출 예정

throw new ImageException.NotUploadImageToS3Exception(ImageErrorCode.CANT_UPLOAD_IMAGE_TO_S3);
}
catch (IOException e) {
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Expand All @@ -75,15 +73,16 @@ public ProductImageDtoCollection getImageByProductId(Long productId) {

@Override
public void saveMainImage(ImageUrl mainImageUrl, Product product) {
imageRepository.save(ImageFactory.createImageToMain(mainImageUrl,product));
imageRepository.save(ImageFactory.createImageToMain(mainImageUrl, product));
}

@Override
public void clearMainImageFlags(Product product){
public void clearMainImageFlags(Product product) {
imageRepository.resetMainImagesByProduct(product);
}

@Override
public void saveImages(ImageCollection imageCollection){
public void saveImages(ImageCollection imageCollection) {
try {
imageRepository.saveAll(imageCollection.getImages());
} catch (DataIntegrityViolationException | JpaSystemException | PersistenceException |
Expand All @@ -94,10 +93,10 @@ public void saveImages(ImageCollection imageCollection){

@Override
public void deleteImages(ProductImageDeleteRequestDto productImageDeleteRequestDto) {
if(productImageDeleteRequestDto.getImagesId()==null){
if (productImageDeleteRequestDto.getImagesId() == null) {
throw new ImageException.NotContainedImageIdException(ImageErrorCode.NOT_CONTAINED_IMAGE_ID);
}
try{
try {
imageRepository.deleteImages(productImageDeleteRequestDto);
} catch (DataIntegrityViolationException e) {
throw new ImageException.ImageDeleteValidationException(ImageErrorCode.IMAGE_DELETE_FAILED);
Expand All @@ -112,7 +111,7 @@ public void deleteImages(ProductImageDeleteRequestDto productImageDeleteRequestD
}


private Product resolveProduct(Long productId){
private Product resolveProduct(Long productId) {
return productCrudService.getProductById(productId);
}

Expand Down

0 comments on commit 07d2b26

Please sign in to comment.