From 5af08680216a12db4620535f62ca8d5c4bed8b4d Mon Sep 17 00:00:00 2001 From: Katerina881 Date: Tue, 18 Apr 2023 13:02:39 +0400 Subject: [PATCH] Lab5: Finished --- .../java/np/something/DTO/CommentDto.java | 23 +++ src/main/java/np/something/DTO/PostDto.java | 12 +- src/main/java/np/something/mvc/Admin.java | 61 ++++++++ src/main/java/np/something/mvc/Comments.java | 2 +- src/main/java/np/something/mvc/Customers.java | 3 +- src/main/java/np/something/mvc/Feed.java | 47 +++++- src/main/java/np/something/mvc/Posts.java | 2 +- .../repositories/CommentRepository.java | 6 + .../repositories/PostRepository.java | 7 + .../np/something/services/CommentService.java | 5 + .../np/something/services/PostService.java | 5 + src/main/resources/templates/admin.html | 103 +++++++++++++ src/main/resources/templates/customers.html | 11 +- src/main/resources/templates/default.html | 5 +- src/main/resources/templates/feed.html | 145 ++++++++++++++++++ 15 files changed, 424 insertions(+), 13 deletions(-) create mode 100644 src/main/java/np/something/mvc/Admin.java create mode 100644 src/main/resources/templates/admin.html diff --git a/src/main/java/np/something/DTO/CommentDto.java b/src/main/java/np/something/DTO/CommentDto.java index a239ae7..82016da 100644 --- a/src/main/java/np/something/DTO/CommentDto.java +++ b/src/main/java/np/something/DTO/CommentDto.java @@ -49,6 +49,10 @@ public class CommentDto { return customerId; } + public void setCustomerId(long customerId) { + this.customerId = customerId; + } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public String getCustomerName() { return customerName; @@ -58,6 +62,10 @@ public class CommentDto { return postId; } + public void setPostId(long postId) { + this.postId = postId; + } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public String getPostTitle() {return postTitle;} @@ -75,4 +83,19 @@ public class CommentDto { public String getCreateDate() { return createDate; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + CommentDto that = (CommentDto) o; + + return id == that.id; + } + + @Override + public int hashCode() { + return (int) (id ^ (id >>> 32)); + } } diff --git a/src/main/java/np/something/DTO/PostDto.java b/src/main/java/np/something/DTO/PostDto.java index 56612b9..9c6d146 100644 --- a/src/main/java/np/something/DTO/PostDto.java +++ b/src/main/java/np/something/DTO/PostDto.java @@ -3,8 +3,8 @@ package np.something.DTO; import com.fasterxml.jackson.annotation.JsonProperty; import np.something.model.Post; -import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; import java.util.List; public class PostDto { @@ -13,7 +13,7 @@ public class PostDto { public String content; public String customerName; public long customerId; - public List comments; + public ArrayList comments; public String createDate; @@ -27,7 +27,7 @@ public class PostDto { this.content = post.getContent(); this.customerName = post.getCustomer().getUsername(); this.customerId = post.getCustomer().getId(); - this.comments = post.getComments().stream().map(CommentDto::new).toList(); + this.comments = new ArrayList<>(post.getComments().stream().map(CommentDto::new).toList()); this.createDate = post.getCreateDate().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")); } @@ -48,7 +48,7 @@ public class PostDto { return content; } - public void setContent() { + public void setContent(String content) { this.content = content; } @@ -66,6 +66,10 @@ public class PostDto { return customerId; } + public void setCustomerId(long customerId) { + this.customerId = customerId; + } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public String getCreateDate() { return createDate; diff --git a/src/main/java/np/something/mvc/Admin.java b/src/main/java/np/something/mvc/Admin.java new file mode 100644 index 0000000..c51dcd8 --- /dev/null +++ b/src/main/java/np/something/mvc/Admin.java @@ -0,0 +1,61 @@ +package np.something.mvc; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; +import jakarta.validation.Valid; +import np.something.DTO.CustomerDto; +import np.something.services.CustomerService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.*; + +@Controller +@RequestMapping("/admin") +public class Admin { + private final CustomerService customerService; + + public Admin(CustomerService customerService) { + this.customerService = customerService; + } + + @GetMapping(value = { "/", "/{id}" }) + public String getCustomers(@PathVariable(required = false) Long id, HttpServletRequest request, HttpSession session, Model model) { + model.addAttribute("request", request); + model.addAttribute("session", session); + if (id == null || id <= 0) { + model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList()); + return "admin"; + } else { + return "redirect:/customers/" + id; + } + } + + @PostMapping("/delete/{id}") + public String deleteCustomer(@PathVariable Long id, HttpSession session) { + session.setAttribute("currentCustomerId", -1); + customerService.deleteCustomer(id); + return "redirect:/admin/"; + } + + @PostMapping(value = { "/", "/{id}"}) + public String manipulateCustomer(@PathVariable(required = false) Long id, @ModelAttribute @Valid CustomerDto customerDto, + HttpServletRequest request, HttpSession session, + BindingResult bindingResult, + Model model) { + model.addAttribute("request", request); + model.addAttribute("session", session); + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "/admin"; + } + + if (id == null || id <= 0) { + customerService.addCustomer(customerDto.username, customerDto.password); + } else { + customerService.updateCustomer(id, customerDto.username, customerDto.password); + } + + return "redirect:/admin/"; + } +} diff --git a/src/main/java/np/something/mvc/Comments.java b/src/main/java/np/something/mvc/Comments.java index d9acd3b..a53c4cf 100644 --- a/src/main/java/np/something/mvc/Comments.java +++ b/src/main/java/np/something/mvc/Comments.java @@ -50,6 +50,6 @@ public class Comments { @PostMapping("/delete/{id}") public String deleteComment(@PathVariable Long id) { commentService.deleteComment(id); - return "redirect:/feed/"; + return "redirect:/feed"; } } diff --git a/src/main/java/np/something/mvc/Customers.java b/src/main/java/np/something/mvc/Customers.java index 9821279..e06acf8 100644 --- a/src/main/java/np/something/mvc/Customers.java +++ b/src/main/java/np/something/mvc/Customers.java @@ -33,7 +33,8 @@ public class Customers { } @PostMapping("/delete/{id}") - public String deleteCustomer(@PathVariable Long id) { + public String deleteCustomer(@PathVariable Long id, HttpSession session) { + session.setAttribute("currentCustomerId", -1); customerService.deleteCustomer(id); return "redirect:/customers/"; } diff --git a/src/main/java/np/something/mvc/Feed.java b/src/main/java/np/something/mvc/Feed.java index cedf813..48988f8 100644 --- a/src/main/java/np/something/mvc/Feed.java +++ b/src/main/java/np/something/mvc/Feed.java @@ -2,28 +2,69 @@ package np.something.mvc; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpSession; +import np.something.DTO.CommentDto; +import np.something.DTO.CustomerDto; import np.something.DTO.PostDto; +import np.something.model.Post; +import np.something.services.CommentService; +import np.something.services.CustomerService; import np.something.services.PostService; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; @Controller @RequestMapping("/feed") public class Feed { private final PostService postService; + private final CustomerService customerService; - public Feed(PostService postService) { + private final CommentService commentService; + + public Feed(PostService postService, CustomerService customerService, CommentService commentService) { this.postService = postService; + this.customerService = customerService; + this.commentService = commentService; } @GetMapping - public String getPosts(HttpServletRequest request, HttpSession session, Model model) { + public String getPosts(@RequestParam(required = false) String search, HttpServletRequest request, HttpSession session, Model model) { model.addAttribute("request", request); model.addAttribute("session", session); - model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); + 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); + } + } + model.addAttribute("posts", posts.stream().map(PostDto::new).toList()); + } + model.addAttribute("customers", customerService.findAllCustomers().stream().map(CustomerDto::new).toList()); return "/feed"; } diff --git a/src/main/java/np/something/mvc/Posts.java b/src/main/java/np/something/mvc/Posts.java index f187f53..2d9892c 100644 --- a/src/main/java/np/something/mvc/Posts.java +++ b/src/main/java/np/something/mvc/Posts.java @@ -51,6 +51,6 @@ public class Posts { postService.updatePost(id, postDto.title, postDto.content); } - return "redirect:/feed/"; + return "redirect:/feed"; } } diff --git a/src/main/java/np/something/repositories/CommentRepository.java b/src/main/java/np/something/repositories/CommentRepository.java index aa5f822..cab8d44 100644 --- a/src/main/java/np/something/repositories/CommentRepository.java +++ b/src/main/java/np/something/repositories/CommentRepository.java @@ -2,6 +2,12 @@ package np.something.repositories; import np.something.model.Comment; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +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); } diff --git a/src/main/java/np/something/repositories/PostRepository.java b/src/main/java/np/something/repositories/PostRepository.java index 2786375..998809f 100644 --- a/src/main/java/np/something/repositories/PostRepository.java +++ b/src/main/java/np/something/repositories/PostRepository.java @@ -1,7 +1,14 @@ package np.something.repositories; +import np.something.model.Comment; import np.something.model.Post; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; + +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); } diff --git a/src/main/java/np/something/services/CommentService.java b/src/main/java/np/something/services/CommentService.java index a70240e..bff7e44 100644 --- a/src/main/java/np/something/services/CommentService.java +++ b/src/main/java/np/something/services/CommentService.java @@ -66,4 +66,9 @@ public class CommentService { public void deleteAllComments() { commentRepository.deleteAll(); } + + @jakarta.transaction.Transactional + public List searchComments(String tag) { + return commentRepository.searchComments(tag); + } } diff --git a/src/main/java/np/something/services/PostService.java b/src/main/java/np/something/services/PostService.java index 74c809c..d9e9d04 100644 --- a/src/main/java/np/something/services/PostService.java +++ b/src/main/java/np/something/services/PostService.java @@ -68,4 +68,9 @@ public class PostService { public void deleteAllPosts() { postRepository.deleteAll(); } + + @Transactional + public List searchPosts(String tag) { + return postRepository.searchPosts(tag); + } } diff --git a/src/main/resources/templates/admin.html b/src/main/resources/templates/admin.html new file mode 100644 index 0000000..5bc6896 --- /dev/null +++ b/src/main/resources/templates/admin.html @@ -0,0 +1,103 @@ + + + + + +
+
+
+
+

Профили

+
+
+
+
+ +
+
+
+ +

Список профилей

+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ +
+ + + +
+ +
+
+
+
+
+
+
+ + + + + +
+ + + + + \ No newline at end of file diff --git a/src/main/resources/templates/customers.html b/src/main/resources/templates/customers.html index f022039..5c06ce9 100644 --- a/src/main/resources/templates/customers.html +++ b/src/main/resources/templates/customers.html @@ -51,7 +51,7 @@
- + @@ -101,4 +101,13 @@ + + + \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index a9ca43b..663977e 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -6,6 +6,7 @@ + Социальная сеть @@ -13,8 +14,8 @@
+ + + + +
+ + + \ No newline at end of file