-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#16 - Feat : 도서 등록시 도서 해시태그도 저장 되도록 수정
- Loading branch information
Showing
5 changed files
with
150 additions
and
1 deletion.
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
13 changes: 13 additions & 0 deletions
13
...ain/java/com/example/mutbooks/app/productHashTag/repository/ProductHashTagRepository.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,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<ProductHashTag, Long> { | ||
List<ProductHashTag> findByProductId(Long productId); | ||
|
||
Optional<ProductHashTag> findByProductIdAndProductKeywordId(Long productId, Long productKeywordId); | ||
} |
84 changes: 84 additions & 0 deletions
84
.../src/main/java/com/example/mutbooks/app/productHashTag/service/ProductHashTagService.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,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<ProductHashTag> oldHashTags = findByProductId(product.getId()); | ||
|
||
// 2. 새로운 해시태그 키워드 리스트 | ||
List<String> keywordContents = Arrays.stream(productKeywords.split("#")) | ||
.map(String::trim) | ||
.filter(s -> s.length() > 0) | ||
.collect(Collectors.toList()); | ||
|
||
// 3. 삭제할 해시태그 구하기(기존 해시태그 리스트에서 새로운 해시태그 리스트에 없는 것) | ||
List<ProductHashTag> 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<ProductHashTag> findByProductId(Long productId) { | ||
return productHashTagRepository.findByProductId(productId); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...ain/java/com/example/mutbooks/app/productKeyword/repository/ProductKeywordRepository.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,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<ProductKeyword, Long> { | ||
Optional<ProductKeyword> findByContent(String content); | ||
} |
36 changes: 36 additions & 0 deletions
36
.../src/main/java/com/example/mutbooks/app/productKeyword/service/ProductKeywordService.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,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<ProductKeyword> 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; | ||
} | ||
} |