parent
d7485f796f
commit
7db94512a7
@ -0,0 +1,39 @@ |
||||
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); |
||||
} |
||||
} |
||||
|
@ -0,0 +1,43 @@ |
||||
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; |
||||
|
||||
@Relationship(type = "HAS_POST") |
||||
private List<Post> posts; |
||||
|
||||
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; |
||||
} |
||||
|
||||
public List<Post> getPosts() { |
||||
return posts; |
||||
} |
||||
|
||||
public void setPosts(List<Post> posts) { |
||||
this.posts = posts; |
||||
} |
||||
} |
@ -0,0 +1,31 @@ |
||||
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; |
||||
} |
||||
} |
@ -0,0 +1,22 @@ |
||||
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; |
||||
} |
||||
} |
@ -0,0 +1,61 @@ |
||||
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
|
||||
} |
@ -0,0 +1,51 @@ |
||||
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
|
||||
} |
@ -0,0 +1,16 @@ |
||||
package org.example.repositories; |
||||
|
||||
import org.example.models.Category; |
||||
import org.example.models.Post; |
||||
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> { |
||||
@Query("MATCH (c:Category)-[:HAS_POST]->(p:Post) WHERE c.name IN $categoryNames RETURN p") |
||||
List<Post> findPostsByCategoryNames(@Param("categoryNames") List<String> categoryNames); |
||||
} |
@ -1,25 +1,28 @@ |
||||
package org.example.repositories; |
||||
|
||||
import org.example.models.Category; |
||||
import org.example.models.Post; |
||||
import org.example.models.User; |
||||
import org.example.queryresults.PostQueryResult; |
||||
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; |
||||
import java.util.Optional; |
||||
|
||||
@Repository |
||||
public interface UserRepository extends Neo4jRepository<User, Long> { |
||||
Optional<User> findUserByUsername(String username); |
||||
User findUserBy(String username); |
||||
boolean existsByUsername(String username); |
||||
|
||||
|
||||
@Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " + |
||||
"RETURN EXISTS((user)-[:ENROLLED_IN]->(course))") |
||||
Boolean findEnrolmentStatus(String username, String identifier); |
||||
@Query("MATCH (u:User)-[:FRIEND]->(f:User)-[:LIKED|COMMENTED|SHARED]->(p:Post) WHERE u.username=$username RETURN p") |
||||
List<Post> findFriendPosts(@Param("username") String username); |
||||
|
||||
// @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " +
|
||||
// "CREATE (user)-[:ENROLLED_IN]->(course) RETURN user, course")
|
||||
// CourseEnrolmentQueryResult createEnrolmentRelationship(String username, String identifier);
|
||||
@Query("MATCH (u:User)-[:LIKED|COMMENTED|SHARED]->(p:Post)-[:HAS_POST]->(c:Category) WHERE u.username=$username RETURN DISTINCT c") |
||||
List<Category> findInteractedCategories(@Param("username") String username); |
||||
|
||||
|
||||
} |
||||
|
@ -0,0 +1,43 @@ |
||||
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; |
||||
|
||||
@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); |
||||
} |
||||
} |
@ -0,0 +1,39 @@ |
||||
package org.example.services; |
||||
|
||||
|
||||
import org.example.models.Category; |
||||
import org.example.models.Post; |
||||
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.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<>(); |
||||
recommendationPosts.addAll(postRepository.findTop10ByInteractions()); |
||||
List<Category> categories = userRepository.findInteractedCategories(username); |
||||
List<String> categoriesNames = categories.stream().map(Category::getName).collect(Collectors.toList()); |
||||
recommendationPosts.addAll(categoryRepository.findPostsByCategoryNames(categoriesNames)); |
||||
recommendationPosts.addAll(userRepository.findFriendPosts(username)); |
||||
return recommendationPosts.stream().distinct().collect(Collectors.toList()); |
||||
} |
||||
|
||||
} |
@ -1,4 +1,3 @@ |
||||
spring.neo4j.uri=neo4j://localhost:7687 |
||||
spring.neo4j.authentication.username=neo4j |
||||
spring.neo4j.authentication.password=12345678 |
||||
file_upload = C:/Users/P R O B O O K/Documents/Facebook/BE/images |
Loading…
Reference in new issue