Skip to content

Commit

Permalink
fix(#91): 썸네일 이미지 경로도 함께 만들어서 저장 (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
yooyouny authored Oct 22, 2024
1 parent 39c52db commit dc6b74a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package com.sparta.product.application.dto;

public record ImgDto(String originImgUrl, String detailImgUrl) {}
public record ImgDto(String originImgUrl, String detailImgUrl, String thumbnailImgUrl) {}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ public String createProduct(
validateCategoryId(request.categoryId());
String productImgUrl = imageService.uploadImage("origin", productImg);
String detailImgUrl = imageService.uploadImage("detail", detailImg);
ProductResponse product = productService.createProduct(request, productImgUrl, detailImgUrl);
String thumbnailImgUrl = getThumbnailImgUrl(productImgUrl);
ProductResponse product =
productService.createProduct(
request, new ImgDto(productImgUrl, detailImgUrl, thumbnailImgUrl));
elasticSearchService.saveProduct(product);
return product.getProductId();
}
Expand All @@ -44,7 +47,6 @@ public ProductResponse updateProduct(
throws IOException {
validateCategoryId(request.categoryId());
Product savedProduct = productService.getSavedProduct(request.productId());

ImgDto imgData = fetchImgUrls(savedProduct, productImg, detailImg);
ProductResponse newProduct = productService.updateProduct(request, savedProduct, imgData);
elasticSearchService.updateProduct(newProduct);
Expand Down Expand Up @@ -72,15 +74,31 @@ private ImgDto fetchImgUrls(
Product savedProduct, MultipartFile productImg, MultipartFile detailImg) throws IOException {
String productImgUrl = savedProduct.getOriginImgUrl();
String detailImgUrl = savedProduct.getDetailImgUrl();
String thumbnailImgUrl = savedProduct.getThumbnailImgUrl();
if (productImg != null && !productImg.isEmpty()) {
imageService.deleteImage(productImgUrl);
imageService.deleteImage(thumbnailImgUrl);
productImgUrl = imageService.uploadImage("origin", productImg);
thumbnailImgUrl = getThumbnailImgUrl(productImgUrl);
}
if (detailImg != null && !detailImg.isEmpty()) {
imageService.deleteImage(detailImgUrl);
detailImgUrl = imageService.uploadImage("detail", detailImg);
}
return new ImgDto(productImgUrl, detailImgUrl);
return new ImgDto(productImgUrl, detailImgUrl, thumbnailImgUrl);
}

private String getThumbnailImgUrl(String productImgUrl) {
String[] parts = productImgUrl.split("/");
String fileNameWithExtension = parts[parts.length - 1];

String imageName = "resized-" + fileNameWithExtension;

String baseUrl =
productImgUrl
.substring(0, productImgUrl.lastIndexOf("/") + 1)
.replace("sasaping-products-origin", "sasaping-products-thumbnail");
return baseUrl + imageName;
}

private void validateCategoryId(Long categoryId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ public static ProductDto fromEntity(Product product) {
.build();
}

public static Product toEntity(
ProductCreateRequest request, String productImgUrl, String detailImgUrl) {
public static Product toEntity(ProductCreateRequest request, ImgDto imgDto) {
return Product.builder()
.categoryId(request.categoryId())
.productName(request.productName())
Expand All @@ -31,8 +30,9 @@ public static Product toEntity(
.description(request.description())
.originalPrice(request.originalPrice())
.discountPercent(request.discountPercent())
.originImgUrl(productImgUrl)
.detailImgUrl(detailImgUrl)
.originImgUrl(imgDto.originImgUrl())
.detailImgUrl(imgDto.detailImgUrl())
.thumbnailImgUrl(imgDto.thumbnailImgUrl())
.stock(request.stock())
.limitCountPerUser(request.limitCountPerUser())
.tags(request.tags())
Expand All @@ -53,6 +53,7 @@ public static void updateProduct(
request.description(),
imgUrls.originImgUrl(),
imgUrls.detailImgUrl(),
imgUrls.thumbnailImgUrl(),
request.limitCountPerUser(),
request.tags(),
request.isPublic());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ public class ProductService {
private final ProductRepository productRepository;

@Transactional
public ProductResponse createProduct(
ProductCreateRequest request, String productImgUrl, String detailImgUrl) {
Product newProduct = ProductMapper.toEntity(request, productImgUrl, detailImgUrl);
public ProductResponse createProduct(ProductCreateRequest request, ImgDto imgDto) {
Product newProduct = ProductMapper.toEntity(request, imgDto);
newProduct.setIsNew(true);
Product savedProduct = productRepository.save(newProduct);
return ProductResponse.fromEntity(savedProduct);
Expand Down Expand Up @@ -146,6 +145,5 @@ private void validateProductStock(Product product, int reduceCount) {
if (product.getStock() < reduceCount) {
throw new ProductServerException(ProductErrorCode.STOCK_NOT_AVAILABLE);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public void updateProduct(
String description,
String originImgUrl,
String detailImgUrl,
String thumbnailImgUrl,
Integer limitCountPerUser,
List<String> tags,
boolean isPublic) {
Expand All @@ -114,6 +115,7 @@ public void updateProduct(
this.description = description;
this.originImgUrl = originImgUrl;
this.detailImgUrl = detailImgUrl;
this.thumbnailImgUrl = thumbnailImgUrl;
this.tags = tags;
this.limitCountPerUser = limitCountPerUser;
this.isPublic = isPublic;
Expand Down

0 comments on commit dc6b74a

Please sign in to comment.