Compare commits

..

2 Commits

  1. 25
      BE/src/main/java/org/example/controllers/CategoryController.java
  2. 39
      BE/src/main/java/org/example/controllers/InteractionController.java
  3. 41
      BE/src/main/java/org/example/controllers/PostController.java
  4. 30
      BE/src/main/java/org/example/controllers/SearchController.java
  5. 7
      BE/src/main/java/org/example/controllers/UserController.java
  6. 34
      BE/src/main/java/org/example/models/Category.java
  7. 25
      BE/src/main/java/org/example/models/Post.java
  8. 18
      BE/src/main/java/org/example/models/User.java
  9. 1
      BE/src/main/java/org/example/models/UserPostResult.java
  10. 31
      BE/src/main/java/org/example/objects/CommentDTO.java
  11. 21
      BE/src/main/java/org/example/objects/FriendDTO.java
  12. 22
      BE/src/main/java/org/example/objects/InteractionsDTO.java
  13. 14
      BE/src/main/java/org/example/objects/PostDTO.java
  14. 39
      BE/src/main/java/org/example/objects/PostWithMedia.java
  15. 35
      BE/src/main/java/org/example/objects/SearchDTO.java
  16. 36
      BE/src/main/java/org/example/objects/SearchResultDTO.java
  17. 30
      BE/src/main/java/org/example/queryresults/SearchQueryResult.java
  18. 61
      BE/src/main/java/org/example/realtionship/CommentRelationship.java
  19. 51
      BE/src/main/java/org/example/realtionship/ShareRelationship.java
  20. 26
      BE/src/main/java/org/example/repositories/CategoryRepository.java
  21. 54
      BE/src/main/java/org/example/repositories/PostRepository.java
  22. 23
      BE/src/main/java/org/example/repositories/SearchRepository.java
  23. 46
      BE/src/main/java/org/example/repositories/UserRepository.java
  24. 23
      BE/src/main/java/org/example/services/CategoryService.java
  25. 45
      BE/src/main/java/org/example/services/InteractService.java
  26. 37
      BE/src/main/java/org/example/services/PostService.java
  27. 67
      BE/src/main/java/org/example/services/RecommendationService.java
  28. 40
      BE/src/main/java/org/example/services/SearchService.java
  29. 8
      BE/src/main/java/org/example/services/UserService.java
  30. 1
      BE/src/main/resources/application.properties

@ -1,25 +0,0 @@
package org.example.controllers;
import org.example.models.Category;
import org.example.services.CategoryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/category")
public class CategoryController {
private final CategoryService categoryService;
public CategoryController(CategoryService categoryService) {
this.categoryService = categoryService;
}
@GetMapping("/getAllCategories")
public List<Category> getAllCategories(){
return categoryService.getAllCategories();
}
}

@ -1,39 +0,0 @@
package org.example.controllers;
import org.example.objects.CommentDTO;
import org.example.objects.InteractionsDTO;
import org.example.services.InteractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/interactions")
public class InteractionController {
private final InteractService interactionService;
@Autowired
public InteractionController(InteractService interactionService) {
this.interactionService = interactionService;
}
@PostMapping("/like")
public ResponseEntity<Void> likePost(@RequestBody InteractionsDTO interactionsDTO) {
interactionService.toggleLike(interactionsDTO.getUsername(), interactionsDTO.getPostId());
return new ResponseEntity<>(HttpStatus.OK);
}
@PostMapping("/comment")
public ResponseEntity<Void> commentPost(@RequestBody CommentDTO interactionsDTO) {
interactionService.commentPost(interactionsDTO.getUsername(), interactionsDTO.getPostId(), interactionsDTO.getContent());
return new ResponseEntity<>(HttpStatus.OK);
}
@PostMapping("/share")
public ResponseEntity<Void> sharePost(@RequestBody InteractionsDTO interactionsDTO) {
interactionService.sharePost(interactionsDTO.getUsername(), interactionsDTO.getPostId());
return new ResponseEntity<>(HttpStatus.OK);
}
}

