Lab4 сдана
This commit is contained in:
parent
01e07b5568
commit
78a86fb68e
BIN
data.mv.db
BIN
data.mv.db
Binary file not shown.
@ -27,6 +27,13 @@ public class CommentController {
|
||||
.map(CommentDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/find")
|
||||
public List<PostDto> getFilteredComments(@RequestParam("text") String text) {
|
||||
return commentService.findFilteredComments(text).stream()
|
||||
.map(PostDto::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
|
||||
@PostMapping
|
||||
public CommentDto createComment(@RequestParam("text") String text, @RequestParam("ownerId") Long id, @RequestParam("postId") Long postId){
|
||||
|
@ -27,6 +27,13 @@ public class PostController {
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/find")
|
||||
public List<PostDto> getFilteredPosts(@RequestParam("text") String text) {
|
||||
return postService.findFilteredPosts(text).stream()
|
||||
.map(PostDto::new)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public PostDto createPost(
|
||||
@RequestParam("text") String text,
|
||||
|
@ -3,5 +3,8 @@ package com.webproglabs.lab1.lab34.repository;
|
||||
import com.webproglabs.lab1.lab34.model.Comment;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface CommentRepository extends JpaRepository<Comment, Long> {
|
||||
List<Comment> findByTextLike(String text);
|
||||
}
|
||||
|
@ -4,5 +4,8 @@ import com.webproglabs.lab1.lab34.model.Comment;
|
||||
import com.webproglabs.lab1.lab34.model.Post;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PostRepository extends JpaRepository<Post, Long> {
|
||||
List<Post> findByTextLike(String text);
|
||||
}
|
||||
|
@ -39,6 +39,21 @@ public class CommentService {
|
||||
return commentRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Post> findFilteredComments(String filter) {
|
||||
List<Post> postList = postRepository.findByTextLike("%" + filter + "%");
|
||||
List<Comment> commentList = commentRepository.findByTextLike("%" + filter + "%");
|
||||
List<Post> allPosts = postRepository.findAll();
|
||||
for(Post post : allPosts) {
|
||||
for (Comment comm : commentList) {
|
||||
if (post.getComments().contains(comm) && !(postList.contains(post))) {
|
||||
postList.add(post);
|
||||
}
|
||||
}
|
||||
}
|
||||
return postList;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Comment addComment(String text, Long profileId, Long postId) {
|
||||
if (!StringUtils.hasText(text)) {
|
||||
|
@ -3,6 +3,7 @@ package com.webproglabs.lab1.lab34.services;
|
||||
import com.webproglabs.lab1.lab34.model.Comment;
|
||||
import com.webproglabs.lab1.lab34.model.Post;
|
||||
import com.webproglabs.lab1.lab34.model.Profile;
|
||||
import com.webproglabs.lab1.lab34.repository.CommentRepository;
|
||||
import com.webproglabs.lab1.lab34.repository.PostRepository;
|
||||
import com.webproglabs.lab1.lab34.repository.ProfileRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
@ -16,10 +17,12 @@ import java.util.Optional;
|
||||
@Service
|
||||
public class PostService {
|
||||
private final PostRepository postRepository;
|
||||
private final CommentRepository commentRepository;
|
||||
private final ProfileRepository profileRepository;
|
||||
|
||||
public PostService(PostRepository postRepository, ProfileRepository profileRepository) {
|
||||
public PostService(PostRepository postRepository, CommentRepository commentRepository, ProfileRepository profileRepository) {
|
||||
this.postRepository = postRepository;
|
||||
this.commentRepository = commentRepository;
|
||||
this.profileRepository = profileRepository;
|
||||
}
|
||||
|
||||
@ -34,6 +37,21 @@ public class PostService {
|
||||
return postRepository.findAll();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Post> findFilteredPosts(String filter) {
|
||||
List<Post> postList = postRepository.findByTextLike("%" + filter + "%");
|
||||
List<Comment> commentList = commentRepository.findByTextLike("%" + filter + "%");
|
||||
List<Post> allPosts = postRepository.findAll();
|
||||
for(Post post : allPosts) {
|
||||
for (Comment comm : commentList) {
|
||||
if (post.getComments().contains(comm) && !(postList.contains(post))) {
|
||||
postList.add(post);
|
||||
}
|
||||
}
|
||||
}
|
||||
return postList;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Post addPost(String text, List<Comment> comments, Long authorId) {
|
||||
if (!StringUtils.hasText(text)) {
|
||||
|
Loading…
Reference in New Issue
Block a user