Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] FAQ 검색기능 추가 #222

Merged
merged 11 commits into from
Nov 18, 2024
Prev Previous commit
Next Next commit
[feat] FaqService keyword 관련 로직 추가 및 테스트
wnd01jun committed Nov 13, 2024
commit f6166e6dd77ed8f60778de33dd805456f712bab1
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import com.bbteam.budgetbuddies.domain.faq.dto.FaqRequestDto;
import com.bbteam.budgetbuddies.domain.faq.dto.FaqResponseDto;
import com.bbteam.budgetbuddies.domain.faqkeyword.dto.FaqKeywordResponseDto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

@@ -18,4 +19,8 @@ public interface FaqService {
FaqResponseDto.FaqModifyResponse modifyFaq(FaqRequestDto.FaqModifyRequest dto, Long faqId);

String deleteFaq(Long faqId);

FaqKeywordResponseDto addKeyword(Long faqId, Long searchKeywordId);

String removeKeyword(Long faqId, Long searchKeywordId);
}
Original file line number Diff line number Diff line change
@@ -7,6 +7,11 @@
import com.bbteam.budgetbuddies.domain.faq.dto.FaqResponseDto;
import com.bbteam.budgetbuddies.domain.faq.entity.Faq;
import com.bbteam.budgetbuddies.domain.faq.repository.FaqRepository;
import com.bbteam.budgetbuddies.domain.faqkeyword.domain.FaqKeyword;
import com.bbteam.budgetbuddies.domain.faqkeyword.dto.FaqKeywordResponseDto;
import com.bbteam.budgetbuddies.domain.faqkeyword.repository.FaqKeywordRepository;
import com.bbteam.budgetbuddies.domain.searchkeyword.domain.SearchKeyword;
import com.bbteam.budgetbuddies.domain.searchkeyword.repository.SearchKeywordRepository;
import com.bbteam.budgetbuddies.domain.user.entity.User;
import com.bbteam.budgetbuddies.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
@@ -22,6 +27,8 @@ public class FaqServiceImpl implements FaqService{

private final FaqRepository faqRepository;
private final UserRepository userRepository;
private final FaqKeywordRepository faqKeywordRepository;
private final SearchKeywordRepository searchKeywordRepository;

@Override
public FaqResponseDto.FaqFindResponse findOneFaq(Long faqId) {
@@ -67,4 +74,31 @@ private Faq findFaq(Long faqId) {
public Page<FaqResponseDto.FaqFindResponse> searchFaq(Pageable pageable, String searchCondition) {
return faqRepository.searchFaq(pageable, searchCondition).map(FaqConverter::entityToFind);
}

@Override
@Transactional
public FaqKeywordResponseDto addKeyword(Long faqId, Long searchKeywordId) {
Faq faq = findFaq(faqId);
SearchKeyword searchKeyword = searchKeywordRepository.findById(searchKeywordId).orElseThrow(() -> new GeneralException(ErrorStatus._SEARCH_KEYWORD_NOT_FOUND));

FaqKeyword faqKeyword = FaqKeyword.builder()
.searchKeyword(searchKeyword)
.faq(faq)
.build();

faqKeywordRepository.save(faqKeyword);
return FaqKeywordResponseDto.toDto(faqKeyword);
}

@Override
@Transactional
public String removeKeyword(Long faqId, Long searchKeywordId) {
Faq faq = findFaq(faqId);
SearchKeyword searchKeyword = searchKeywordRepository.findById(searchKeywordId).orElseThrow(() -> new GeneralException(ErrorStatus._SEARCH_KEYWORD_NOT_FOUND));

FaqKeyword faqKeyword = faqKeywordRepository.findByFaqAndSearchKeyword(faq, searchKeyword).orElseThrow(() -> new GeneralException(ErrorStatus._FAQ_KEYWORD_NOT_FOUND));
faqKeywordRepository.delete(faqKeyword);

return "ok";
}
}
Original file line number Diff line number Diff line change
@@ -4,6 +4,10 @@
import com.bbteam.budgetbuddies.domain.faq.dto.FaqResponseDto;
import com.bbteam.budgetbuddies.domain.faq.entity.Faq;
import com.bbteam.budgetbuddies.domain.faq.repository.FaqRepository;
import com.bbteam.budgetbuddies.domain.faqkeyword.dto.FaqKeywordResponseDto;
import com.bbteam.budgetbuddies.domain.faqkeyword.repository.FaqKeywordRepository;
import com.bbteam.budgetbuddies.domain.searchkeyword.domain.SearchKeyword;
import com.bbteam.budgetbuddies.domain.searchkeyword.repository.SearchKeywordRepository;
import com.bbteam.budgetbuddies.domain.user.entity.User;
import com.bbteam.budgetbuddies.domain.user.repository.UserRepository;
import org.assertj.core.api.Assertions;
@@ -14,6 +18,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
@@ -31,6 +36,10 @@ class FaqServiceTest {
FaqRepository faqRepository;
@Autowired
UserRepository userRepository;
@Autowired
FaqKeywordRepository faqKeywordRepository;
@Autowired
SearchKeywordRepository searchKeywordRepository;

static Long userId;
static Long faqId;
@@ -149,4 +158,35 @@ void deleteFaq() {

assertThat(faq.isEmpty()).isTrue();
}

@Test
void addKeywordAndRemoveKeyword() {
User user = userRepository.findById(userId).get();

Faq faq1 = Faq.builder()
.title("test1")
.body("test1")
.user(user)
.build();
faqRepository.save(faq1);

SearchKeyword searchKeyword = SearchKeyword.builder()
.keyword("testKeyword")
.build();
searchKeywordRepository.save(searchKeyword);
faqService.addKeyword(faq1.getId(), searchKeyword.getId());
PageRequest pageRequest = PageRequest.of(0, 1);

Page<FaqResponseDto.FaqFindResponse> result1 = faqService.searchFaq(pageRequest, "test");
Assertions.assertThat(result1.getNumberOfElements()).isEqualTo(1);

Page<FaqResponseDto.FaqFindResponse> result2 = faqService.searchFaq(pageRequest, "no");
Assertions.assertThat(result2.getNumberOfElements()).isEqualTo(0);

faqService.removeKeyword(faq1.getId(), searchKeyword.getId());
Page<FaqResponseDto.FaqFindResponse> result3 = faqService.searchFaq(pageRequest, "test");
Assertions.assertThat(result3.getNumberOfElements()).isEqualTo(0);


}
}