@ -1,15 +1,14 @@
package org.example.controllers; package org.example.controllers;
//import org.example.models.Author; //import org.example.models.Author;
import org.example.models.*; import org.example.models.Image;
import org.example.models.LikeRequest;
import org.example.models.Post;
import org.example.models.User;
import org.example.objects.PostDTO; import org.example.objects.PostDTO;
import org.example.objects.PostWithMedia;
import org.example.repositories.CategoryRepository;
import org.example.repositories.PostRepository; import org.example.repositories.PostRepository;
import org.example.services.PostService; import org.example.services.PostService;
import org.example.services.RecommendationService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -24,15 +23,11 @@ public class PostController {
private final PostService postService; private final PostService postService;
private final PostRepository postRepository; private final PostRepository postRepository;
private final RecommendationService recommendationService;
private final CategoryRepository categoryRepository;
@Autowired @Autowired
public PostController(PostService postService, PostRepository postRepository, RecommendationService recommendationService, CategoryRepository categoryRepository) { public PostController(PostService postService, PostRepository postRepository) {
this.postService = postService; this.postService = postService;
this.postRepository = postRepository; this.postRepository = postRepository;
this.recommendationService = recommendationService;
this.categoryRepository = categoryRepository;
} }
@GetMapping("/getAllPosts") @GetMapping("/getAllPosts")
@ -47,23 +42,27 @@ public class PostController {
@PostMapping("/edit/{id}") @PostMapping("/edit/{id}")
public Post updatePost(@PathVariable Long id,@RequestBody PostDTO postDTO) throws IOException { public Post updatePost(@PathVariable Long id,@RequestBody PostDTO postDTO) throws IOException {
return postService.editPost(id, postDTO.getTitle(), postDTO.getContent(),postDTO.getCategory(), postDTO.getImageFile(), postDTO.getVideoFile()); return postService.editPost(id, postDTO.getTitle(), postDTO.getContent(), postDTO.getImageFile(), postDTO.getVideoFile());
} }
@PostMapping @PostMapping("/like")
public Post createPost( public ResponseEntity<String> handleLikeAction(@RequestBody LikeRequest request){
try{
System.out.println(request.getUsername()+" "+request.getPostId());
postService.toggleLike(request.getUsername(),request.getPostId());
return ResponseEntity.ok("Success");
}
catch(Exception e){
@RequestParam("title") String title, return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error:"+e.getMessage());
}
}
@PostMapping
public Post createPost(@RequestParam("title") String title,
@RequestParam("content") String content, @RequestParam("content") String content,
@RequestParam("category") Category category,
@RequestParam(value = "imageFile", required = false) MultipartFile imageFile, @RequestParam(value = "imageFile", required = false) MultipartFile imageFile,
@RequestParam(value = "videoFile", required = false) MultipartFile videoFile) throws IOException { @RequestParam(value = "videoFile", required = false) MultipartFile videoFile) throws IOException {
return postService.createPost(title, content,category, imageFile, videoFile); return postService.createPost(title, content, imageFile, videoFile);
} }
@GetMapping("/recommend/{username}")
public List<Post> getRecommendationPostByUsername(@PathVariable String username){
List<Post> recommend= recommendationService.getRecommendationPosts(username);
return recommend;
}
} }

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

@ -1,7 +1,6 @@
package org.example.controllers; package org.example.controllers;
import org.example.models.User; import org.example.models.User;
import org.example.objects.FriendDTO;
import org.example.objects.UserDTO; import org.example.objects.UserDTO;
import org.example.repositories.UserRepository; import org.example.repositories.UserRepository;
import org.example.requests.CreateUserRequest; import org.example.requests.CreateUserRequest;
@ -37,10 +36,4 @@ public class UserController {
return new ResponseEntity<>(responseUser, HttpStatus.CREATED); return new ResponseEntity<>(responseUser, HttpStatus.CREATED);
} }
@PostMapping("/addFriend")
public ResponseEntity<String> addFriend(@RequestBody FriendDTO friendDTO){
userService.addFriend(friendDTO.getUsername1(),friendDTO.getUsername2());
System.out.println(friendDTO.getUsername1());
return new ResponseEntity<>("Friend added", HttpStatus.CREATED);
}
} }

