diff --git a/data.mv.db b/data.mv.db index e689d54..23341a7 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java index 6b8cb7d..9b6ead5 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/CommentDto.java @@ -6,10 +6,12 @@ import com.webproglabs.lab1.lab34.model.Comment; public class CommentDto { private Long id; private String text; + private String authorLogin; public CommentDto(Comment comment) { this.id = comment.getId(); this.text = comment.getText(); + this.authorLogin = comment.getOwner().getLogin(); } @JsonProperty(access = JsonProperty.Access.READ_ONLY) @@ -17,4 +19,5 @@ public class CommentDto { return id; } public String getText() {return text;} + public String getAuthor() {return authorLogin;} } diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/FeedMvcController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/FeedMvcController.java index 3acb327..6ceecf8 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/FeedMvcController.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/FeedMvcController.java @@ -1,8 +1,9 @@ package com.webproglabs.lab1.lab34.controller.mvc_controllers; +import com.webproglabs.lab1.lab34.controller.CommentDto; import com.webproglabs.lab1.lab34.controller.PostDto; import com.webproglabs.lab1.lab34.controller.ProfileDto; -import com.webproglabs.lab1.lab34.model.Post; +import com.webproglabs.lab1.lab34.services.CommentService; import com.webproglabs.lab1.lab34.services.PostService; import com.webproglabs.lab1.lab34.services.ProfileService; import org.springframework.stereotype.Controller; @@ -14,13 +15,13 @@ import java.util.ArrayList; @Controller @RequestMapping("/feed") public class FeedMvcController { - private final ProfileService profileService; private final PostService postService; - - public FeedMvcController(ProfileService profileService, PostService postService) { + private final CommentService commentService; + public FeedMvcController(ProfileService profileService, PostService postService, CommentService commentService) { this.profileService = profileService; this.postService = postService; + this.commentService = commentService; } @GetMapping @@ -72,4 +73,40 @@ public class FeedMvcController { postService.updatePost(id, postEditField); return "redirect:/feed/" + authorId.toString(); } + + @GetMapping(value = {"commentModal/{authorId}/{postId}"}) + public String getCommentModal(@PathVariable(required = false) Long authorId,@PathVariable(required = false) Long postId, Model model) { + model.addAttribute("selectedPost", new PostDto(postService.findPost(postId))); + model.addAttribute("profiles", profileService.findAllUsers().stream().map(ProfileDto::new).toList()); + model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); + model.addAttribute("selectedProfile", new ProfileDto(profileService.findUser(authorId))); + return "commentModal"; + } + + @PostMapping(value = {"comment/{authorId}/{postId}/"}) + public String createComment(@PathVariable(required = false) Long authorId,@PathVariable(required = false) Long postId, @RequestParam(value="commentInputField") String commentInputField) { + commentService.addComment(commentInputField, authorId, postId); + return "redirect:/feed/" + authorId.toString(); + } + + @PostMapping(value = {"/deleteComment/{id}/{authorId}"}) + public String deleteComment(@PathVariable(required = false) Long id, @PathVariable(required = false) Long authorId) { + commentService.deleteComment(id); + return "redirect:/feed/" + authorId.toString(); + } + + @GetMapping(value = {"commentEditModal/{id}/{authorId}"}) + public String getCommentEditModal(@PathVariable(required = false) Long id,@PathVariable(required = false) Long authorId, Model model) { + model.addAttribute("selectedComment", new CommentDto(commentService.findComment(id))); + model.addAttribute("profiles", profileService.findAllUsers().stream().map(ProfileDto::new).toList()); + model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); + model.addAttribute("selectedProfile", new ProfileDto(profileService.findUser(authorId))); + return "editCommentModal"; + } + + @PostMapping(value = {"editComment/{authorId}/{commentId}/"}) + public String editComment(@PathVariable(required = false) Long authorId,@PathVariable(required = false) Long commentId, @RequestParam(value="commentEditField") String commentEditField) { + commentService.updateComment(commentId, commentEditField); + return "redirect:/feed/" + authorId.toString(); + } } diff --git a/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/ProfileMvcController.java b/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/ProfileMvcController.java index 032ba34..efd2e16 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/ProfileMvcController.java +++ b/src/main/java/com/webproglabs/lab1/lab34/controller/mvc_controllers/ProfileMvcController.java @@ -1,15 +1,30 @@ package com.webproglabs.lab1.lab34.controller.mvc_controllers; +import com.webproglabs.lab1.lab34.controller.ProfileDto; +import com.webproglabs.lab1.lab34.services.ProfileService; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/") public class ProfileMvcController { + private final ProfileService profileService; + + public ProfileMvcController(ProfileService profileService) { + this.profileService = profileService; + } @GetMapping public String index() { return "default"; } + + @GetMapping(value = {"profile/{login}"}) + public String getProfile(@PathVariable(required = false) String login, Model model) { + model.addAttribute("profile", new ProfileDto(profileService.findUserByLogin(login))); + return "profilePage"; + } } diff --git a/src/main/java/com/webproglabs/lab1/lab34/repository/ProfileRepository.java b/src/main/java/com/webproglabs/lab1/lab34/repository/ProfileRepository.java index f2508b7..1497572 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/repository/ProfileRepository.java +++ b/src/main/java/com/webproglabs/lab1/lab34/repository/ProfileRepository.java @@ -1,7 +1,11 @@ package com.webproglabs.lab1.lab34.repository; +import com.webproglabs.lab1.lab34.model.Post; import com.webproglabs.lab1.lab34.model.Profile; import org.springframework.data.jpa.repository.JpaRepository; +import java.util.List; + public interface ProfileRepository extends JpaRepository { + List findByLoginLike(String login); } diff --git a/src/main/java/com/webproglabs/lab1/lab34/services/ProfileService.java b/src/main/java/com/webproglabs/lab1/lab34/services/ProfileService.java index 933e9bb..1be9ffe 100644 --- a/src/main/java/com/webproglabs/lab1/lab34/services/ProfileService.java +++ b/src/main/java/com/webproglabs/lab1/lab34/services/ProfileService.java @@ -28,6 +28,12 @@ public class ProfileService { return profile.orElseThrow(EntityNotFoundException::new); } + @Transactional + public Profile findUserByLogin(String login) { + final Optional profile = profileRepository.findByLoginLike(login).stream().findFirst(); + return profile.orElseThrow(EntityNotFoundException::new); + } + @Transactional public List findAllUsers() { return profileRepository.findAll(); diff --git a/src/main/resources/templates/commentModal.html b/src/main/resources/templates/commentModal.html new file mode 100644 index 0000000..a09c141 --- /dev/null +++ b/src/main/resources/templates/commentModal.html @@ -0,0 +1,31 @@ + + + + + +
+ + + + + +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/default.html b/src/main/resources/templates/default.html index 098c64a..4d91ccf 100644 --- a/src/main/resources/templates/default.html +++ b/src/main/resources/templates/default.html @@ -33,6 +33,9 @@ \ 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..1a3ccb9 --- /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/editPostModal.html b/src/main/resources/templates/editPostModal.html index fcb157d..51b5340 100644 --- a/src/main/resources/templates/editPostModal.html +++ b/src/main/resources/templates/editPostModal.html @@ -29,9 +29,4 @@ - \ No newline at end of file diff --git a/src/main/resources/templates/feed.html b/src/main/resources/templates/feed.html index 2b02dd5..13aee47 100644 --- a/src/main/resources/templates/feed.html +++ b/src/main/resources/templates/feed.html @@ -27,10 +27,6 @@ - + \ No newline at end of file diff --git a/src/main/resources/templates/feedPosts.html b/src/main/resources/templates/feedPosts.html index e6064e4..c21453a 100644 --- a/src/main/resources/templates/feedPosts.html +++ b/src/main/resources/templates/feedPosts.html @@ -26,13 +26,13 @@ -
+

Автор: - +
@@ -48,6 +48,34 @@
+ +
+
+

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

+
+

+
+

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

Профиль

+

+

Список постов пользователя:

+ +
+
+

+
+
+

+ Нет постов +

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