Lab4 сдана

This commit is contained in:
Данила Мочалов 2023-04-11 22:43:02 +04:00
parent 01e07b5568
commit 78a86fb68e
7 changed files with 54 additions and 1 deletions

Binary file not shown.

View File

@ -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){

View File

@ -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,

View File

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

View File

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

View File

@ -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)) {

View File

@ -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)) {