4 lab pre front
This commit is contained in:
parent
c88068f489
commit
9cd8ce211d
@ -2,13 +2,11 @@ package ru.ulstu.is.sbapp.Comment.controller;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.sbapp.Comment.service.CommentService;
|
||||
import ru.ulstu.is.sbapp.Post.controller.PostDto;
|
||||
import ru.ulstu.is.sbapp.Post.service.PostService;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/controller")
|
||||
@RequestMapping("/comment")
|
||||
public class CommentController {
|
||||
private final CommentService commentService;
|
||||
public CommentController(CommentService commentService) {
|
||||
@ -31,7 +29,7 @@ public class CommentController {
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public CommentDto updateComment(@PathVariable Long id,
|
||||
@RequestParam("Text") String Text){
|
||||
@RequestParam("Text") String Text){
|
||||
return new CommentDto(commentService.updateComment(id,Text));
|
||||
}
|
||||
@DeleteMapping("/{id}")
|
||||
|
@ -7,12 +7,13 @@ import ru.ulstu.is.sbapp.User.model.User;
|
||||
public class CommentDto {
|
||||
private Long id;
|
||||
private String Text;
|
||||
private Post post;
|
||||
private User user;
|
||||
private String userName;
|
||||
public CommentDto(){}
|
||||
public CommentDto(Comment comment)
|
||||
{
|
||||
this.id= comment.getId();
|
||||
this.Text=comment.getText();
|
||||
this.userName=comment.getUser().getFirstName() + " " + comment.getUser().getLastName();
|
||||
}
|
||||
public Long getId()
|
||||
{
|
||||
@ -20,14 +21,10 @@ public class CommentDto {
|
||||
}
|
||||
public String getText() {return Text;}
|
||||
|
||||
public User getUser()
|
||||
public String getUser()
|
||||
{
|
||||
return user;
|
||||
return userName;
|
||||
}
|
||||
|
||||
public Post getPost()
|
||||
{
|
||||
return post;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ru.ulstu.is.sbapp.Comment.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
@ -16,9 +17,11 @@ public class Comment {
|
||||
private String Text;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
@JsonIgnore
|
||||
private Post post;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
@JsonIgnore
|
||||
private User user;
|
||||
|
||||
public Comment()
|
||||
|
@ -2,6 +2,8 @@ package ru.ulstu.is.sbapp.Post.controller;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import ru.ulstu.is.sbapp.Comment.controller.CommentDto;
|
||||
import ru.ulstu.is.sbapp.Post.model.Post;
|
||||
import ru.ulstu.is.sbapp.Post.service.PostService;
|
||||
import ru.ulstu.is.sbapp.User.controller.UserDto;
|
||||
import ru.ulstu.is.sbapp.User.service.UserService;
|
||||
@ -25,6 +27,12 @@ public class PostController {
|
||||
.map(PostDto::new)
|
||||
.toList();
|
||||
}
|
||||
@GetMapping("/{id}/comments")
|
||||
public List<CommentDto> getComments(@PathVariable Long id) {
|
||||
return postService.GetPostComments(id).stream()
|
||||
.map(CommentDto::new)
|
||||
.toList();
|
||||
}
|
||||
@PostMapping
|
||||
public PostDto createPost(@RequestBody @Valid PostDto postDto){
|
||||
return new PostDto(postService.addPost(postDto));
|
||||
@ -32,19 +40,18 @@ public class PostController {
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public PostDto updatePost(@PathVariable Long id,
|
||||
@RequestParam("Heading") String Heading,
|
||||
@RequestParam("Content") String Content){
|
||||
return new PostDto(postService.updatePost(id,Heading,Content));
|
||||
@RequestBody @Valid PostDto postDto){
|
||||
return new PostDto(postService.updatePost(id,postDto));
|
||||
}
|
||||
@PostMapping("/{id}/Comment/{userId}")
|
||||
public void addComment(@PathVariable Long id,
|
||||
@PathVariable Long userId,
|
||||
@RequestParam("Text") String Text) {
|
||||
@RequestParam("Text") String Text) {
|
||||
postService.addCommentToPost(id, userId,Text);
|
||||
}
|
||||
@DeleteMapping("/{id}/Comment/{postId}")
|
||||
@DeleteMapping("/{id}/Comment/{commentId}")
|
||||
public void removeComment(@PathVariable Long id,
|
||||
@PathVariable Long commentId)
|
||||
@PathVariable Long commentId)
|
||||
{
|
||||
postService.removeCommentFromPost(id,commentId);
|
||||
}
|
||||
|
@ -15,15 +15,14 @@ public class PostDto {
|
||||
|
||||
private String content;
|
||||
|
||||
private User user;
|
||||
|
||||
private String image;
|
||||
|
||||
private List<Comment> comments = new ArrayList<>();
|
||||
|
||||
public PostDto(){}
|
||||
public PostDto(Post post)
|
||||
{
|
||||
this.id= post.getId();
|
||||
this.heading = post.getHeading();
|
||||
this.content = post.getContent();
|
||||
this.image = new String(post.getImage(), StandardCharsets.UTF_8);
|
||||
@ -42,15 +41,7 @@ public class PostDto {
|
||||
{
|
||||
return content;
|
||||
}
|
||||
public User getUser()
|
||||
{
|
||||
return user;
|
||||
}
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
public List<Comment> getComments()
|
||||
{
|
||||
return comments;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package ru.ulstu.is.sbapp.Post.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import jakarta.persistence.*;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import ru.ulstu.is.sbapp.Comment.model.Comment;
|
||||
@ -27,6 +28,7 @@ public class Post {
|
||||
private byte[] image;
|
||||
|
||||
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.MERGE)
|
||||
@JsonIgnore
|
||||
private User user;
|
||||
|
||||
@OneToMany(mappedBy = "post",fetch = FetchType.EAGER,cascade = CascadeType.ALL,orphanRemoval = true)
|
||||
|
@ -11,4 +11,5 @@ public interface PostRepositoryExtension {
|
||||
void safeRemoveAll();
|
||||
void addComment(Long id,Long userId,String text);
|
||||
void removeComment(Long id, Long commentId);
|
||||
List<Comment> getPostComments(Long id);
|
||||
}
|
||||
|
@ -68,4 +68,12 @@ public class PostRepositoryImpl implements PostRepositoryExtension{
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Comment> getPostComments(Long id) {
|
||||
TypedQuery<Comment> query =
|
||||
em.createQuery("Select c from Comment c where post.id = :id",Comment.class)
|
||||
.setParameter("id",id);
|
||||
return query.getResultList();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ import ru.ulstu.is.sbapp.Post.repository.PostRepository;
|
||||
import ru.ulstu.is.sbapp.User.model.User;
|
||||
import ru.ulstu.is.sbapp.Util.validation.ValidatorUtil;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
@ -46,14 +47,20 @@ public class PostService {
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Post updatePost(Long id, String Heading, String Content) {
|
||||
public Post updatePost(Long id, PostDto postDto) {
|
||||
final Post currentPost = findPost(id);
|
||||
currentPost.setHeading(Heading);
|
||||
currentPost.setContent(Content);
|
||||
currentPost.setHeading(postDto.getHeading());
|
||||
currentPost.setContent(postDto.getContent());
|
||||
currentPost.setImage(postDto.getImage().getBytes(StandardCharsets.UTF_8));
|
||||
validatorUtil.validate(currentPost);
|
||||
return postRepository.save(currentPost);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Comment> GetPostComments(Long id)
|
||||
{
|
||||
return postRepository.getPostComments(id);
|
||||
}
|
||||
@Transactional
|
||||
public Post deletePost(Long id) {
|
||||
final Optional<Post> post = postRepository.safeRemove(id);
|
||||
|
Loading…
Reference in New Issue
Block a user