Lab5: Some mvc completed, feed.html remains

This commit is contained in:
Сергей Полевой 2023-04-18 04:13:40 +04:00
parent 535cca10bd
commit 4410a17fcf
6 changed files with 163 additions and 0 deletions

View File

@ -41,6 +41,10 @@ public class CommentDto {
return content; return content;
} }
public void setContent(String content) {
this.content = content;
}
public long getCustomerId() { public long getCustomerId() {
return customerId; return customerId;
} }

View File

@ -40,10 +40,18 @@ public class PostDto {
return title; return title;
} }
public void setTitle(String title) {
this.title = title;
}
public String getContent() { public String getContent() {
return content; return content;
} }
public void setContent() {
this.content = content;
}
@JsonProperty(access = JsonProperty.Access.READ_ONLY) @JsonProperty(access = JsonProperty.Access.READ_ONLY)
public String getCustomerName() { public String getCustomerName() {
return customerName; return customerName;

View File

@ -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/";
}
}

View File

@ -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";
}
}

View File

@ -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/";
}
}

View File

@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
</div>
</body>
</html>