diff --git a/src/main/java/com/webproglabs/lab1/dao/PostRepository.java b/src/main/java/com/webproglabs/lab1/dao/PostRepository.java index f2add34..6c628bf 100644 --- a/src/main/java/com/webproglabs/lab1/dao/PostRepository.java +++ b/src/main/java/com/webproglabs/lab1/dao/PostRepository.java @@ -1,7 +1,10 @@ package com.webproglabs.lab1.dao; import com.webproglabs.lab1.models.Post; +import com.webproglabs.lab1.models.Topic; 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; import java.util.Optional; @@ -9,4 +12,7 @@ import java.util.Optional; public interface PostRepository extends JpaRepository { List findByTextLike(String text); Optional findById(Long Id); + + @Query("SELECT p FROM Post p WHERE p.topic = :topic AND p.text LIKE %:text%") + List findPostsByTextInTopic(@Param("text") String text, @Param("topic") Topic topic); } diff --git a/src/main/java/com/webproglabs/lab1/dao/TopicRepository.java b/src/main/java/com/webproglabs/lab1/dao/TopicRepository.java index e0ea4f2..a8c17f1 100644 --- a/src/main/java/com/webproglabs/lab1/dao/TopicRepository.java +++ b/src/main/java/com/webproglabs/lab1/dao/TopicRepository.java @@ -1,10 +1,15 @@ package com.webproglabs.lab1.dao; +import com.webproglabs.lab1.models.Post; import com.webproglabs.lab1.models.Topic; 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; import java.util.Optional; public interface TopicRepository extends JpaRepository { Optional findById(Long Id); + } diff --git a/src/main/java/com/webproglabs/lab1/dto/TopicDto.java b/src/main/java/com/webproglabs/lab1/dto/TopicDto.java index 95813f0..9d2961a 100644 --- a/src/main/java/com/webproglabs/lab1/dto/TopicDto.java +++ b/src/main/java/com/webproglabs/lab1/dto/TopicDto.java @@ -21,6 +21,7 @@ public class TopicDto { posts.add(new PostDto(post)); } } + public TopicDto(){} public Long getId() { return id; diff --git a/src/main/java/com/webproglabs/lab1/models/Topic.java b/src/main/java/com/webproglabs/lab1/models/Topic.java index 5415714..a7e65a4 100644 --- a/src/main/java/com/webproglabs/lab1/models/Topic.java +++ b/src/main/java/com/webproglabs/lab1/models/Topic.java @@ -22,6 +22,7 @@ public class Topic { this.description = description; this.posts = new ArrayList(); } + public Topic(){} public Long getId() { return id; diff --git a/src/main/java/com/webproglabs/lab1/mvc/FeedMvcController.java b/src/main/java/com/webproglabs/lab1/mvc/FeedMvcController.java new file mode 100644 index 0000000..2dcb327 --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/mvc/FeedMvcController.java @@ -0,0 +1,97 @@ +package com.webproglabs.lab1.mvc; + +import com.webproglabs.lab1.dto.PostDto; +import com.webproglabs.lab1.dto.TopicDto; +import com.webproglabs.lab1.dto.UserDto; +import com.webproglabs.lab1.services.CommentService; +import com.webproglabs.lab1.services.PostService; +import com.webproglabs.lab1.services.TopicService; +import com.webproglabs.lab1.services.UserService; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +@Controller +@RequestMapping("/feed") +public class FeedMvcController { + private final UserService userService; + private final PostService postService; + private final CommentService commentService; + private final TopicService topicService; + + public FeedMvcController(UserService userService, PostService postService, CommentService commentService, TopicService topicService) { + this.userService = userService; + this.postService = postService; + this.commentService = commentService; + this.topicService = topicService; + } + + @GetMapping + public String getFeedPage(Model model) { + model.addAttribute("topics", topicService.findAllTopics().stream().map(TopicDto::new).toList()); + return "feed"; + } + + @GetMapping(value = {"/{id}"}) + public String getFeedPageWithTopic(@PathVariable Long id, Model model) { + model.addAttribute("profiles", userService.findAllUsers().stream().map(UserDto::new).toList()); + model.addAttribute("posts", topicService.findTopicById(id).getPosts().stream().map(PostDto::new).toList()); + model.addAttribute("topics", topicService.findAllTopics().stream().map(TopicDto::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(id))); + + return "feedPosts"; + } + + @GetMapping(value= {"/filter/{id}/"}) + public String getFeedPageFiltered(@PathVariable Long id, @RequestParam(value="searchField") String searchField, Model model) { + model.addAttribute("profiles", userService.findAllUsers().stream().map(UserDto::new).toList()); + model.addAttribute("topics", topicService.findAllTopics().stream().map(TopicDto::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(id))); + + model.addAttribute("posts", postService.findPostsInTopicByText(searchField, id).stream().map(PostDto::new).toList()); + + return "feedPosts"; + } + + @PostMapping(value={"/{topicId}/post/{id}/"}) + public String createPost(@PathVariable Long topicId, @PathVariable Long id, @RequestParam(value="postInputField") String postInputField) { + postService.addPost(postInputField, id, topicId); + return "redirect:/feed/" + topicId.toString(); + } + + @PostMapping(value = {"/deletePost/{id}/{topicId}"}) + public String deletePost(@PathVariable Long id, @PathVariable Long topicId) { + postService.deletePost(id); + return "redirect:/feed/" + topicId.toString(); + } + + @GetMapping(value = {"postModal/{id}/{topicId}"}) + public String getPostEditModal(@PathVariable Long id,@PathVariable Long topicId, Model model) { + model.addAttribute("selectedPost", new PostDto(postService.findPostById(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 "editPostModal"; + } + + @PostMapping(value = {"editPost/{id}/{topicId}/"}) + public String editPost(@PathVariable Long id, @PathVariable Long topicId, @RequestParam(value="postEditField") String postEditField) { + postService.updatePost(id, postEditField); + return "redirect:/feed/" + topicId.toString(); + } + +} diff --git a/src/main/java/com/webproglabs/lab1/mvc/TopicMvcController.java b/src/main/java/com/webproglabs/lab1/mvc/TopicMvcController.java new file mode 100644 index 0000000..90656ff --- /dev/null +++ b/src/main/java/com/webproglabs/lab1/mvc/TopicMvcController.java @@ -0,0 +1,58 @@ +package com.webproglabs.lab1.mvc; + +import com.webproglabs.lab1.dto.TopicDto; +import com.webproglabs.lab1.dto.UserSignupDto; +import com.webproglabs.lab1.models.UserRole; +import com.webproglabs.lab1.services.TopicService; +import org.springframework.security.access.annotation.Secured; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +@Controller +@RequestMapping("/topic") +public class TopicMvcController { + private final TopicService topicService; + + public TopicMvcController(TopicService topicService) { + this.topicService = topicService; + } + + @GetMapping + @Secured({UserRole.AsString.ADMIN}) + public String getTopics(Model model) { + model.addAttribute("topics", topicService.findAllTopics().stream().map(TopicDto::new).toList()); + model.addAttribute("topicDto", new TopicDto()); + return "topics"; + } + + @PostMapping(value = {"/create"}) + @Secured({UserRole.AsString.ADMIN}) + public String createTopic(@ModelAttribute TopicDto topicDto) { + topicService.addTopic(topicDto.getName(), topicDto.getDescription()); + return "redirect:/topic"; + } + + @PostMapping(value = {"/delete/{Id}"}) + @Secured({UserRole.AsString.ADMIN}) + public String deleteTopic(@PathVariable Long Id) { + topicService.deleteTopic(Id); + return "redirect:/topic"; + } + + @GetMapping(value = {"/edit/{Id}"}) + @Secured({UserRole.AsString.ADMIN}) + public String getTopicEdit(@PathVariable Long Id, Model model) { + model.addAttribute("topic", new TopicDto(topicService.findTopicById(Id))); + model.addAttribute("topicDto", new TopicDto(topicService.findTopicById(Id))); + return "topicEdit"; + } + + @PostMapping(value = {"/edit/{Id}"}) + @Secured({UserRole.AsString.ADMIN}) + public String editTopic(@PathVariable Long Id, @ModelAttribute TopicDto topicDto) { + topicService.updateTopic(Id, topicDto.getName(), topicDto.getDescription()); + return "redirect:/topic"; + } + +} diff --git a/src/main/java/com/webproglabs/lab1/services/PostService.java b/src/main/java/com/webproglabs/lab1/services/PostService.java index 2cf1a08..93f7f10 100644 --- a/src/main/java/com/webproglabs/lab1/services/PostService.java +++ b/src/main/java/com/webproglabs/lab1/services/PostService.java @@ -57,6 +57,12 @@ public class PostService { return postList; } + @Transactional + public List findPostsInTopicByText(String text, Long topicId) { + Topic topic = topicRepository.findById(topicId).get(); + return postRepository.findPostsByTextInTopic(text, topic); + } + @Transactional public Post addPost (String text, Long authorId, Long topicId) { if (!StringUtils.hasText(text)) { diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index de0a82b..4d06ca0 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -24,10 +24,10 @@ + \ No newline at end of file diff --git a/src/main/resources/templates/editPostModal.html b/src/main/resources/templates/editPostModal.html new file mode 100644 index 0000000..2b818ab --- /dev/null +++ b/src/main/resources/templates/editPostModal.html @@ -0,0 +1,32 @@ + + + + + + +
+ + + +
+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/feed.html b/src/main/resources/templates/feed.html new file mode 100644 index 0000000..02d1a6a --- /dev/null +++ b/src/main/resources/templates/feed.html @@ -0,0 +1,32 @@ + + + + + + +
+
+ +
+ + +
+
+ + + + \ No newline at end of file diff --git a/src/main/resources/templates/feedPosts.html b/src/main/resources/templates/feedPosts.html new file mode 100644 index 0000000..d49d32c --- /dev/null +++ b/src/main/resources/templates/feedPosts.html @@ -0,0 +1,107 @@ + + + + + +
+ +
+

+

+
+
+ + Лента + +
+ +
+ + +
+ + +
+
+

+
+
+ Автор: + +
+
+
+ +
+
+ +
+
+
+
+ +
+
+

Комментарии:

+
+

+
+

+ +
+
+ +
+
+ +
+
+ +
+
+
+
+
+ +
+ +
+ + + + + +
+ + +
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/signup.html b/src/main/resources/templates/signup.html index 6eb4da4..77dd026 100644 --- a/src/main/resources/templates/signup.html +++ b/src/main/resources/templates/signup.html @@ -1,4 +1,3 @@ - + + +
+
Изменить топик
+
+

Новое название:

+ +

Новое описание:

+ + +
+
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/topics.html b/src/main/resources/templates/topics.html new file mode 100644 index 0000000..5ad87ad --- /dev/null +++ b/src/main/resources/templates/topics.html @@ -0,0 +1,32 @@ + + + +
+ +
+

Название:

+ +

Описание:

+ + +
+ +
+
+
+
+ +
+
+ +
+
+ + + + +
+ + \ No newline at end of file