|
|
@@ -0,0 +1,74 @@
|
|
|
+package com.seec.bok.service;
|
|
|
+
|
|
|
+import com.seec.bok.back.entity.Question;
|
|
|
+import com.seec.bok.back.repository.QuestionRepository;
|
|
|
+import com.seec.bok.service.exception.EntityNotFoundException;
|
|
|
+import com.seec.bok.web.util.FakeQuestionData;
|
|
|
+import com.seec.bok.web.vo.PageVO;
|
|
|
+import com.seec.bok.web.vo.PageableQuestionListVO;
|
|
|
+import com.seec.bok.web.vo.QuestionVO;
|
|
|
+import org.elasticsearch.index.query.QueryBuilder;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Qualifier;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.data.domain.Pageable;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Optional;
|
|
|
+import java.util.Set;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class QuestionService {
|
|
|
+ @Autowired
|
|
|
+ @Qualifier("QuestionRepositoryImp") // inject Spring implementation here
|
|
|
+ private QuestionRepository questionRepository;
|
|
|
+
|
|
|
+ public Page<Question> findByIdIn(Collection<String> id, Pageable pageable) {
|
|
|
+ return questionRepository.findByIdIn(id, pageable);
|
|
|
+ }
|
|
|
+
|
|
|
+ public QuestionVO findOne(String id) {
|
|
|
+ final Question question = questionRepository.findById(id).orElseThrow(EntityNotFoundException::new);
|
|
|
+ return QuestionVO.of(question);
|
|
|
+ }
|
|
|
+ private boolean noValue(String value){
|
|
|
+ return (value==null||"".equals(value));
|
|
|
+ }
|
|
|
+ public PageableQuestionListVO search(String stem, String labels, String knowledgeIds, Pageable pageable) {
|
|
|
+ Page<Question> questionPage=null;
|
|
|
+ if(stem==null){stem="";}
|
|
|
+ if(noValue(knowledgeIds)&&noValue(labels)){
|
|
|
+ questionPage = questionRepository.findByStemLike(stem, pageable);
|
|
|
+ }else if (noValue(knowledgeIds)){
|
|
|
+ questionPage = questionRepository.findByStemLikeAndLabelsContaining(stem,labels, pageable);
|
|
|
+ }else if (noValue(labels)){
|
|
|
+ questionPage = questionRepository.findByStemLikeAndKnowledgeIdsContaining(stem,knowledgeIds, pageable);
|
|
|
+ }else {
|
|
|
+ questionPage = questionRepository.findByStemLikeAndLabelsContainingAndKnowledgeIdsContaining(stem,labels,knowledgeIds, pageable);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ PageableQuestionListVO vos=PageableQuestionListVO.builder()
|
|
|
+ .questions(questionPage.getContent().stream().map(QuestionVO::of).collect(Collectors.toList()))
|
|
|
+ .page(
|
|
|
+ PageVO.builder()
|
|
|
+ .size(questionPage.getSize())
|
|
|
+ .totalPages(questionPage.getTotalPages())
|
|
|
+ .totalElements(questionPage.getTotalElements())
|
|
|
+ .number(questionPage.getNumber())
|
|
|
+ .build())
|
|
|
+ .build();
|
|
|
+ return vos;
|
|
|
+ }
|
|
|
+
|
|
|
+ public QuestionVO updateQuestion(QuestionVO vo) {
|
|
|
+ return QuestionVO.of(questionRepository.save(vo.toEntity()));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Page<Question> search(QueryBuilder query, Pageable pageable) {
|
|
|
+ return questionRepository.search(query, pageable);
|
|
|
+ }
|
|
|
+}
|