XuShengTao 6 лет назад
Родитель
Сommit
795b415583

+ 19 - 1
pom.xml

@@ -42,7 +42,7 @@
         <!--</dependency>-->
             <dependency>
                 <groupId>org.springframework.data</groupId>
-                <artifactId>spring-data-rest-hal-browser</artifactId>
+                <artifactId>spring-data-rest-hal-explorer</artifactId>
             </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
@@ -59,6 +59,24 @@
             <optional>true</optional>
         </dependency>
 
+        <dependency>
+            <groupId>com.graphql-java</groupId>
+            <artifactId>graphql-spring-boot-starter</artifactId>
+            <version>5.0.2</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.graphql-java</groupId>
+            <artifactId>graphiql-spring-boot-starter</artifactId>
+            <version>5.0.2</version>
+        </dependency>
+
+        <dependency>
+            <groupId>com.graphql-java</groupId>
+            <artifactId>graphql-java-tools</artifactId>
+            <version>5.2.4</version>
+        </dependency>
+
         <dependency>
             <groupId>org.springframework.restdocs</groupId>
             <artifactId>spring-restdocs-mockmvc</artifactId>

+ 2 - 1
src/main/java/com/es/demo/TransportClientConfig.java

@@ -30,7 +30,8 @@ public class TransportClientConfig extends ElasticsearchConfigurationSupport {
     public Client elasticsearchClient() throws UnknownHostException {
         Settings settings = Settings.builder().put("cluster.name", "se4-es").build();
         TransportClient client = new PreBuiltTransportClient(settings);
-        client.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.68.76"), 9300));
+        //client.addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.68.76"), 9300));
+        client.addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 19300));
         return client;
     }
 

+ 3 - 0
src/main/java/com/es/demo/back/ChoiceQuestion.java

