PhucLe
Le Phuc 4 months ago
parent a836e4308f
commit ac63c62ccb
  1. 29
      BE/src/main/java/org/example/controllers/SearchController.java
  2. 36
      BE/src/main/java/org/example/objects/SearchResultDTO.java
  3. 3
      BE/src/main/java/org/example/repositories/CategoryRepository.java
  4. 3
      BE/src/main/java/org/example/repositories/PostRepository.java
  5. 23
      BE/src/main/java/org/example/repositories/SearchRepository.java
  6. 2
      BE/src/main/java/org/example/services/RecommendationService.java
  7. 40
      BE/src/main/java/org/example/services/SearchService.java

@ -0,0 +1,29 @@
package org.example.controllers;
import ch.qos.logback.core.joran.sanity.Pair;
import org.example.models.Post;
import org.example.models.User;
import org.example.objects.SearchResultDTO;
import org.example.services.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/search")
public class SearchController {
@Autowired
private final SearchService searchService;
public SearchController(SearchService searchService) {
this.searchService = searchService;
}
@GetMapping("/get")
public List<Post> search(@RequestParam("condition") String condition) {
System.out.println(condition);
return searchService.searchResult(condition);
}
}

@ -0,0 +1,36 @@
package org.example.objects;
import org.example.models.Post;
import org.example.models.User;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.web.bind.annotation.GetMapping;
public class SearchResultDTO {
@Id
@GeneratedValue
private Long id;
private User user;
private Post post;
public SearchResultDTO(Post post, User user) {
this.user = user;
this.post = post;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
}

@ -20,4 +20,7 @@ public interface CategoryRepository extends Neo4jRepository<Category, Long> {
"OPTIONAL MATCH (p)-[:HAS_VIDEO]->(video:Video)\n" + "OPTIONAL MATCH (p)-[:HAS_VIDEO]->(video:Video)\n" +
"RETURN ID(p)") "RETURN ID(p)")
List<Long> findPostsByCategoryNames(@Param("categoryName") String categoryName); List<Long> findPostsByCategoryNames(@Param("categoryName") String categoryName);
@Query("MATCH (post:Post)-[HAS_CATEGORY]->(cate:Category) WHERE ID(post)=$postId return cate")
Category getCategory(@Param("postId") Long postId);
} }

@ -1,6 +1,7 @@
package org.example.repositories; package org.example.repositories;
import org.example.controllers.UserController; import org.example.controllers.UserController;
import org.example.models.Category;
import org.example.models.Image; import org.example.models.Image;
import org.example.models.Post; import org.example.models.Post;
import org.example.queryresults.PostQueryResult; import org.example.queryresults.PostQueryResult;
@ -28,6 +29,8 @@ public interface PostRepository extends Neo4jRepository<Post,Long> {
"return image.filename") "return image.filename")
String getImage(@Param("postId") Long postId); String getImage(@Param("postId") Long postId);
@Query("MATCH (p:Post ) where ID(p)=$postId \n" + @Query("MATCH (p:Post ) where ID(p)=$postId \n" +
"SET p.title = $title,\n" + "SET p.title = $title,\n" +
" p.content = $content\n" + " p.content = $content\n" +

@ -0,0 +1,23 @@
package org.example.repositories;
import ch.qos.logback.core.joran.sanity.Pair;
import org.example.models.Post;
import org.example.models.User;
import org.example.objects.SearchResultDTO;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Repository
public interface SearchRepository extends Neo4jRepository<Post,Long> {
@Query("MATCH (p:Post)<-[:createPost]-(u:User)\n" +
"WHERE p.title CONTAINS $condition OR p.content CONTAINS $condition OR u.username CONTAINS $condition \n" +
"RETURN p \n")
List<Post> search(@Param("condition") String condition);
}

@ -37,7 +37,7 @@ public class RecommendationService {
for (Post post : postsInteracts) { for (Post post : postsInteracts) {
post.setImgUrl(postRepository.getImage(post.getId())); post.setImgUrl(postRepository.getImage(post.getId()));
recommendationPosts.add(post); recommendationPosts.add(post);
} }
//tìm kiếm các bài post có chung chủ đề //tìm kiếm các bài post có chung chủ đề
List<String> categoriesNames = userRepository.findInteractedCategories(username); List<String> categoriesNames = userRepository.findInteractedCategories(username);
for (String categoryName : categoriesNames) { for (String categoryName : categoriesNames) {

@ -0,0 +1,40 @@
package org.example.services;
import ch.qos.logback.core.joran.sanity.Pair;
import org.example.models.Post;
import org.example.models.User;
import org.example.objects.SearchResultDTO;
import org.example.repositories.CategoryRepository;
import org.example.repositories.PostRepository;
import org.example.repositories.SearchRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class SearchService {
@Autowired
private final SearchRepository searchRepository;
@Autowired
private final PostRepository postRepository;
@Autowired
private final CategoryRepository categoryRepository;
public SearchService(SearchRepository searchRepository, PostRepository postRepository,CategoryRepository categoryRepository) {
this.searchRepository = searchRepository;
this.postRepository = postRepository;
this.categoryRepository = categoryRepository;
}
public List<Post> searchResult(String condition) {
List<Post> posts= searchRepository.search(condition);
for(Post post: posts) {
post.setImgUrl(postRepository.getImage(post.getId()));
post.setCategory(categoryRepository.getCategory(post.getId()));
}
return posts;
}
}
Loading…
Cancel
Save