comments & users start
This commit is contained in:
parent
89c76406ea
commit
263182db1d
@ -1,5 +1,6 @@
|
|||||||
package com.webproglabs.lab1.mvc;
|
package com.webproglabs.lab1.mvc;
|
||||||
|
|
||||||
|
import com.webproglabs.lab1.dto.CommentDto;
|
||||||
import com.webproglabs.lab1.dto.PostDto;
|
import com.webproglabs.lab1.dto.PostDto;
|
||||||
import com.webproglabs.lab1.dto.TopicDto;
|
import com.webproglabs.lab1.dto.TopicDto;
|
||||||
import com.webproglabs.lab1.dto.UserDto;
|
import com.webproglabs.lab1.dto.UserDto;
|
||||||
@ -94,4 +95,48 @@ public class FeedMvcController {
|
|||||||
return "redirect:/feed/" + topicId.toString();
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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";
|
||||||
|
}
|
||||||
|
}
|
@ -14,7 +14,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||||||
import javax.validation.Valid;
|
import javax.validation.Valid;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(UserSignupMvcController.SIGNUP_URL)
|
@RequestMapping()
|
||||||
public class UserSignupMvcController {
|
public class UserSignupMvcController {
|
||||||
public static final String SIGNUP_URL = "/signup";
|
public static final String SIGNUP_URL = "/signup";
|
||||||
|
|
||||||
@ -25,12 +25,17 @@ public class UserSignupMvcController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping
|
@GetMapping
|
||||||
|
public String toFeed() {
|
||||||
|
return "redirect:/feed";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping(SIGNUP_URL)
|
||||||
public String showSignupForm(Model model) {
|
public String showSignupForm(Model model) {
|
||||||
model.addAttribute("userDto", new UserSignupDto());
|
model.addAttribute("userDto", new UserSignupDto());
|
||||||
return "signup";
|
return "signup";
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping(SIGNUP_URL)
|
||||||
public String signup(@ModelAttribute("userDto") @Valid UserSignupDto userSignupDto,
|
public String signup(@ModelAttribute("userDto") @Valid UserSignupDto userSignupDto,
|
||||||
BindingResult bindingResult,
|
BindingResult bindingResult,
|
||||||
Model model) {
|
Model model) {
|
||||||
|
@ -37,21 +37,6 @@ public class CommentService {
|
|||||||
return commentRepository.findAll();
|
return commentRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public List<Post> findFilteredComments(String filter) {
|
|
||||||
List<Post> postList = postRepository.findByTextLike("%" + filter + "%");
|
|
||||||
List<Comment> commentList = commentRepository.findByTextLike("%" + filter + "%");
|
|
||||||
List<Post> 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
|
@Transactional
|
||||||
public Comment addComment(String text, Long userId, Long postId) {
|
public Comment addComment(String text, Long userId, Long postId) {
|
||||||
if (!StringUtils.hasText(text)) {
|
if (!StringUtils.hasText(text)) {
|
||||||
|
@ -42,21 +42,6 @@ public class PostService {
|
|||||||
return postRepository.findAll();
|
return postRepository.findAll();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
|
||||||
public List<Post> findFilteredPosts(String filter) {
|
|
||||||
List<Post> postList = postRepository.findByTextLike("%" + filter + "%");
|
|
||||||
List<Comment> commentList = commentRepository.findByTextLike("%" + filter + "%");
|
|
||||||
List<Post> 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
|
@Transactional
|
||||||
public List<Post> findPostsInTopicByText(String text, Long topicId) {
|
public List<Post> findPostsInTopicByText(String text, Long topicId) {
|
||||||
Topic topic = topicRepository.findById(topicId).get();
|
Topic topic = topicRepository.findById(topicId).get();
|
||||||
|
32
src/main/resources/templates/commentModal.html
Normal file
32
src/main/resources/templates/commentModal.html
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
layout:decorate="~{feedPosts}" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div layout:fragment="modalFeed">
|
||||||
|
|
||||||
|
<div class="modal fade" id="commentCreate" tabindex="-1" role="dialog" aria-labelledby="commentCreateLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="commentCreateLabel">Создать комментарий</h5>
|
||||||
|
</div>
|
||||||
|
<form th:action="@{/feed/comment/{id}/{topicId}/{postId}/{text} (id=${selectedProfile.id}, topicId=${selectedTopic.id}, postId=${selectedPost.id}, text=${commentInputField}) }" method="post" class="modal-body text-center">
|
||||||
|
<p>Текст комментария:</p>
|
||||||
|
<input th:value="${commentInputField}" id="commentInputField" name="commentInputField" type="text" class="mb-2">
|
||||||
|
<br>
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
|
||||||
|
<button type="submit" class="btn btn-primary" >Сохранить</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
30
src/main/resources/templates/editCommentModal.html
Normal file
30
src/main/resources/templates/editCommentModal.html
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en"
|
||||||
|
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
|
||||||
|
layout:decorate="~{feedPosts}" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||||
|
<head>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div layout:fragment="modalFeed">
|
||||||
|
|
||||||
|
<div class="modal fade" id="commentEdit" tabindex="-1" role="dialog" aria-labelledby="commentEditLabel" aria-hidden="true">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title" id="commentEditLabel">Редактировать комментарий</h5>
|
||||||
|
</div>
|
||||||
|
<form th:action="@{/feed/editComment/{id}/{commentId}/{text} (id=${selectedTopic.id}, commentId=${selectedComment.id}, text=${commentEditField}) }" method="post" class="modal-body text-center">
|
||||||
|
<p>Новый текст комментария:</p>
|
||||||
|
<input th:attr="value = ${selectedComment.text}" id="commentEditField" name="commentEditField" type="text" class="mb-2">
|
||||||
|
<br>
|
||||||
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Закрыть</button>
|
||||||
|
<button type="submit" class="btn btn-primary" >Изменить</button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -59,10 +59,10 @@
|
|||||||
<p class="ms-3" th:text="${comment.text}"></p>
|
<p class="ms-3" th:text="${comment.text}"></p>
|
||||||
|
|
||||||
<div th:if="${selectedProfile.getLogin() == comment.getAuthor()}" class="d-flex justify-content-between fst-italic">
|
<div th:if="${selectedProfile.getLogin() == comment.getAuthor()}" class="d-flex justify-content-between fst-italic">
|
||||||
<form th:action="@{/feed/deleteComment/{id}/{authorId} (id=${comment.id}, authorId=${selectedProfile.id})}" method="post" >
|
<form th:action="@{/feed/deleteComment/{id}/{topicId} (id=${comment.id}, topicId=${selectedTopic.id})}" method="post" >
|
||||||
<button type="submit" class="btn btn-danger me-1 mb-1"> <i class="fa fa-trash" aria-hidden="true"> </i> </button>
|
<button type="submit" class="btn btn-danger me-1 mb-1"> <i class="fa fa-trash" aria-hidden="true"> </i> </button>
|
||||||
</form>
|
</form>
|
||||||
<form th:action="@{/feed/commentEditModal/{id}/{authorId} (id=${comment.id}, authorId=${selectedProfile.id})}" method="get">
|
<form th:action="@{/feed/commentEditModal/{id}/{topicId} (id=${comment.id}, topicId=${selectedTopic.id})}" method="get">
|
||||||
<button type="submit" class="btn btn-warning mb-1" data-bs-toggle="modal" data-bs-target="#postEdit" >
|
<button type="submit" class="btn btn-warning mb-1" data-bs-toggle="modal" data-bs-target="#postEdit" >
|
||||||
<i class="fa fa-pencil" aria-hidden="true"></i>
|
<i class="fa fa-pencil" aria-hidden="true"></i>
|
||||||
</button>
|
</button>
|
||||||
@ -73,7 +73,7 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<form th:action="@{/feed/commentModal/{authorId}/{postId}/ ( authorId=${selectedProfile.id}, postId=${post.id} ) }" method="get" class="text-end">
|
<form th:action="@{/feed/commentModal/{topicId}/{postId}/ ( topicId=${selectedTopic.id}, postId=${post.id} ) }" method="get" class="text-end">
|
||||||
<button type="submit" class="btn btn-info mb-3" data-bs-toggle="modal" data-bs-target="#commentCreate">Добавить комментарий</button>
|
<button type="submit" class="btn btn-info mb-3" data-bs-toggle="modal" data-bs-target="#commentCreate">Добавить комментарий</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user