LabWork05: доп задание 5 лабы

This commit is contained in:
Safgerd 2023-05-16 05:39:21 +04:00
parent 60c2564229
commit cbda0f455f
6 changed files with 56 additions and 35 deletions

View File

@ -29,33 +29,18 @@ public class Feed {
} }
@GetMapping @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("request", request);
model.addAttribute("session", session); model.addAttribute("session", session);
if (search == null) { if (search == null) {
model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList());
} else { } else {
var posts = new ArrayList<>(postService.searchPosts(search)); var posts = postService.searchPosts(page, size, 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);
}
}
model.addAttribute("posts", posts.stream().map(PostDto::new).toList()); model.addAttribute("posts", posts.stream().map(PostDto::new).toList());
} }
model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList()); model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList());

View File

@ -1,5 +1,7 @@
package ru.ulstu.is.labwork.Lab4.repositories; 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.JpaRepository;
import ru.ulstu.is.labwork.Lab4.model.Comment; import ru.ulstu.is.labwork.Lab4.model.Comment;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
@ -11,5 +13,5 @@ import java.util.List;
public interface CommentRepository extends JpaRepository<Comment, Long> { public interface CommentRepository extends JpaRepository<Comment, Long> {
@Query("SELECT DISTINCT c FROM Comment c WHERE c.content LIKE %:tag%") @Query("SELECT DISTINCT c FROM Comment c WHERE c.content LIKE %:tag%")
List<Comment> searchComments(@Param("tag") String tag); Page<Comment> searchComments(@Param("tag") String tag, Pageable pageable);
} }

View File

@ -1,5 +1,7 @@
package ru.ulstu.is.labwork.Lab4.repositories; 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.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import ru.ulstu.is.labwork.Lab4.model.Post; 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;
import java.util.List;
public interface PostRepository extends JpaRepository<Post, Long> { public interface PostRepository extends JpaRepository<Post, Long> {
//@Query("SELECT DISTINCT p FROM Post p WHERE p.title LIKE %:tag% OR p.content LIKE %:tag%")
//List<Post> searchPosts(@Param("tag") String tag);
@Query("SELECT DISTINCT p FROM Post p WHERE p.title LIKE %:tag% OR p.content LIKE %:tag%") @Query("SELECT DISTINCT p FROM Post p WHERE p.title LIKE %:tag% OR p.content LIKE %:tag%")
List<Post> searchPosts(@Param("tag") String tag); Page<Post> searchPosts(@Param("tag") String tag, Pageable pageable);
//Page<Post> findDistinctByContentOrTitleLikeIgnoreCase
} }

View File

@ -1,6 +1,7 @@
package ru.ulstu.is.labwork.Lab4.services; package ru.ulstu.is.labwork.Lab4.services;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional; 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.Comment;
import ru.ulstu.is.labwork.Lab4.model.Customer; import ru.ulstu.is.labwork.Lab4.model.Customer;
import ru.ulstu.is.labwork.Lab4.model.Post; import ru.ulstu.is.labwork.Lab4.model.Post;
@ -20,10 +21,10 @@ public class CommentService {
this.commentRepository = commentRepository; this.commentRepository = commentRepository;
} }
@jakarta.transaction.Transactional //@jakarta.transaction.Transactional
public List<Comment> searchComments(String tag) { //public List<Comment> searchComments(String tag) {
return commentRepository.searchComments(tag); // return commentRepository.searchComments(tag, PageRequest.of(0,10)).stream().toList();
} //}
@Transactional @Transactional

View File

@ -2,6 +2,9 @@ package ru.ulstu.is.labwork.Lab4.services;
import jakarta.persistence.EntityNotFoundException; import jakarta.persistence.EntityNotFoundException;
import jakarta.transaction.Transactional; 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.Comment;
import ru.ulstu.is.labwork.Lab4.model.Customer; import ru.ulstu.is.labwork.Lab4.model.Customer;
import ru.ulstu.is.labwork.Lab4.model.Post; 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.CommentRepository;
import ru.ulstu.is.labwork.Lab4.repositories.PostRepository; import ru.ulstu.is.labwork.Lab4.repositories.PostRepository;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
@Service @Service
public class PostService { public class PostService {
private final PostRepository postRepository; private final PostRepository postRepository;
private final CommentRepository commentRepository;
public PostService(PostRepository postRepository) { public PostService(PostRepository postRepository, CommentRepository commentRepository) {
this.postRepository = postRepository; this.postRepository = postRepository;
this.commentRepository = commentRepository;
} }
@Transactional @Transactional
public List<Post> searchPosts(String tag) { public List<Post> searchPosts(int page, int size, String tag) {
return postRepository.searchPosts(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 @Transactional

View File

@ -7,13 +7,13 @@
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous"> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<link rel="stylesheet" href="https://unpkg.com/chota@latest"> <link rel="stylesheet" href="https://unpkg.com/chota@latest">
<title>LabWork04 - Social Network</title> <title>LabWork05 - Social Network</title>
</head> </head>
<body class="container"> <body class="container">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-OERcA2EqjJCMA+/3y+gxIOqMEjwtxJY7qPCqsdltbNJuaOe923+mo//f6V8Qbsw3" crossorigin="anonymous"></script>
<div> <div>
<div> <div>
<p class='text-center m-3 h3'>LabWork04 - Social Network</p> <p class='text-center m-3 h3'>LabWork05 - Social Network</p>
</div> </div>
<div class="nav row"> <div class="nav row">
<div class="nav-left col-4" th:with="activeLink=${request.requestURI}"> <div class="nav-left col-4" th:with="activeLink=${request.requestURI}">