Skip to content

Commit

Permalink
#16 - Feat : 도서 등록시 도서 해시태그도 저장 되도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
ahah525 committed Oct 24, 2022
1 parent 5ca63f2 commit 2c488dc
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
}
Expand Down
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);
}
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);
}
}
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);
}
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;
}
}

0 comments on commit 2c488dc

Please sign in to comment.