From c88cfe904e180c74d93742e7050d9547d8dba82f Mon Sep 17 00:00:00 2001 From: HuyDuong <64969059+Deamon227@users.noreply.github.com> Date: Wed, 3 Jul 2024 14:02:36 +0700 Subject: [PATCH] add search function, still not working as intended tho --- .../example/controllers/PostController.java | 4 +-- .../example/controllers/SearchController.java | 26 ++++++++++++++++++ BE/src/main/java/org/example/models/Post.java | 1 - .../org/example/models/UserPostResult.java | 1 - .../java/org/example/objects/SearchDTO.java | 27 +++++++++++++++++++ .../example/repositories/PostRepository.java | 7 +++++ .../org/example/services/PostService.java | 5 ++++ 7 files changed, 66 insertions(+), 5 deletions(-) create mode 100644 BE/src/main/java/org/example/controllers/SearchController.java delete mode 100644 BE/src/main/java/org/example/models/UserPostResult.java create mode 100644 BE/src/main/java/org/example/objects/SearchDTO.java diff --git a/BE/src/main/java/org/example/controllers/PostController.java b/BE/src/main/java/org/example/controllers/PostController.java index 463b7e1..7e855f2 100644 --- a/BE/src/main/java/org/example/controllers/PostController.java +++ b/BE/src/main/java/org/example/controllers/PostController.java @@ -57,9 +57,7 @@ public class PostController { } } @PostMapping - public Post createPost( - - @RequestParam("title") String title, + public Post createPost(@RequestParam("title") String title, @RequestParam("content") String content, @RequestParam(value = "imageFile", required = false) MultipartFile imageFile, @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) throws IOException { diff --git a/BE/src/main/java/org/example/controllers/SearchController.java b/BE/src/main/java/org/example/controllers/SearchController.java new file mode 100644 index 0000000..cb3b199 --- /dev/null +++ b/BE/src/main/java/org/example/controllers/SearchController.java @@ -0,0 +1,26 @@ +package org.example.controllers; + +import org.example.objects.SearchDTO; +import org.example.services.PostService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/search") +public class SearchController { + private final PostService postService; + + public SearchController(PostService postService) { + this.postService = postService; + } + + + @GetMapping("/search_result") + public ResponseEntity> searchRelatedTopics(@RequestParam String key){ + List searchResult = postService.searchRelatedTopics(key); + return new ResponseEntity<>(searchResult, HttpStatus.OK); + } +} diff --git a/BE/src/main/java/org/example/models/Post.java b/BE/src/main/java/org/example/models/Post.java index 9270ad2..e44b9c0 100644 --- a/BE/src/main/java/org/example/models/Post.java +++ b/BE/src/main/java/org/example/models/Post.java @@ -16,7 +16,6 @@ import java.util.UUID; @Getter @Node public class Post { - @Id @GeneratedValue private Long id; diff --git a/BE/src/main/java/org/example/models/UserPostResult.java b/BE/src/main/java/org/example/models/UserPostResult.java deleted file mode 100644 index 8b13789..0000000 --- a/BE/src/main/java/org/example/models/UserPostResult.java +++ /dev/null @@ -1 +0,0 @@ - diff --git a/BE/src/main/java/org/example/objects/SearchDTO.java b/BE/src/main/java/org/example/objects/SearchDTO.java new file mode 100644 index 0000000..6932664 --- /dev/null +++ b/BE/src/main/java/org/example/objects/SearchDTO.java @@ -0,0 +1,27 @@ +package org.example.objects; + +public class SearchDTO { + private String username; + private String title; + + public SearchDTO(String username, String title) { + this.username = username; + this.title = title; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } +} diff --git a/BE/src/main/java/org/example/repositories/PostRepository.java b/BE/src/main/java/org/example/repositories/PostRepository.java index 367d51f..1cf0a2a 100644 --- a/BE/src/main/java/org/example/repositories/PostRepository.java +++ b/BE/src/main/java/org/example/repositories/PostRepository.java @@ -3,6 +3,7 @@ package org.example.repositories; import org.example.controllers.UserController; import org.example.models.Image; import org.example.models.Post; +import org.example.objects.SearchDTO; import org.example.queryresults.PostQueryResult; import org.springframework.data.neo4j.repository.Neo4jRepository; import org.springframework.data.neo4j.repository.query.Query; @@ -48,6 +49,7 @@ public interface PostRepository extends Neo4jRepository { ")\n" + "RETURN p") Post updatePost(@Param("postId") Long postId, @Param("title") String title, @Param("content") String content, @Param("imageFile")MultipartFile imageFile, @Param("videoFile") MultipartFile videoFile); + @Query("MATCH (u:User {username: $username}), (p:Post) WHERE ID(p)=$postId CREATE (u)-[:LIKES]->(p)") void createLike(@Param("username") String username,@Param("postId") Long postId); @@ -57,4 +59,9 @@ public interface PostRepository extends Neo4jRepository { @Query("MATCH (user:User {username: $username}),(post:Post )"+"where ID(post)=$postId"+ " CREATE (user)-[:createPost ]->(post)"+ "RETURN user, post") PostQueryResult createPostRelationship(@Param("username") String username,@Param("postId") Long postId); + + @Query("OPTIONAL MATCH (p:Post), (u:User) " + + "WHERE toLower(p.title) CONTAINS toLower($input) OR toLower(u.username) CONTAINS toLower($input) " + + "RETURN p, u;") + List search(@Param("input") String input); } diff --git a/BE/src/main/java/org/example/services/PostService.java b/BE/src/main/java/org/example/services/PostService.java index d0eb34b..1632423 100644 --- a/BE/src/main/java/org/example/services/PostService.java +++ b/BE/src/main/java/org/example/services/PostService.java @@ -7,6 +7,7 @@ import org.example.models.Image; import org.example.models.Post; import org.example.models.User; import org.example.models.Video; +import org.example.objects.SearchDTO; import org.example.queryresults.PostQueryResult; import org.example.repositories.PostRepository; import org.example.repositories.UserRepository; @@ -185,4 +186,8 @@ public class PostService { return video; } + @Transactional + public List searchRelatedTopics(String input){ + return postRepository.search(input); + } }