From cbda0f455f3c902b92eb9b74d97336d2058c08c3 Mon Sep 17 00:00:00 2001 From: Safgerd Date: Tue, 16 May 2023 05:39:21 +0400 Subject: [PATCH] =?UTF-8?q?LabWork05:=20=D0=B4=D0=BE=D0=BF=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B4=D0=B0=D0=BD=D0=B8=D0=B5=205=20=D0=BB=D0=B0=D0=B1=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/ulstu/is/labwork/Lab4/mvc/Feed.java | 29 ++++----------- .../Lab4/repositories/CommentRepository.java | 4 ++- .../Lab4/repositories/PostRepository.java | 10 ++++-- .../labwork/Lab4/services/CommentService.java | 9 ++--- .../is/labwork/Lab4/services/PostService.java | 35 +++++++++++++++++-- src/main/resources/templates/default.html | 4 +-- 6 files changed, 56 insertions(+), 35 deletions(-) diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java index 2698c68..36d731d 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/mvc/Feed.java @@ -29,33 +29,18 @@ public class Feed { } @GetMapping - public String getPosts(@RequestParam(required = false) String search, HttpServletRequest request, HttpSession session, Model model) { + public String getPosts(@RequestParam(required = false) String search, + HttpServletRequest request, + HttpSession session, + @RequestParam(defaultValue = "0") int page, + @RequestParam(defaultValue = "5") int size, + Model model) { model.addAttribute("request", request); model.addAttribute("session", session); if (search == null) { model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); } else { - var posts = new ArrayList<>(postService.searchPosts(search)); - var comments = commentService.searchComments(search); - for (var post: posts) { - post.getComments().clear(); - } - for (var comment: comments) { - boolean found = false; - for (var post: posts) { - if (Objects.equals(comment.getPost().getId(), post.getId())) { - post.getComments().add(comment); - found = true; - break; - } - } - if (!found) { - var newPost = comment.getPost(); - newPost.getComments().clear(); - newPost.getComments().add(comment); - posts.add(newPost); - } - } + var posts = postService.searchPosts(page, size, search); model.addAttribute("posts", posts.stream().map(PostDto::new).toList()); } model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList()); diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/CommentRepository.java b/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/CommentRepository.java index 701ebeb..5198093 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/CommentRepository.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/CommentRepository.java @@ -1,5 +1,7 @@ package ru.ulstu.is.labwork.Lab4.repositories; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import ru.ulstu.is.labwork.Lab4.model.Comment; import org.springframework.data.jpa.repository.Query; @@ -11,5 +13,5 @@ import java.util.List; public interface CommentRepository extends JpaRepository { @Query("SELECT DISTINCT c FROM Comment c WHERE c.content LIKE %:tag%") - List searchComments(@Param("tag") String tag); + Page searchComments(@Param("tag") String tag, Pageable pageable); } \ No newline at end of file diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/PostRepository.java b/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/PostRepository.java index dac2360..9fd26c0 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/PostRepository.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/repositories/PostRepository.java @@ -1,5 +1,7 @@ package ru.ulstu.is.labwork.Lab4.repositories; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import ru.ulstu.is.labwork.Lab4.model.Post; @@ -7,9 +9,11 @@ import org.springframework.data.repository.query.Param; import java.util.List; -import java.util.List; - public interface PostRepository extends JpaRepository { + //@Query("SELECT DISTINCT p FROM Post p WHERE p.title LIKE %:tag% OR p.content LIKE %:tag%") + //List searchPosts(@Param("tag") String tag); @Query("SELECT DISTINCT p FROM Post p WHERE p.title LIKE %:tag% OR p.content LIKE %:tag%") - List searchPosts(@Param("tag") String tag); + Page searchPosts(@Param("tag") String tag, Pageable pageable); + + //Page findDistinctByContentOrTitleLikeIgnoreCase } diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/services/CommentService.java b/src/main/java/ru/ulstu/is/labwork/Lab4/services/CommentService.java index 2814a2a..62c7bdc 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/services/CommentService.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/services/CommentService.java @@ -1,6 +1,7 @@ package ru.ulstu.is.labwork.Lab4.services; import jakarta.persistence.EntityNotFoundException; import jakarta.transaction.Transactional; +import org.springframework.data.domain.PageRequest; import ru.ulstu.is.labwork.Lab4.model.Comment; import ru.ulstu.is.labwork.Lab4.model.Customer; import ru.ulstu.is.labwork.Lab4.model.Post; @@ -20,10 +21,10 @@ public class CommentService { this.commentRepository = commentRepository; } - @jakarta.transaction.Transactional - public List searchComments(String tag) { - return commentRepository.searchComments(tag); - } + //@jakarta.transaction.Transactional + //public List searchComments(String tag) { + // return commentRepository.searchComments(tag, PageRequest.of(0,10)).stream().toList(); + //} @Transactional diff --git a/src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java b/src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java index 5b05229..b343497 100644 --- a/src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java +++ b/src/main/java/ru/ulstu/is/labwork/Lab4/services/PostService.java @@ -2,6 +2,9 @@ package ru.ulstu.is.labwork.Lab4.services; import jakarta.persistence.EntityNotFoundException; import jakarta.transaction.Transactional; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import ru.ulstu.is.labwork.Lab4.DTO.PostDto; import ru.ulstu.is.labwork.Lab4.model.Comment; import ru.ulstu.is.labwork.Lab4.model.Customer; import ru.ulstu.is.labwork.Lab4.model.Post; @@ -10,19 +13,45 @@ import org.springframework.util.StringUtils; import ru.ulstu.is.labwork.Lab4.repositories.CommentRepository; import ru.ulstu.is.labwork.Lab4.repositories.PostRepository; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; @Service public class PostService { private final PostRepository postRepository; + private final CommentRepository commentRepository; - public PostService(PostRepository postRepository) { + public PostService(PostRepository postRepository, CommentRepository commentRepository) { this.postRepository = postRepository; + this.commentRepository = commentRepository; } @Transactional - public List searchPosts(String tag) { - return postRepository.searchPosts(tag); + public List searchPosts(int page, int size, String tag) { + //var posts = postRepository.searchPosts(tag); + var posts = new ArrayList<>(postRepository.searchPosts(tag, PageRequest.of(page, size)).getContent().stream().toList()); + var comments = new ArrayList<>(commentRepository.searchComments(tag, PageRequest.of(page, size)).getContent().stream().toList()); + for (var post: posts) { + post.getComments().clear(); + } + for (var comment: comments) { + boolean found = false; + for (var post: posts) { + if (Objects.equals(comment.getPost().getId(), post.getId())) { + post.getComments().add(comment); + found = true; + break; + } + } + if (!found) { + var newPost = comment.getPost(); + newPost.getComments().clear(); + newPost.getComments().add(comment); + posts.add(newPost); + } + } + return posts; } @Transactional diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index e3eaa1d..6ed2ced 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -7,13 +7,13 @@ - LabWork04 - Social Network + LabWork05 - Social Network
-

LabWork04 - Social Network

+

LabWork05 - Social Network