@ -1,34 +0,0 @@
package org.example.models;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
import java.util.List;
@Node
public class Category {
@Id
@GeneratedValue
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

@ -2,8 +2,6 @@ package org.example.models;
import lombok.Getter; import lombok.Getter;
import org.example.realtionship.CommentRelationship;
import org.example.realtionship.ShareRelationship;
import org.example.repositories.PostRepository; import org.example.repositories.PostRepository;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.GeneratedValue;
@ -18,7 +16,6 @@ import java.util.UUID;
@Getter @Getter
@Node @Node
public class Post { public class Post {
@Id @Id
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@ -26,33 +23,13 @@ public class Post {
private String content; private String content;
private String author; private String author;
private String imgUrl; private String imgUrl;
private int interactions;
@Relationship(type="HAS_CATEGORY", direction = Relationship.Direction.OUTGOING)
private Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public int getInteractions() {
return interactions;
}
public void setInteractions(int interactions) {
this.interactions = interactions;
}
public String getImgUrl() { public String getImgUrl() {
return imgUrl; return imgUrl;
} }
public void setImgUrl(String imgUrl) { public void setImgUrl(String imgUrl) {
this.imgUrl=imgUrl; this.imgUrl = imgUrl;
} }
public String getAuthor() { public String getAuthor() {

@ -3,14 +3,12 @@ package org.example.models;
import org.springframework.data.neo4j.core.schema.GeneratedValue; import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id; import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.Node; import org.springframework.data.neo4j.core.schema.Node;
import org.springframework.data.neo4j.core.schema.Relationship;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.List;
@Node @Node
public class User implements UserDetails { public class User implements UserDetails {
@ -22,22 +20,6 @@ public class User implements UserDetails {
private String password; private String password;
private String roles; private String roles;
@Relationship(type = "FRIEND")
private List<User> friends;
public void setId(Long id) {
this.id = id;
}
public List<User> getFriends() {
return friends;
}
public void setFriends(List<User> friends) {
this.friends = friends;
}
public User() { public User() {
} }

@ -1,31 +0,0 @@
package org.example.objects;
public class CommentDTO {
private String username;
private Long postId;
private String content;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getPostId() {
return postId;
}
public void setPostId(Long postId) {
this.postId = postId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}

@ -1,21 +0,0 @@
package org.example.objects;
public class FriendDTO {
private String username1,username2;
public String getUsername1() {
return username1;
}
public void setUsername1(String username1) {
this.username1 = username1;
}
public String getUsername2() {
return username2;
}
public void setUsername2(String username2) {
this.username2 = username2;
}
}

@ -1,22 +0,0 @@
package org.example.objects;
public class InteractionsDTO {
private String username;
private Long postId;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Long getPostId() {
return postId;
}
public void setPostId(Long postId) {
this.postId = postId;
}
}

@ -1,22 +1,10 @@
package org.example.objects; package org.example.objects;
import org.example.models.Category;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
public class PostDTO { public class PostDTO {
String title,content; String title,content;
Category category; MultipartFile imageFile,videoFile;
MultipartFile imageFile;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
MultipartFile videoFile;
public String getTitle() { public String getTitle() {
return title; return title;

@ -1,39 +0,0 @@
package org.example.objects;
import org.example.models.Image;
import org.example.models.Post;
import org.example.models.Video;
import org.springframework.data.neo4j.core.schema.Node;
import java.util.List;
public class PostWithMedia {
private Post post;
private List<Image> images;
private List<Video> videos;
// Getters and setters
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
public List<Image> getImages() {
return images;
}
public void setImages(List<Image> images) {
this.images = images;
}
public List<Video> getVideos() {
return videos;
}
public void setVideos(List<Video> videos) {
this.videos = videos;
}
}

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

@ -1,36 +0,0 @@
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,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;
}
}

@ -1,61 +0,0 @@
package org.example.realtionship;
import org.example.models.User;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.Id;
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
import org.springframework.data.neo4j.core.schema.TargetNode;
import org.springframework.web.bind.annotation.GetMapping;
import java.time.LocalDateTime;
@RelationshipProperties
public class CommentRelationship {
@Id
@GeneratedValue
private Long id;
private String content;
private LocalDateTime timestamp;
@TargetNode private User user;
public CommentRelationship(String content, LocalDateTime timestamp, User user) {
this.content = content;
this.timestamp = timestamp;
this.user = user;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public LocalDateTime getTimestamp() {
return timestamp;
}
public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
// Getters and Setters
}

@ -1,51 +0,0 @@
package org.example.realtionship;
import org.example.models.User;
import org.springframework.data.annotation.Id;
import org.springframework.data.neo4j.core.schema.GeneratedValue;
import org.springframework.data.neo4j.core.schema.RelationshipProperties;
import org.springframework.data.neo4j.core.schema.TargetNode;
import java.time.LocalDateTime;
@RelationshipProperties
public class ShareRelationship {
@Id
@GeneratedValue
private Long id;
private LocalDateTime timestamp;
@TargetNode
private User user;
public ShareRelationship(LocalDateTime timestamp, User user) {
this.timestamp = timestamp;
this.user = user;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public LocalDateTime getTimestamp() {
return timestamp;
}
public void setTimestamp(LocalDateTime timestamp) {
this.timestamp = timestamp;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
// Getters and Setters
}

@ -1,26 +0,0 @@
package org.example.repositories;
import org.example.models.Category;
import org.example.models.Post;
import org.example.objects.PostDTO;
import org.example.objects.PostWithMedia;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface CategoryRepository extends Neo4jRepository<Category, Long> {
Category findByName(String name);
@Query("MATCH (c:Category)<-[:HAS_CATEGORY]-(p:Post)\n" +
"WHERE c.name = $categoryName\n" +
"OPTIONAL MATCH (p)-[:HAS_IMAGE]->(image:Image)\n" +
"OPTIONAL MATCH (p)-[:HAS_VIDEO]->(video:Video)\n" +
"RETURN ID(p)")
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,10 +1,11 @@
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.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;
@ -29,8 +30,6 @@ 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" +
@ -52,51 +51,18 @@ public interface PostRepository extends Neo4jRepository<Post,Long> {
"RETURN p") "RETURN p")
Post updatePost(@Param("postId") Long postId, @Param("title") String title, @Param("content") String content, @Param("imageFile")MultipartFile imageFile, @Param("videoFile") MultipartFile videoFile); 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), (p:Post) WHERE u.username = $username AND ID(p) = $postId " + @Query("MATCH (u:User {username: $username}), (p:Post) WHERE ID(p)=$postId CREATE (u)-[:LIKES]->(p)")
"MERGE (u)-[:LIKED {timestamp: datetime()}]->(p) " + void createLike(@Param("username") String username,@Param("postId") Long postId);
"RETURN p")
Post likePost(@Param("username") String username, @Param("postId") Long postId);
@Query("MATCH (u:User)-[l:LIKED]->(p:Post) " +
"WHERE u.username=$username AND ID(p) = $postId " +
"DELETE l " +
"RETURN CASE WHEN COUNT(l) > 0 THEN true ELSE false END AS removed")
boolean toggleLike(@Param("username") String username, @Param("postId") Long postId);
@Query("MATCH (u:User), (p:Post) WHERE u.username=$username AND ID(p) = $postId " +
"MERGE (u)-[:COMMENTED {content: $content, timestamp: datetime()}]->(p) " +
"RETURN p")
Post commentPost(@Param("username") String username, @Param("postId") Long postId, @Param("content") String content);
@Query("MATCH (u:User), (p:Post) WHERE u.username=$username AND ID(p) = $postId " +
"MERGE (u)-[:SHARED {timestamp: datetime()}]->(p) " +
"RETURN p")
Post sharePost(@Param("username") String username, @Param("postId") Long postId);
@Query("MATCH (p:Post),(u:User)\n" + @Query("MATCH (u:User {username: $username})-[r:LIKES]->(p:Post ) WHERE ID(p)=$postId DELETE r")
"OPTIONAL MATCH (p)<-[l:LIKED]-(u)\n" + void deleteLike(@Param("username") String username, @Param("postId") Long postId);
"OPTIONAL MATCH (p)<-[c:COMMENTED]-(u)\n" +
"OPTIONAL MATCH (p)<-[s:SHARED]-(u)\n" +
"OPTIONAL MATCH (p)-[:HAS_IMAGE]->(image:Image)\n" +
"OPTIONAL MATCH (p)-[:HAS_VIDEO]->(video:Video)\n" +
"WITH p, \n" +
" COUNT(DISTINCT l) AS likeCount, \n" +
" COUNT(DISTINCT c) AS commentCount, \n" +
" COUNT(DISTINCT s) AS shareCount, \n" +
" COLLECT(DISTINCT image) AS images, \n" +
" COLLECT(DISTINCT video) AS videos\n" +
"RETURN p, images, videos\n" +
"ORDER BY (likeCount + commentCount + shareCount) DESC\n" +
"LIMIT 10\n")
List<Post> findTop10ByInteractions();
@Query("MATCH (user:User {username: $username}),(post:Post )"+"where ID(post)=$postId"+ " CREATE (user)-[:createPost ]->(post)"+ @Query("MATCH (user:User {username: $username}),(post:Post )"+"where ID(post)=$postId"+ " CREATE (user)-[:createPost ]->(post)"+
"RETURN user, post") "RETURN user, post")
PostQueryResult createPostRelationship(@Param("username") String username,@Param("postId") Long postId); PostQueryResult createPostRelationship(@Param("username") String username,@Param("postId") Long postId);
// @Query("match (p:Post)where id(p)=$postID\n" + @Query("OPTIONAL MATCH (p:Post), (u:User) " +
// "optional match (p)-[:HAS_IMAGE]->(image:Image)\n" + "WHERE toLower(p.title) CONTAINS toLower($input) OR toLower(u.username) CONTAINS toLower($input) " +
// "optional match (p)-[:HAS_VIDEO]->(video:Video)\n" + "RETURN p, u;")
// "return p,image") List<SearchQueryResult> search(@Param("input") String input);
// List<Post> fi
} }

@ -1,23 +0,0 @@
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);
}

@ -1,53 +1,25 @@
package org.example.repositories; package org.example.repositories;
import org.example.models.Category;
import org.example.models.Post;
import org.example.models.User; import org.example.models.User;
import org.example.objects.FriendDTO;
import org.example.queryresults.PostQueryResult; import org.example.queryresults.PostQueryResult;
import org.example.services.UserService;
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.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestBody;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Repository
public interface UserRepository extends Neo4jRepository<User, Long> { public interface UserRepository extends Neo4jRepository<User, Long> {
Optional<User> findUserByUsername(String username); Optional<User> findUserByUsername(String username);
User findUserBy(String username);
boolean existsByUsername(String username); boolean existsByUsername(String username);
@Query("MATCH (u:User {name:$username1}), (f:User {name:$username2})\n" +
"OPTIONAL MATCH (u)-[fr:Friend]->(f), (f)-[fr1:Friend]->(u)\n" + @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " +
"WITH u, f, collect(fr) as friendships1, collect(fr1) as friendships2\n" + "RETURN EXISTS((user)-[:ENROLLED_IN]->(course))")
"FOREACH (_ IN CASE WHEN size(friendships1) = 0 AND size(friendships2) = 0 THEN [1] ELSE [] END | \n" + Boolean findEnrolmentStatus(String username, String identifier);
" CREATE (u)-[:Friend]->(f) \n" +
" CREATE (f)-[:Friend]->(u))\n" + // @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " +
"FOREACH (_ IN CASE WHEN size(friendships1) > 0 AND size(friendships2) > 0 THEN [1] ELSE [] END | \n" + // "CREATE (user)-[:ENROLLED_IN]->(course) RETURN user, course")
" DELETE friendships1[0], friendships2[0])\n") // CourseEnrolmentQueryResult createEnrolmentRelationship(String username, String identifier);
void addFriend(@Param("username1") String username1, @Param("username2") String username2);
@Query("MATCH (u:User {username:$username})-[:Friend]->(f:User)\n" +
"match (f)-[:createPost]->(p:Post)RETURN ID(p)")
List<Long> findFriendPosts(@Param("username") String username);
@Query("MATCH (u:User {username: $username})-[r:LIKED|COMMENTED|SHARED]->(p:Post)\n" +
"OPTIONAL MATCH (p)<-[like:LIKED]-(:User)\n" +
"OPTIONAL MATCH (p)<-[comment:COMMENTED]-(:User)\n" +
"OPTIONAL MATCH (p)<-[share:SHARED]-(:User)\n" +
"OPTIONAL MATCH (p)-[:HAS_CATEGORY]->(c:Category)\n" +
"WITH p, COUNT(DISTINCT like) AS likeCount, COUNT(DISTINCT comment) AS commentCount, COUNT(DISTINCT share) AS shareCount, c.name AS categories \n" +
"WHERE likeCount > 0 OR commentCount > 0 OR shareCount > 0\n" +
"RETURN categories \n" +
"ORDER BY (likeCount + commentCount + shareCount) DESC")
List<String> findInteractedCategories(@Param("username") String username);
} }

@ -1,23 +0,0 @@
package org.example.services;
import org.example.models.Category;
import org.example.repositories.CategoryRepository;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
@Service
public class CategoryService {
private final CategoryRepository categoryRepository;
public CategoryService(CategoryRepository categoryRepository) {
this.categoryRepository = categoryRepository;
}
public List<Category> getAllCategories() {
List<Category> categories = categoryRepository.findAll();
return categories;
}
}

@ -1,45 +0,0 @@
package org.example.services;
import org.example.models.*;
import org.example.realtionship.CommentRelationship;
import org.example.realtionship.ShareRelationship;
import org.example.repositories.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
@Service
public class InteractService {
private final PostRepository postRepository;
private final UserRepository userRepository;
@Autowired
public InteractService(PostRepository postRepository, UserRepository userRepository) {
this.postRepository = postRepository;
this.userRepository = userRepository;
}
@Transactional
public void toggleLike(String username , Long postId) {
boolean removed = postRepository.toggleLike(username, postId);
if (!removed) {
postRepository.likePost(username, postId);
System.out.println("Liked");
}
}
@Transactional
public void commentPost(String username, Long postId, String content) {
postRepository.commentPost(username, postId, content);
}
@Transactional
public void sharePost(String username, Long postId) {
postRepository.sharePost(username, postId);
}
}

@ -3,8 +3,13 @@ package org.example.services;
//import org.example.models.Author; //import org.example.models.Author;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import org.example.controllers.UserController; import org.example.controllers.UserController;
import org.example.models.*; 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.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;
@ -20,7 +25,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.security.Principal; import java.security.Principal;
import java.time.LocalDateTime;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -34,7 +38,6 @@ public class PostService {
private final UserService userService; private final UserService userService;
private final UserRepository userRepository; private final UserRepository userRepository;
private String username; private String username;
private Category category;
@Autowired @Autowired
private Neo4jTemplate neo4jTemplate; private Neo4jTemplate neo4jTemplate;
@ -63,13 +66,27 @@ public class PostService {
return getAllPost; return getAllPost;
} }
@Transactional @Transactional
public Post createPost(String title, String content,Category category, MultipartFile imageFile, MultipartFile videoFile) throws IOException { public void toggleLike(String username,Long postId){
System.out.println("Service:"+username+postId);
boolean alreadyLiked = neo4jTemplate.count("MATCH (u:User {username: $username})-[r:LIKES]->(p:Post) WHERE ID(p)=$postId RETURN count(r) ",
Map.of("username", username, "postId", postId)) > 0;
if (alreadyLiked) {
postRepository.deleteLike(username,postId);
} else {
System.out.println("Like de");
postRepository.createLike(username,postId);
}
}
@Transactional
public Post createPost(String title, String content, MultipartFile imageFile, MultipartFile videoFile) throws IOException {
Post post = new Post(); Post post = new Post();
post.setTitle(title); post.setTitle(title);
post.setContent(content); post.setContent(content);
post.setCategory(category);
if (imageFile != null) { if (imageFile != null) {
Image image = saveImageFile(imageFile, "C:\\Users\\phucl\\OneDrive\\Desktop\\JAVA_INTER\\Facebook\\FE\\FE\\public\\picture"); Image image = saveImageFile(imageFile, "C:\\Users\\P R O B O O K\\Documents\\Phuc\\Facebook\\FE/FE/public/picture");
// post.getImages().add(image); // post.getImages().add(image);
post.setImages(image); post.setImages(image);
} }
@ -97,11 +114,11 @@ public class PostService {
} }
} }
@Transactional @Transactional
public Post editPost(Long postId, String title, String content,Category category, MultipartFile imageFile, MultipartFile videoFile) throws IOException { public Post editPost(Long postId, String title, String content, MultipartFile imageFile, MultipartFile videoFile) throws IOException {
Post post= postRepository.updatePost(postId,title,content,imageFile,videoFile); Post post= postRepository.updatePost(postId,title,content,imageFile,videoFile);
// Update the image file if provided // Update the image file if provided
if (imageFile != null) { if (imageFile != null) {
Image newImage = saveImageFile(imageFile, "C:\\Users\\phucl\\OneDrive\\Desktop\\JAVA_INTER\\Facebook\\FE\\FE\\public\\picture"); Image newImage = saveImageFile(imageFile, "C:\\Users\\P R O B O O K\\Documents\\Phuc\\Facebook\\FE/FE/public/picture");
post.setImages(newImage); post.setImages(newImage);
} }
@ -170,4 +187,8 @@ public class PostService {
return video; return video;
} }
@Transactional
public List<SearchQueryResult> searchRelatedTopics(String input){
return postRepository.search(input);
}
} }

@ -1,67 +0,0 @@
package org.example.services;
import org.example.models.Category;
import org.example.models.Post;
import org.example.objects.PostDTO;
import org.example.objects.PostWithMedia;
import org.example.repositories.CategoryRepository;
import org.example.repositories.PostRepository;
import org.example.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
@Service
public class RecommendationService {
private final PostRepository postRepository;
private final UserRepository userRepository;
private final CategoryRepository categoryRepository;
@Autowired
public RecommendationService(PostRepository postRepository, UserRepository userRepository, CategoryRepository categoryRepository) {
this.postRepository = postRepository;
this.userRepository = userRepository;
this.categoryRepository = categoryRepository;
}
public List<Post> getRecommendationPosts(String username) {
List<Post> recommendationPosts = new ArrayList<>();
//tim kiem bai viết phổ biến theo số lượng interact
List<Post> postsInteracts = new ArrayList<>();
postsInteracts=postRepository.findTop10ByInteractions();
for (Post post : postsInteracts) {
post.setImgUrl(postRepository.getImage(post.getId()));
recommendationPosts.add(post);
}
//tìm kiếm các bài post có chung chủ đề
List<String> categoriesNames = userRepository.findInteractedCategories(username);
for (String categoryName : categoriesNames) {
System.out.println(categoryName);
List<Long> IDposts = categoryRepository.findPostsByCategoryNames(categoryName);
for(Long IDpost : IDposts) {
System.out.println(IDpost);
Post post= postRepository.findById(IDpost).get();
if(!recommendationPosts.contains(post)) {
recommendationPosts.add(post);
post.setImgUrl(postRepository.getImage(IDpost));
}
}
}
List<Long> postsFriend;
postsFriend=userRepository.findFriendPosts(username);
for (Long postID : postsFriend) {
Post post = postRepository.findById(postID).get();
if(!recommendationPosts.contains(post)) {
post.setImgUrl(postRepository.getImage(post.getId()));
recommendationPosts.add(post);
}
}
return recommendationPosts.stream().distinct().collect(Collectors.toList());
}
}

@ -1,40 +0,0 @@
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;
}
}

@ -1,7 +1,6 @@
package org.example.services; package org.example.services;
import org.example.models.User; import org.example.models.User;
import org.example.repositories.PostRepository;
import org.example.repositories.UserRepository; import org.example.repositories.UserRepository;
import org.example.requests.CreateUserRequest; import org.example.requests.CreateUserRequest;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
@ -12,12 +11,10 @@ public class UserService {
private final UserRepository userRepository; private final UserRepository userRepository;
private final PasswordEncoder passwordEncoder; private final PasswordEncoder passwordEncoder;
private final PostRepository postRepository;
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, PostRepository postRepository) { public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder) {
this.userRepository = userRepository; this.userRepository = userRepository;
this.passwordEncoder = passwordEncoder; this.passwordEncoder = passwordEncoder;
this.postRepository = postRepository;
} }
public User createUser(CreateUserRequest request) { public User createUser(CreateUserRequest request) {
@ -36,7 +33,4 @@ public class UserService {
return user; return user;
} }
public void addFriend(String username1, String username2) {
userRepository.addFriend(username1,username2);
}
} }

@ -1,3 +1,4 @@
spring.neo4j.uri=neo4j://localhost:7687 spring.neo4j.uri=neo4j://localhost:7687
spring.neo4j.authentication.username=neo4j spring.neo4j.authentication.username=neo4j
spring.neo4j.authentication.password=12345678 spring.neo4j.authentication.password=12345678
file_upload = C:/Users/P R O B O O K/Documents/Facebook/BE/images
Loading…
Cancel
Save