diff --git a/src/main/java/np/something/DTO/CommentDto.java b/src/main/java/np/something/DTO/CommentDto.java index 589efdf..a239ae7 100644 --- a/src/main/java/np/something/DTO/CommentDto.java +++ b/src/main/java/np/something/DTO/CommentDto.java @@ -41,6 +41,10 @@ public class CommentDto { return content; } + public void setContent(String content) { + this.content = content; + } + public long getCustomerId() { return customerId; } diff --git a/src/main/java/np/something/DTO/PostDto.java b/src/main/java/np/something/DTO/PostDto.java index 73239a0..56612b9 100644 --- a/src/main/java/np/something/DTO/PostDto.java +++ b/src/main/java/np/something/DTO/PostDto.java @@ -40,10 +40,18 @@ public class PostDto { return title; } + public void setTitle(String title) { + this.title = title; + } + public String getContent() { return content; } + public void setContent() { + this.content = content; + } + @JsonProperty(access = JsonProperty.Access.READ_ONLY) public String getCustomerName() { return customerName; diff --git a/src/main/java/np/something/mvc/Comments.java b/src/main/java/np/something/mvc/Comments.java new file mode 100644 index 0000000..d9acd3b --- /dev/null +++ b/src/main/java/np/something/mvc/Comments.java @@ -0,0 +1,55 @@ +package np.something.mvc; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; +import jakarta.validation.Valid; +import np.something.DTO.CommentDto; +import np.something.DTO.PostDto; +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.*; + +@Controller +@RequestMapping("/comments") +public class Comments { + private final CustomerService customerService; + private final CommentService commentService; + private final PostService postService; + + public Comments(CustomerService customerService, CommentService commentService, PostService postService) { + this.customerService = customerService; + this.commentService = commentService; + this.postService = postService; + } + + @PostMapping(value = { "/", "/{id}"}) + public String manipulateComment(@PathVariable(required = false) Long id, @ModelAttribute @Valid CommentDto commentDto, + HttpServletRequest request, HttpSession session, BindingResult bindingResult, Model model) { + model.addAttribute("request", request); + model.addAttribute("session", session); + model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); + + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "/feed"; + } + + if (id == null || id <= 0) { + commentService.addComment(customerService.findCustomer(commentDto.customerId), postService.findPost(commentDto.postId), commentDto.content); + } else { + commentService.updateComment(id, commentDto.content); + } + + return "redirect:/feed"; + } + + @PostMapping("/delete/{id}") + public String deleteComment(@PathVariable Long id) { + commentService.deleteComment(id); + return "redirect:/feed/"; + } +} diff --git a/src/main/java/np/something/mvc/Feed.java b/src/main/java/np/something/mvc/Feed.java new file mode 100644 index 0000000..cedf813 --- /dev/null +++ b/src/main/java/np/something/mvc/Feed.java @@ -0,0 +1,30 @@ +package np.something.mvc; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; +import np.something.DTO.PostDto; +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; + +@Controller +@RequestMapping("/feed") +public class Feed { + private final PostService postService; + + public Feed(PostService postService) { + this.postService = postService; + } + + @GetMapping + public String getPosts(HttpServletRequest request, HttpSession session, Model model) { + model.addAttribute("request", request); + model.addAttribute("session", session); + model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); + + return "/feed"; + } +} diff --git a/src/main/java/np/something/mvc/Posts.java b/src/main/java/np/something/mvc/Posts.java new file mode 100644 index 0000000..f187f53 --- /dev/null +++ b/src/main/java/np/something/mvc/Posts.java @@ -0,0 +1,56 @@ +package np.something.mvc; + +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpSession; +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.web.bind.annotation.*; +import org.springframework.validation.BindingResult; +import jakarta.validation.Valid; +import np.something.DTO.PostDto; + +@Controller +@RequestMapping("/posts") +public class Posts { + private final CustomerService customerService; + private final CommentService commentService; + private final PostService postService; + + public Posts(CustomerService customerService, CommentService commentService, PostService postService) { + this.customerService = customerService; + this.commentService = commentService; + this.postService = postService; + } + + @PostMapping("/delete/{id}") + public String deletePost(@PathVariable Long id) { + postService.deletePost(id); + return "redirect:/feed"; + } + + @PostMapping(value = { "/", "/{id}"}) + public String manipulatePost(@PathVariable(required = false) Long id, @ModelAttribute @Valid PostDto postDto, + HttpServletRequest request, HttpSession session, + BindingResult bindingResult, + Model model) { + model.addAttribute("request", request); + model.addAttribute("session", session); + model.addAttribute("posts", postService.findAllPosts().stream().map(PostDto::new).toList()); + + if (bindingResult.hasErrors()) { + model.addAttribute("errors", bindingResult.getAllErrors()); + return "/feed"; + } + + if (id == null || id <= 0) { + postService.addPost(customerService.findCustomer(postDto.customerId), postDto.title, postDto.content); + } else { + postService.updatePost(id, postDto.title, postDto.content); + } + + return "redirect:/feed/"; + } +} diff --git a/src/main/resources/templates/feed.html b/src/main/resources/templates/feed.html new file mode 100644 index 0000000..1d67f27 --- /dev/null +++ b/src/main/resources/templates/feed.html @@ -0,0 +1,10 @@ + + + + + +
+ +
+ + \ No newline at end of file