4 lab попытка сделать добавление постов (неудачно)

This commit is contained in:
Павел Сорокин 2023-04-07 20:33:54 +04:00
parent 62d5bd2394
commit 4b9638ad2f
7 changed files with 9 additions and 10 deletions

Binary file not shown.

View File

@ -9,6 +9,7 @@ public class CommentDto {
private String Text;
private Post post;
private User user;
public CommentDto(){}
public CommentDto(Comment comment)
{
this.Text=comment.getText();

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp.Post.controller;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import ru.ulstu.is.sbapp.Post.service.PostService;
import ru.ulstu.is.sbapp.User.controller.UserDto;
@ -25,9 +26,8 @@ public class PostController {
.toList();
}
@PostMapping
public PostDto createPost(@RequestParam("Heading") String Heading,
@RequestParam("Content") String Content){
return new PostDto(postService.addPost(Heading, Content));
public PostDto createPost(@RequestBody @Valid PostDto postDto){
return new PostDto(postService.addPost(postDto));
}
@PutMapping("/{id}")

View File

@ -21,6 +21,7 @@ public class PostDto {
private List<Comment> comments = new ArrayList<>();
public PostDto(){}
public PostDto(Post post)
{
this.Heading = post.getHeading();

View File

@ -33,11 +33,6 @@ public class Post {
private List<Comment> comments=new ArrayList<>();
public Post(){}
public Post(String Heading, String Content)
{
this.Heading = Heading;
this.Content = Content;
}
public Post(String Heading, String Content,byte[] image)
{
this.Heading = Heading;

View File

@ -6,6 +6,7 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import ru.ulstu.is.sbapp.Comment.model.Comment;
import ru.ulstu.is.sbapp.Comment.repository.CommentRepository;
import ru.ulstu.is.sbapp.Post.controller.PostDto;
import ru.ulstu.is.sbapp.Post.model.Post;
import ru.ulstu.is.sbapp.Post.repository.PostRepository;
import ru.ulstu.is.sbapp.User.model.User;
@ -27,8 +28,8 @@ public class PostService {
this.validatorUtil=validatorUtil;
}
@Transactional
public Post addPost(String Heading, String Content) {
final Post post = new Post(Heading,Content);
public Post addPost(PostDto postDto) {
final Post post = new Post(postDto);
validatorUtil.validate(post);
return postRepository.save(post);
}

View File

@ -20,6 +20,7 @@ public class UserDto {
private List<Comment> comments = new ArrayList<>();
public UserDto(){}
public UserDto(User user) {
this.id=user.getId();
this.firstName = user.getFirstName();