add search function, still not working as intended tho

HuyDuong
HuyDuong 5 months ago
parent d7485f796f
commit c88cfe904e
  1. 4
      BE/src/main/java/org/example/controllers/PostController.java
  2. 26
      BE/src/main/java/org/example/controllers/SearchController.java
  3. 1
      BE/src/main/java/org/example/models/Post.java
  4. 1
      BE/src/main/java/org/example/models/UserPostResult.java
  5. 27
      BE/src/main/java/org/example/objects/SearchDTO.java
  6. 7
      BE/src/main/java/org/example/repositories/PostRepository.java
  7. 5
      BE/src/main/java/org/example/services/PostService.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 {

@ -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<List<SearchDTO>> searchRelatedTopics(@RequestParam String key){
List<SearchDTO> searchResult = postService.searchRelatedTopics(key);
return new ResponseEntity<>(searchResult, HttpStatus.OK);
}
}

@ -16,7 +16,6 @@ import java.util.UUID;
@Getter
@Node
public class Post {
@Id
@GeneratedValue
private Long id;

@ -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;
}
}

@ -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<Post,Long> {
")\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<Post,Long> {
@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<SearchDTO> search(@Param("input") String input);
}

@ -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<SearchDTO> searchRelatedTopics(String input){
return postRepository.search(input);
}
}

Loading…
Cancel
Save