Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
Le Phuc | ac63c62ccb | 4 months ago |
Le Phuc | a836e4308f | 5 months ago |
Le Phuc | 7db94512a7 | 5 months ago |
@ -0,0 +1,25 @@ |
|||||||
|
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(); |
||||||
|
} |
||||||
|
} |
@ -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,29 @@ |
|||||||
|
package org.example.controllers; |
||||||
|
|
||||||
|
|
||||||
|
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.services.SearchService; |
||||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||||
|
import org.springframework.web.bind.annotation.*; |
||||||
|
|
||||||
|
import java.util.List; |
||||||
|
|
||||||
|
@RestController |
||||||
|
@RequestMapping("/search") |
||||||
|
public class SearchController { |
||||||
|
@Autowired |
||||||
|
private final SearchService searchService; |
||||||
|
|
||||||
|
public SearchController(SearchService searchService) { |
||||||
|
this.searchService = searchService; |
||||||
|
} |
||||||
|
|
||||||
|
@GetMapping("/get") |
||||||
|
public List<Post> search(@RequestParam("condition") String condition) { |
||||||
|
System.out.println(condition); |
||||||
|
return searchService.searchResult(condition); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,34 @@ |
|||||||
|
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; |
||||||
|
} |
||||||
|
} |
@ -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,21 @@ |
|||||||
|
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; |
||||||
|
} |
||||||
|
} |
@ -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,39 @@ |
|||||||
|
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,36 @@ |
|||||||
|
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,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,26 @@ |
|||||||
|
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); |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
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,25 +1,53 @@ |
|||||||
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); |
|
||||||
User findUserBy(String username); |
|
||||||
boolean existsByUsername(String username); |
|
||||||
|
|
||||||
|
|
||||||
@Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " + |
Optional<User> findUserByUsername(String username); |
||||||
"RETURN EXISTS((user)-[:ENROLLED_IN]->(course))") |
boolean existsByUsername(String username); |
||||||
Boolean findEnrolmentStatus(String username, String identifier); |
|
||||||
|
|
||||||
// @Query("MATCH (user:User), (course:Course) WHERE user.username = $username AND course.identifier = $identifier " +
|
@Query("MATCH (u:User {name:$username1}), (f:User {name:$username2})\n" + |
||||||
// "CREATE (user)-[:ENROLLED_IN]->(course) RETURN user, course")
|
"OPTIONAL MATCH (u)-[fr:Friend]->(f), (f)-[fr1:Friend]->(u)\n" + |
||||||
// CourseEnrolmentQueryResult createEnrolmentRelationship(String username, String identifier);
|
"WITH u, f, collect(fr) as friendships1, collect(fr1) as friendships2\n" + |
||||||
|
"FOREACH (_ IN CASE WHEN size(friendships1) = 0 AND size(friendships2) = 0 THEN [1] ELSE [] END | \n" + |
||||||
|
" CREATE (u)-[:Friend]->(f) \n" + |
||||||
|
" CREATE (f)-[:Friend]->(u))\n" + |
||||||
|
"FOREACH (_ IN CASE WHEN size(friendships1) > 0 AND size(friendships2) > 0 THEN [1] ELSE [] END | \n" + |
||||||
|
" DELETE friendships1[0], friendships2[0])\n") |
||||||
|
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); |
||||||
|
|
||||||
|
|
||||||
} |
} |
||||||
|
@ -0,0 +1,23 @@ |
|||||||
|
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; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,45 @@ |
|||||||
|
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); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,67 @@ |
|||||||
|
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()); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,40 @@ |
|||||||
|
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,4 +1,3 @@ |
|||||||
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…
Reference in new issue