-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from kakao-tech-campus-2nd-step3/feature/ISSU…
…E-167 이미지 validator, linker, deletelinker 수정
- Loading branch information
Showing
20 changed files
with
233 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
src/main/java/poomasi/domain/image/deleteLinker/ImageDeleteLinker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package poomasi.domain.image.deleteLinker; | ||
|
||
import poomasi.domain.image.entity.Image; | ||
import poomasi.domain.image.entity.ImageType; | ||
|
||
public interface ImageDeleteLinker { | ||
boolean supports(ImageType type); | ||
void handleImageDeletion(Image image); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/main/java/poomasi/domain/image/deleteLinker/ProductIntroDeleteLinker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package poomasi.domain.image.deleteLinker; | ||
|
||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import poomasi.domain.image.entity.Image; | ||
import poomasi.domain.image.entity.ImageType; | ||
import poomasi.domain.product._intro.entity.ProductIntro; | ||
import poomasi.domain.product._intro.service.ProductIntroService; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
@Component | ||
public class ProductIntroDeleteLinker implements ImageDeleteLinker { | ||
|
||
private final ProductIntroService productIntroService; | ||
|
||
public ProductIntroDeleteLinker(ProductIntroService productIntroService) { | ||
this.productIntroService = productIntroService; | ||
} | ||
|
||
@Override | ||
public boolean supports(ImageType type) { | ||
return type == ImageType.PRODUCT_INTRO; | ||
} | ||
|
||
@Transactional | ||
@Override | ||
public void handleImageDeletion(Image image) { | ||
ProductIntro productIntro = productIntroService.getIntroByIntroId(image.getReferenceId()); | ||
|
||
List<Image> images = Arrays.asList(productIntro.getMainImage(), productIntro.getSubImage1(), productIntro.getSubImage2(), productIntro.getSubImage3()); | ||
for (int i = 0; i < images.size(); i++) { | ||
if (images.get(i) == image) { | ||
switch (i) { | ||
case 0 -> productIntro.setMainImage(null); | ||
case 1 -> productIntro.setSubImage1(null); | ||
case 2 -> productIntro.setSubImage2(null); | ||
case 3 -> productIntro.setSubImage3(null); | ||
} | ||
} | ||
} | ||
productIntroService.saveExistedProductIntro(productIntro); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/poomasi/domain/image/linker/ProductIntroImageLinker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package poomasi.domain.image.linker; | ||
|
||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import poomasi.domain.image.entity.Image; | ||
import poomasi.domain.image.entity.ImageType; | ||
import poomasi.domain.product._intro.entity.ProductIntro; | ||
import poomasi.domain.product._intro.service.ProductIntroService; | ||
import poomasi.global.error.BusinessException; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static poomasi.global.error.BusinessError.IMAGE_LIMIT_EXCEED; | ||
|
||
@Service | ||
public class ProductIntroImageLinker implements ImageLinker { | ||
|
||
private final ProductIntroService productIntroService; | ||
|
||
public ProductIntroImageLinker(ProductIntroService productIntroService) { | ||
this.productIntroService = productIntroService; | ||
} | ||
|
||
@Override | ||
public boolean supports(ImageType type) { | ||
return type == ImageType.PRODUCT_INTRO; | ||
} | ||
|
||
@Transactional | ||
@Override | ||
public void link(Long referenceId, Image savedImage) { | ||
ProductIntro productIntro = productIntroService.getIntroByIntroId(referenceId); | ||
addImageToProductIntro(productIntro, savedImage); | ||
productIntroService.saveExistedProductIntro(productIntro); | ||
} | ||
|
||
private void addImageToProductIntro(ProductIntro productIntro, Image savedImage) { | ||
List<Image> images = Arrays.asList( | ||
productIntro.getMainImage(), | ||
productIntro.getSubImage1(), | ||
productIntro.getSubImage2(), | ||
productIntro.getSubImage3() | ||
); | ||
|
||
for (int i = 0; i < images.size(); i++) { | ||
if (images.get(i) == null) { | ||
setImageByIndex(productIntro, i, savedImage); | ||
return; | ||
} | ||
} | ||
|
||
throw new BusinessException(IMAGE_LIMIT_EXCEED); | ||
} | ||
|
||
private void setImageByIndex(ProductIntro productIntro, int index, Image image) { | ||
switch (index) { | ||
case 0 -> productIntro.setMainImage(image); | ||
case 1 -> productIntro.setSubImage1(image); | ||
case 2 -> productIntro.setSubImage2(image); | ||
case 3 -> productIntro.setSubImage3(image); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
src/main/java/poomasi/domain/image/validator/ImageOwnerValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
package poomasi.domain.image.validator; | ||
|
||
import poomasi.domain.image.entity.ImageType; | ||
|
||
public interface ImageOwnerValidator { | ||
boolean validateOwner(Long memberId, Long referenceId); | ||
boolean supports(ImageType type); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
src/main/java/poomasi/domain/image/validator/ProductIntroOwnerValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package poomasi.domain.image.validator; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
import poomasi.domain.image.entity.ImageType; | ||
import poomasi.domain.product._intro.repository.ProductIntroRepository; | ||
|
||
@Component | ||
@RequiredArgsConstructor | ||
public class ProductIntroOwnerValidator implements ImageOwnerValidator{ | ||
private final ProductIntroRepository productIntroRepository; | ||
|
||
@Override | ||
public boolean supports(ImageType type) { | ||
return type == ImageType.PRODUCT_INTRO; | ||
} | ||
|
||
@Override | ||
public boolean validateOwner(Long memberId, Long referenceId) { | ||
return productIntroRepository.findById(referenceId) | ||
.filter(productIntro -> productIntro.getFarmerId().equals(memberId)) | ||
.isPresent(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.