Skip to content

Commit

Permalink
[Product] Fix error when remove images when create, update product, c…
Browse files Browse the repository at this point in the history
…ategory (#1015)

* fix remove images when update product

* fix create, update category image

* restore product service with checkstyle
  • Loading branch information
khanhduzz authored Sep 11, 2024
1 parent 56fda46 commit 46b0258
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
2 changes: 2 additions & 0 deletions backoffice/modules/catalog/components/CategoryImage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const CategoryImage = ({ id, image, setValue }: CategoryImageProps) => {

const onDeleteImage = () => {
setImageURL(null);
setValue('categoryImage', undefined);
setValue('imageId', undefined);
};

return (
Expand Down
1 change: 1 addition & 0 deletions backoffice/pages/catalog/categories/[id]/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ const CategoryEdit: NextPage = () => {

const onDeleteImage = () => {
setCategoryImage(null);
setImageId(undefined);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,18 @@
import com.yas.product.model.ProductImage;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductImageRepository extends JpaRepository<ProductImage, Long> {

@Modifying
@Query("DELETE FROM ProductImage p WHERE p.product.id = :productId AND p.imageId IN :imageIds")
void deleteByImageIdInAndProductId(List<Long> imageIds, Long productId);

@Modifying
@Query("DELETE FROM ProductImage p WHERE p.product.id = :productId")
void deleteByProductId(Long productId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -536,18 +536,19 @@ private static void setValuesForVariantExisting(
public List<ProductImage> setProductImages(List<Long> imageMediaIds, Product product) {
List<ProductImage> productImages = new ArrayList<>();
if (CollectionUtils.isEmpty(imageMediaIds)) {
productImageRepository.deleteByProductId(product.getId());
return productImages;
}
if (product.getProductImages() == null) {
productImages = imageMediaIds.stream()
.map(id -> ProductImage.builder().imageId(id).product(product).build()).toList();
.map(id -> ProductImage.builder().imageId(id).product(product).build()).toList();
} else {
List<Long> productImageIds = product.getProductImages().stream().map(ProductImage::getImageId).toList();
List<Long> newImageIds = imageMediaIds.stream().filter(id -> !productImageIds.contains(id)).toList();
List<Long> deletedImageIds = productImageIds.stream().filter(id -> !imageMediaIds.contains(id)).toList();
if (CollectionUtils.isNotEmpty(newImageIds)) {
productImages = newImageIds.stream()
.map(id -> ProductImage.builder().imageId(id).product(product).build()).toList();
.map(id -> ProductImage.builder().imageId(id).product(product).build()).toList();
}
if (CollectionUtils.isNotEmpty(deletedImageIds)) {
productImageRepository.deleteByImageIdInAndProductId(deletedImageIds, product.getId());
Expand Down

0 comments on commit 46b0258

Please sign in to comment.