add more info related to search function

HuyDuong
HuyDuong 5 months ago
parent c88cfe904e
commit 1b0f81d30a
  1. 5
      BE/src/main/java/org/example/controllers/SearchController.java
  2. 34
      BE/src/main/java/org/example/objects/SearchDTO.java
  3. 30
      BE/src/main/java/org/example/queryresults/SearchQueryResult.java
  4. 3
      BE/src/main/java/org/example/repositories/PostRepository.java
  5. 3
      BE/src/main/java/org/example/services/PostService.java

@ -1,6 +1,7 @@
package org.example.controllers; package org.example.controllers;
import org.example.objects.SearchDTO; import org.example.objects.SearchDTO;
import org.example.queryresults.SearchQueryResult;
import org.example.services.PostService; import org.example.services.PostService;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
@ -19,8 +20,8 @@ public class SearchController {
@GetMapping("/search_result") @GetMapping("/search_result")
public ResponseEntity<List<SearchDTO>> searchRelatedTopics(@RequestParam String key){ public ResponseEntity<List<SearchQueryResult>> searchRelatedTopics(@RequestParam String key){
List<SearchDTO> searchResult = postService.searchRelatedTopics(key); List<SearchQueryResult> searchResult = postService.searchRelatedTopics(key);
return new ResponseEntity<>(searchResult, HttpStatus.OK); return new ResponseEntity<>(searchResult, HttpStatus.OK);
} }
} }

@ -1,27 +1,35 @@
package org.example.objects; package org.example.objects;
import org.example.models.Post;
import org.example.models.User;
import java.util.List;
public class SearchDTO { public class SearchDTO {
private String username; private List<Post> posts;
private String title; private List<User> users;
public SearchDTO() {
}
public SearchDTO(String username, String title) { public SearchDTO(List<Post> posts, List<User> users) {
this.username = username; this.posts = posts;
this.title = title; this.users = users;
} }
public String getUsername() { public List<Post> getPosts() {
return username; return posts;
} }
public void setUsername(String username) { public void setPosts(List<Post> posts) {
this.username = username; this.posts = posts;
} }
public String getTitle() { public List<User> getUsers() {
return title; return users;
} }
public void setTitle(String title) { public void setUsers(List<User> users) {
this.title = title; this.users = users;
} }
} }

@ -0,0 +1,30 @@
package org.example.queryresults;
import org.example.models.Post;
import org.example.models.User;
public class SearchQueryResult {
// private Post post;
private User user;
public SearchQueryResult(Post post, User user) {
// this.post = post;
this.user = user;
}
// public Post getPost() {
// return post;
// }
//
// public void setPost(Post post) {
// this.post = post;
// }
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}

@ -5,6 +5,7 @@ import org.example.models.Image;
import org.example.models.Post; import org.example.models.Post;
import org.example.objects.SearchDTO; import org.example.objects.SearchDTO;
import org.example.queryresults.PostQueryResult; import org.example.queryresults.PostQueryResult;
import org.example.queryresults.SearchQueryResult;
import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query; import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;
@ -63,5 +64,5 @@ public interface PostRepository extends Neo4jRepository<Post,Long> {
@Query("OPTIONAL MATCH (p:Post), (u:User) " + @Query("OPTIONAL MATCH (p:Post), (u:User) " +
"WHERE toLower(p.title) CONTAINS toLower($input) OR toLower(u.username) CONTAINS toLower($input) " + "WHERE toLower(p.title) CONTAINS toLower($input) OR toLower(u.username) CONTAINS toLower($input) " +
"RETURN p, u;") "RETURN p, u;")
List<SearchDTO> search(@Param("input") String input); List<SearchQueryResult> search(@Param("input") String input);
} }

@ -9,6 +9,7 @@ import org.example.models.User;
import org.example.models.Video; import org.example.models.Video;
import org.example.objects.SearchDTO; import org.example.objects.SearchDTO;
import org.example.queryresults.PostQueryResult; import org.example.queryresults.PostQueryResult;
import org.example.queryresults.SearchQueryResult;
import org.example.repositories.PostRepository; import org.example.repositories.PostRepository;
import org.example.repositories.UserRepository; import org.example.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@ -187,7 +188,7 @@ public class PostService {
} }
@Transactional @Transactional
public List<SearchDTO> searchRelatedTopics(String input){ public List<SearchQueryResult> searchRelatedTopics(String input){
return postRepository.search(input); return postRepository.search(input);
} }
} }

Loading…
Cancel
Save