parent
a836e4308f
commit
ac63c62ccb
@ -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; |
||||
} |
||||
} |
@ -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); |
||||
} |
@ -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…
Reference in new issue