@@ -50,6 +50,9 @@ public class ChoiceQuestion {
     @JsonProperty(value = "tags")
     private Set<String> tags;
 
+    @JsonProperty(value = "knowledgeId")
+    private Set<String> knowledgeId;
+
     @JsonProperty(value = "lastModified")
     @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
     private Date lastModifiedTime;

+ 2 - 1
src/main/java/com/es/demo/back/TQRepository.java

@@ -6,6 +6,7 @@ import org.springframework.data.elasticsearch.repository.ElasticsearchRepository
 import org.springframework.data.rest.core.annotation.RepositoryRestResource;
 
 import java.util.Collection;
+import java.util.List;
 import java.util.Optional;
 import java.util.Set;
 
@@ -20,13 +21,13 @@ import java.util.Set;
 @RepositoryRestResource(path = "tq")
 public interface  TQRepository extends ElasticsearchRepository<ChoiceQuestion,Integer> {
     Page<ChoiceQuestion> findAll(Pageable pageable);
+    Page<ChoiceQuestion> findByIdIn(Collection<Integer> id, Pageable pageable);
     Page<ChoiceQuestion> findAllByIdBetween( Pageable pageable,Integer start, Integer end);
     Page<ChoiceQuestion> findByStemLike(String content,Pageable pageable);
     int countAllByIdBetween(Integer id, Integer id2);
     int deleteAllByIdBetween(Integer id, Integer id2);
     void deleteById(Integer integer);
     Optional<ChoiceQuestion> findById(Integer integer);
-
     Page<ChoiceQuestion> findAllByTagsContaining(String tag, Pageable pageable);
     Page<ChoiceQuestion> findAllByTagsNotIn(Collection<String> tags, Pageable pageable);
     @Override

+ 5 - 0
src/main/java/com/es/demo/back/TQRepositoryImp.java

@@ -36,6 +36,11 @@ public class TQRepositoryImp  implements TQRepository {
         return TQRepository.findAll(pageable);
     }
 
+    @Override
+    public Page<ChoiceQuestion> findByIdIn(Collection<Integer> id, Pageable pageable) {
+        return TQRepository.findByIdIn(id,pageable);
+    }
+
     @Override
     public Page<ChoiceQuestion> findAllByIdBetween(Pageable pageable, Integer start, Integer end) {
         return TQRepository.findAllByIdBetween(pageable, start, end);

+ 32 - 0
src/main/java/com/es/demo/graphql/resolver/PostQuery.java

@@ -0,0 +1,32 @@
+package com.es.demo.graphql.resolver;
+
+import com.coxautodev.graphql.tools.GraphQLQueryResolver;
+import com.es.demo.back.ChoiceQuestion;
+import com.es.demo.back.TQRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.stereotype.Component;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * @author HelloWood
+ */
+@Component
+public class PostQuery implements GraphQLQueryResolver {
+
+    private final TQRepository tqRepository;
+
+    @Autowired
+    public PostQuery(@Qualifier("TQRepositoryImp") TQRepository tqRepository) {
+        this.tqRepository = tqRepository;
+    }
+
+
+    public Optional<ChoiceQuestion> tq(Integer id) {
+        return tqRepository.findById(id);
+    }
+
+}

+ 60 - 0
src/main/java/com/es/demo/recommend/RecomRestfulController.java

@@ -0,0 +1,60 @@
+package com.es.demo.recommend;
+
+import com.es.demo.back.ChoiceQuestion;
+import com.es.demo.back.TQRepository;
+import com.es.demo.recommend.dto.GetRecommend;
+import com.es.demo.recommend.dto.GetRecommendAnswer;
+import com.es.demo.recommend.vo.AnswerTQ;
+import com.es.demo.recommend.vo.NoAnswerTQ;
+import com.es.demo.recommend.vo.Res;
+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.PageRequest;
+import org.springframework.data.domain.Sort;
+import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
+import org.springframework.web.bind.annotation.*;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * @ClassName recomRestfulController
+ * @PackageName com.es.demo
+ * @Author sheen
+ * @Date 2020/1/2
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@RestController
+@RequestMapping("/api/question")
+public class RecomRestfulController {
+    private final TQRepository tqRepository;
+
+    @Autowired
+    public RecomRestfulController(@Qualifier("TQRepositoryImp") TQRepository tqRepository) {
+        this.tqRepository = tqRepository;
+    }
+
+    @PostMapping("/recommend")
+    public Res<List<NoAnswerTQ>> getRecommend(
+            @RequestBody GetRecommend body) {
+        final Page<ChoiceQuestion> all = tqRepository.findAll(PageRequest.of(0, body.questionNum.intValue()));
+
+        return new Res<List<NoAnswerTQ>>(
+                all.getContent().stream()
+                        .map(NoAnswerTQ::new)
+                        .collect(Collectors.toList()));
+    }
+    @PostMapping("/answer")
+    public Res<List<AnswerTQ>> getRecommendWithAnswer(
+            @RequestBody GetRecommendAnswer body) {
+        final Page<ChoiceQuestion> all = tqRepository.findByIdIn(body.getQuestionId(),PageRequest.of(0, body.questionId.size()));
+
+        return new Res<List<AnswerTQ>>(
+                all.getContent().stream()
+                        .map(AnswerTQ::new)
+                        .collect(Collectors.toList()));
+    }
+}

+ 21 - 0
src/main/java/com/es/demo/recommend/dto/GetRecommend.java

@@ -0,0 +1,21 @@
+package com.es.demo.recommend.dto;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @ClassName GetRecommend
+ * @PackageName com.es.demo.recommend.dto
+ * @Author sheen
+ * @Date 2020/1/2
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@Data
+public class GetRecommend {
+    public Long courseId;
+    public Long userId;
+    public List<String> knowledgeId;
+    public Long questionNum;
+}

+ 18 - 0
src/main/java/com/es/demo/recommend/dto/GetRecommendAnswer.java

@@ -0,0 +1,18 @@
+package com.es.demo.recommend.dto;
+
+import lombok.Data;
+
+import java.util.List;
+
+/**
+ * @ClassName GetRecommend
+ * @PackageName com.es.demo.recommend.dto
+ * @Author sheen
+ * @Date 2020/1/2
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@Data
+public class GetRecommendAnswer {
+    public List<Integer> questionId;
+}

+ 58 - 0
src/main/java/com/es/demo/recommend/vo/AnswerTQ.java

@@ -0,0 +1,58 @@
+package com.es.demo.recommend.vo;
+
+import com.es.demo.back.ChoiceQuestion;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+import org.springframework.beans.BeanUtils;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @ClassName AnswerTQ
+ * @PackageName com.es.demo.recommend.vo
+ * @Author sheen
+ * @Date 2020/1/2
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@Data
+public class AnswerTQ {
+    @JsonProperty(value = "id")
+    private Integer id;
+
+    @JsonProperty(value = "type")
+    private String type;
+
+    @JsonProperty(value = "stem")
+    private String stem;
+
+    @JsonProperty(value = "options")
+    private HashMap options;
+
+    @JsonProperty(value = "answer")
+    private String answer;
+
+    @JsonProperty(value = "analysis")
+    private String analysis;
+
+    @JsonProperty(value = "tags")
+    private Set<String> tags;
+
+    @JsonProperty(value = "knowledgeId")
+    private Set<String> knowledgeId;
+
+    @JsonProperty(value = "lastModified")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private Date lastModifiedTime;
+
+
+    public AnswerTQ(ChoiceQuestion choiceQuestion) {
+        BeanUtils.copyProperties(choiceQuestion, this);
+        if(tags==null){tags=new HashSet<>(); }
+        if(knowledgeId==null){knowledgeId=new HashSet<>(); }
+    }
+}

+ 52 - 0
src/main/java/com/es/demo/recommend/vo/NoAnswerTQ.java

@@ -0,0 +1,52 @@
+package com.es.demo.recommend.vo;
+
+import com.es.demo.back.ChoiceQuestion;
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import lombok.Data;
+import org.springframework.beans.BeanUtils;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ * @ClassName NoAnswerTQ
+ * @PackageName com.es.demo.recommend.vo
+ * @Author sheen
+ * @Date 2020/1/2
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@Data
+public class NoAnswerTQ {
+    @JsonProperty(value = "id")
+    private Integer id;
+
+    @JsonProperty(value = "type")
+    private String type;
+
+    @JsonProperty(value = "stem")
+    private String stem;
+
+    @JsonProperty(value = "options")
+    private HashMap options;
+
+    @JsonProperty(value = "tags")
+    private Set<String> tags;
+
+    @JsonProperty(value = "knowledgeId")
+    private Set<String> knowledgeId;
+
+    @JsonProperty(value = "lastModified")
+    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
+    private Date lastModifiedTime;
+
+
+    public NoAnswerTQ(ChoiceQuestion choiceQuestion) {
+        BeanUtils.copyProperties(choiceQuestion, this);
+        if(tags==null){tags=new HashSet<>(); }
+        if(knowledgeId==null){knowledgeId=new HashSet<>(); }
+    }
+}

+ 18 - 0
src/main/java/com/es/demo/recommend/vo/Res.java

@@ -0,0 +1,18 @@
+package com.es.demo.recommend.vo;
+
+import lombok.AllArgsConstructor;
+import lombok.Data;
+
+/**
+ * @ClassName Res
+ * @PackageName com.es.demo.recommend.vo
+ * @Author sheen
+ * @Date 2020/1/2
+ * @Version 1.0
+ * @Description //TODO
+ **/
+@Data
+@AllArgsConstructor
+public class Res<T> {
+    T res;
+}

+ 10 - 0
src/main/resources/tq.graphqls

@@ -0,0 +1,10 @@
+type TQ {
+    id: ID,
+    answer: String,
+    stem: String,
+    type: String
+}
+
+type Query{
+    tq(id: Int!): TQ
+}

+ 17 - 17
src/test/java/com/es/demo/DemoApplicationTests.java

@@ -31,22 +31,22 @@ import org.springframework.web.context.WebApplicationContext;
 @ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
 @SpringBootTest
 class DemoApplicationTests {
-    private MockMvc mockMvc;
-    @BeforeEach
-    public void setUp(WebApplicationContext webApplicationContext,
-                      RestDocumentationContextProvider restDocumentation) {
-        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
-                .apply(documentationConfiguration(restDocumentation))
-                .build();
-    }
-    @Test
-    void contextLoads() throws Exception{
-        this.mockMvc.perform(get("/api/tq?page=1&size=2").accept(MediaType.APPLICATION_JSON))
-                .andExpect(status().isOk())
-                .andDo(document("index",requestParameters(
-                        parameterWithName("page").description("从1开始的页号"),
-                        parameterWithName("size").description("每页大小")
-                        )));
-    }
+//    private MockMvc mockMvc;
+//    @BeforeEach
+//    public void setUp(WebApplicationContext webApplicationContext,
+//                      RestDocumentationContextProvider restDocumentation) {
+//        this.mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
+//                .apply(documentationConfiguration(restDocumentation))
+//                .build();
+//    }
+//    @Test
+//    void contextLoads() throws Exception{
+//        this.mockMvc.perform(get("/api/tq?page=1&size=2").accept(MediaType.APPLICATION_JSON))
+//                .andExpect(status().isOk())
+//                .andDo(document("index",requestParameters(
+//                        parameterWithName("page").description("从1开始的页号"),
+//                        parameterWithName("size").description("每页大小")
+//                        )));
+//    }
 
 }