4 lab CommentController

This commit is contained in:
Павел Сорокин 2023-04-04 13:47:11 +04:00
parent 419eb0b15a
commit 6a071071e2
2 changed files with 40 additions and 3 deletions

View File

@ -1,5 +1,41 @@
package ru.ulstu.is.sbapp.Comment.controller;
public class CommentController {
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")
public class CommentController {
private final CommentService commentService;
public CommentController(CommentService commentService) {
this.commentService = commentService;
}
@GetMapping("/{id}")
public CommentDto getComment(@PathVariable Long id) {
return new CommentDto(commentService.findComment(id));
}
@GetMapping
public List<CommentDto> getComments() {
return commentService.findAllComments().stream()
.map(CommentDto::new)
.toList();
}
@PostMapping
public CommentDto createComment(@RequestParam("Text") String Text){
return new CommentDto(commentService.addComment(Text));
}
@PutMapping("/{id}")
public CommentDto updateComment(@PathVariable Long id,
@RequestParam("Text") String Text){
return new CommentDto(commentService.updateComment(id,Text));
}
@DeleteMapping("/{id}")
public CommentDto deleteComment(@PathVariable Long id) {
return new CommentDto(commentService.deleteComment(id));
}
}

View File

@ -1,5 +1,6 @@
package ru.ulstu.is.sbapp.Comment.controller;
import ru.ulstu.is.sbapp.Comment.model.Comment;
import ru.ulstu.is.sbapp.Post.model.Post;
import ru.ulstu.is.sbapp.User.model.User;
@ -8,9 +9,9 @@ public class CommentDto {
private String Text;
private Post post;
private User user;
public CommentDto(String text)
public CommentDto(Comment comment)
{
this.Text=text;
this.Text=comment.getText();
}
public Long getId()
{