From 263182db1d26265a1fd9a3b1eab97c1fc5c9bb8c Mon Sep 17 00:00:00 2001 From: 1yuee Date: Tue, 20 Jun 2023 13:00:30 +0400 Subject: [PATCH] comments & users start --- .../lab1/mvc/FeedMvcController.java | 45 +++++++++++++++++++ .../lab1/mvc/UserMvcController.java | 24 ++++++++++ .../lab1/mvc/UserSignupMvcController.java | 9 +++- .../lab1/services/CommentService.java | 15 ------- .../lab1/services/PostService.java | 15 ------- .../resources/templates/commentModal.html | 32 +++++++++++++ .../resources/templates/editCommentModal.html | 30 +++++++++++++ src/main/resources/templates/feedPosts.html | 6 +-- 8 files changed, 141 insertions(+), 35 deletions(-) create mode 100644 src/main/java/com/webproglabs/lab1/mvc/UserMvcController.java create mode 100644 src/main/resources/templates/commentModal.html create mode 100644 src/main/resources/templates/editCommentModal.html diff --git a/src/main/java/com/webproglabs/lab1/mvc/FeedMvcController.java b/src/main/java/com/webproglabs/lab1/mvc/FeedMvcController.java index 2dcb327..5a832f4 100644 --- a/src/main/java/com/webproglabs/lab1/mvc/FeedMvcController.java +++ b/src/main/java/com/webproglabs/lab1/mvc/FeedMvcController.java @@ -1,5 +1,6 @@ package com.webproglabs.lab1.mvc; +import com.webproglabs.lab1.dto.CommentDto; import com.webproglabs.lab1.dto.PostDto; import com.webproglabs.lab1.dto.TopicDto; import com.webproglabs.lab1.dto.UserDto; @@ -94,4 +95,48 @@ public class FeedMvcController { return "redirect:/feed/" + topicId.toString(); } + @GetMapping(value = {"commentModal/{topicId}/{postId}"}) + public String getCommentModal(@PathVariable Long topicId,@PathVariable Long postId, Model model) { + model.addAttribute("selectedPost", new PostDto(postService.findPostById(postId))); + model.addAttribute("profiles", userService.findAllUsers().stream().map(UserDto::new).toList()); + model.addAttribute("posts", topicService.findTopicById(topicId).getPosts().stream().map(PostDto::new).toList()); + + UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + var user = userService.findUserByLogin(principal.getUsername()); + model.addAttribute("selectedProfile", new UserDto(userService.findUserById(user.getId()))); + model.addAttribute("selectedTopic", new TopicDto(topicService.findTopicById(topicId))); + return "commentModal"; + } + + @PostMapping(value = {"comment/{authorId}/{topicId}/{postId}/"}) + public String createComment(@PathVariable Long authorId, @PathVariable Long topicId, @PathVariable Long postId, @RequestParam(value="commentInputField") String commentInputField) { + commentService.addComment(commentInputField, authorId, postId); + return "redirect:/feed/" + topicId.toString(); + } + + @PostMapping(value = {"/deleteComment/{id}/{topicId}"}) + public String deleteComment(@PathVariable Long id, @PathVariable Long topicId) { + commentService.deleteComment(id); + return "redirect:/feed/" + topicId.toString(); + } + + @GetMapping(value = {"commentEditModal/{id}/{topicId}"}) + public String getCommentEditModal(@PathVariable Long id,@PathVariable Long topicId, Model model) { + model.addAttribute("selectedComment", new CommentDto(commentService.findCommentById(id))); + model.addAttribute("profiles", userService.findAllUsers().stream().map(UserDto::new).toList()); + model.addAttribute("posts", topicService.findTopicById(topicId).getPosts().stream().map(PostDto::new).toList()); + + UserDetails principal = (UserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal(); + var user = userService.findUserByLogin(principal.getUsername()); + model.addAttribute("selectedProfile", new UserDto(userService.findUserById(user.getId()))); + model.addAttribute("selectedTopic", new TopicDto(topicService.findTopicById(topicId))); + return "editCommentModal"; + } + + @PostMapping(value = {"editComment/{topicId}/{commentId}/"}) + public String editComment(@PathVariable Long topicId,@PathVariable Long commentId, @RequestParam(value="commentEditField") String commentEditField) { + commentService.updateComment(commentId, commentEditField); + return "redirect:/feed/" + topicId.toString(); + } + } diff --git a/src/main/java/com/webproglabs/lab1/mvc/UserMvcController.java b/src/main/java/com/webproglabs/lab1/mvc/UserMvcController.java new file mode 100644 index 0000000..9a8d3de --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/mvc/UserMvcController.java @@ -0,0 +1,24 @@ +package com.webproglabs.lab1.mvc; + +import com.webproglabs.lab1.dto.UserDto; +import com.webproglabs.lab1.services.UserService; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; + +@Controller +@RequestMapping("/users") +public class UserMvcController { + private final UserService userService; + + public UserMvcController(UserService userService) { + this.userService = userService; + } + + @GetMapping + public String getUsersPage(Model model){ + model.addAttribute("users", userService.findAllUsers().stream().map(UserDto::new).toList()); + return "users"; + } +} diff --git a/src/main/java/com/webproglabs/lab1/mvc/UserSignupMvcController.java b/src/main/java/com/webproglabs/lab1/mvc/UserSignupMvcController.java index 9e14725..461c86e 100644 --- a/src/main/java/com/webproglabs/lab1/mvc/UserSignupMvcController.java +++ b/src/main/java/com/webproglabs/lab1/mvc/UserSignupMvcController.java @@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping; import javax.validation.Valid; @Controller -@RequestMapping(UserSignupMvcController.SIGNUP_URL) +@RequestMapping() public class UserSignupMvcController { public static final String SIGNUP_URL = "/signup"; @@ -25,12 +25,17 @@ public class UserSignupMvcController { } @GetMapping + public String toFeed() { + return "redirect:/feed"; + } + + @GetMapping(SIGNUP_URL) public String showSignupForm(Model model) { model.addAttribute("userDto", new UserSignupDto()); return "signup"; } - @PostMapping + @PostMapping(SIGNUP_URL) public String signup(@ModelAttribute("userDto") @Valid UserSignupDto userSignupDto, BindingResult bindingResult, Model model) { diff --git a/src/main/java/com/webproglabs/lab1/services/CommentService.java b/src/main/java/com/webproglabs/lab1/services/CommentService.java index f073a4c..8e3d6a4 100644 --- a/src/main/java/com/webproglabs/lab1/services/CommentService.java +++ b/src/main/java/com/webproglabs/lab1/services/CommentService.java @@ -37,21 +37,6 @@ public class CommentService { return commentRepository.findAll(); } - @Transactional - public List findFilteredComments(String filter) { - List postList = postRepository.findByTextLike("%" + filter + "%"); - List commentList = commentRepository.findByTextLike("%" + filter + "%"); - List 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 userId, Long postId) { if (!StringUtils.hasText(text)) { diff --git a/src/main/java/com/webproglabs/lab1/services/PostService.java b/src/main/java/com/webproglabs/lab1/services/PostService.java index 93f7f10..0dcc674 100644 --- a/src/main/java/com/webproglabs/lab1/services/PostService.java +++ b/src/main/java/com/webproglabs/lab1/services/PostService.java @@ -42,21 +42,6 @@ public class PostService { return postRepository.findAll(); } - @Transactional - public List findFilteredPosts(String filter) { - List postList = postRepository.findByTextLike("%" + filter + "%"); - List commentList = commentRepository.findByTextLike("%" + filter + "%"); - List 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 List findPostsInTopicByText(String text, Long topicId) { Topic topic = topicRepository.findById(topicId).get(); diff --git a/src/main/resources/templates/commentModal.html b/src/main/resources/templates/commentModal.html new file mode 100644 index 0000000..63242a1 --- /dev/null +++ b/src/main/resources/templates/commentModal.html @@ -0,0 +1,32 @@ + + + + + + +
+ + + + + +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/editCommentModal.html b/src/main/resources/templates/editCommentModal.html new file mode 100644 index 0000000..0c18c69 --- /dev/null +++ b/src/main/resources/templates/editCommentModal.html @@ -0,0 +1,30 @@ + + + + + +
+ + + + +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/feedPosts.html b/src/main/resources/templates/feedPosts.html index d49d32c..40bde81 100644 --- a/src/main/resources/templates/feedPosts.html +++ b/src/main/resources/templates/feedPosts.html @@ -59,10 +59,10 @@

-
+
-
+ @@ -73,7 +73,7 @@
- +