diff --git a/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/product/service/ProductService.java b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/product/service/ProductService.java index b282396..197cfb3 100644 --- a/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/product/service/ProductService.java +++ b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/product/service/ProductService.java @@ -6,6 +6,7 @@ import com.example.mutbooks.app.product.entity.Product; import com.example.mutbooks.app.product.form.ProductForm; import com.example.mutbooks.app.product.repository.ProductRepository; +import com.example.mutbooks.app.productHashTag.service.ProductHashTagService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -16,6 +17,7 @@ public class ProductService { private final ProductRepository productRepository; private final PostKeywordService postKeywordService; + private final ProductHashTagService productHashTagService; @Transactional public Product create(Member author, ProductForm productForm) { @@ -31,7 +33,11 @@ public Product create(Member author, ProductForm productForm) { productRepository.save(product); - // TODO : 도서 해시태그 저장 + // 도서 해시태그 적용 + String productKeywords = productForm.getProductKeywords(); + if(productKeywords != null) { + productHashTagService.apply(product, productKeywords); + } return product; } diff --git a/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productHashTag/repository/ProductHashTagRepository.java b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productHashTag/repository/ProductHashTagRepository.java new file mode 100644 index 0000000..177afea --- /dev/null +++ b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productHashTag/repository/ProductHashTagRepository.java @@ -0,0 +1,13 @@ +package com.example.mutbooks.app.productHashTag.repository; + +import com.example.mutbooks.app.productHashTag.entity.ProductHashTag; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; +import java.util.Optional; + +public interface ProductHashTagRepository extends JpaRepository { + List findByProductId(Long productId); + + Optional findByProductIdAndProductKeywordId(Long productId, Long productKeywordId); +} diff --git a/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productHashTag/service/ProductHashTagService.java b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productHashTag/service/ProductHashTagService.java new file mode 100644 index 0000000..9c317e2 --- /dev/null +++ b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productHashTag/service/ProductHashTagService.java @@ -0,0 +1,84 @@ +package com.example.mutbooks.app.productHashTag.service; + +import com.example.mutbooks.app.product.entity.Product; +import com.example.mutbooks.app.productHashTag.entity.ProductHashTag; +import com.example.mutbooks.app.productHashTag.repository.ProductHashTagRepository; +import com.example.mutbooks.app.productKeyword.entity.ProductKeyword; +import com.example.mutbooks.app.productKeyword.service.ProductKeywordService; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class ProductHashTagService { + private final ProductHashTagRepository productHashTagRepository; + private final ProductKeywordService productKeywordService; + + // 도서(상품)에 해시태그 반영 + @Transactional + public void apply(Product product, String productKeywords) { + // 1. 기존 해시태그 가져오기 + List oldHashTags = findByProductId(product.getId()); + + // 2. 새로운 해시태그 키워드 리스트 + List keywordContents = Arrays.stream(productKeywords.split("#")) + .map(String::trim) + .filter(s -> s.length() > 0) + .collect(Collectors.toList()); + + // 3. 삭제할 해시태그 구하기(기존 해시태그 리스트에서 새로운 해시태그 리스트에 없는 것) + List deleteHashTags = new ArrayList<>(); + for(ProductHashTag oldHashTag : oldHashTags) { + // 기존에 등록된 해시태그가 새롭게 등록된 해시태그에 포함되었는지 여부 + boolean contains = keywordContents.stream().anyMatch(s -> s.equals(oldHashTag.getProductKeyword().getContent())); + + if(!contains) { + deleteHashTags.add(oldHashTag); + } + } + + // 4. 3번에서 구한 해시태그 삭제 + deleteHashTags.forEach(hashTag -> { + productHashTagRepository.delete(hashTag); + }); + + // 5. 나머지 해시태그는 저장 + keywordContents.forEach(keywordContent -> { + save(product, keywordContent); + }); + } + + // 도서 해시태그 저장 + public ProductHashTag save(Product product, String keywordContent) { + // 1. keyword 가져오기 + ProductKeyword productKeyword = productKeywordService.save(keywordContent); + + // 2. (productId + keywordId) 가 DB에 있으면 바로 리턴 + ProductHashTag productHashTag = productHashTagRepository.findByProductIdAndProductKeywordId(product.getId(), productKeyword.getId()).orElse(null); + if(productHashTag != null) { + return productHashTag; + } + + // 3. (productId + keywordId) 로 DB에 없으면 productHashTag 저장 + productHashTag = ProductHashTag.builder() + .member(product.getAuthor()) + .product(product) + .productKeyword(productKeyword) + .build(); + + productHashTagRepository.save(productHashTag); + + return productHashTag; + } + + private List findByProductId(Long productId) { + return productHashTagRepository.findByProductId(productId); + } +} \ No newline at end of file diff --git a/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productKeyword/repository/ProductKeywordRepository.java b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productKeyword/repository/ProductKeywordRepository.java new file mode 100644 index 0000000..faf58a0 --- /dev/null +++ b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productKeyword/repository/ProductKeywordRepository.java @@ -0,0 +1,10 @@ +package com.example.mutbooks.app.productKeyword.repository; + +import com.example.mutbooks.app.productKeyword.entity.ProductKeyword; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface ProductKeywordRepository extends JpaRepository { + Optional findByContent(String content); +} diff --git a/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productKeyword/service/ProductKeywordService.java b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productKeyword/service/ProductKeywordService.java new file mode 100644 index 0000000..1d9b2e9 --- /dev/null +++ b/2Week_Mission/mutbooks/src/main/java/com/example/mutbooks/app/productKeyword/service/ProductKeywordService.java @@ -0,0 +1,36 @@ +package com.example.mutbooks.app.productKeyword.service; + +import com.example.mutbooks.app.productKeyword.entity.ProductKeyword; +import com.example.mutbooks.app.productKeyword.repository.ProductKeywordRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; + +@Service +@RequiredArgsConstructor +@Transactional(readOnly = true) +public class ProductKeywordService { + private final ProductKeywordRepository productKeywordRepository; + + // 도서 해시태그 키워드 저장 + @Transactional + public ProductKeyword save(String content) { + Optional optionalProductKeyword = productKeywordRepository.findByContent(content); + + // 1. 해당 키워드가(content)가 DB에 있으면 바로 리턴 + if (optionalProductKeyword.isPresent()) { + return optionalProductKeyword.get(); + } + + // 2. 해당 키워드(content)가 DB에 없으면 저장 + ProductKeyword productKeyword = ProductKeyword.builder() + .content(content) + .build(); + + productKeywordRepository.save(productKeyword); + + return productKeyword; + } +}