|
@@ -27,73 +27,44 @@ public class PostController {
|
27
|
27
|
|
28
|
28
|
private PostService postService;
|
29
|
29
|
private UserController userController;
|
30
|
|
- private PostRepository repository;
|
|
30
|
+
|
31
|
31
|
|
32
|
32
|
@Autowired
|
33
|
|
- public PostController(PostService postService, UserController userController, PostRepository repository) {
|
|
33
|
+ public PostController(PostService postService, UserController userController) {
|
34
|
34
|
this.postService = postService;
|
35
|
35
|
this.userController = userController;
|
36
|
|
- this.repository = repository;
|
37
|
|
- }
|
38
|
|
-
|
39
|
|
- @PostMapping("/posts")
|
40
|
|
- public ResponseEntity createPost(@RequestBody Post post) {
|
41
|
|
- return new ResponseEntity(repository.save(post), HttpStatus.CREATED);
|
42
|
|
-
|
43
|
36
|
}
|
44
|
37
|
|
45
|
|
-
|
46
|
|
- @MessageMapping("/posts/{userId}")
|
|
38
|
+ @MessageMapping("/posts")
|
47
|
39
|
@SendTo("/topic/posts")
|
48
|
|
- public Post createPost(@DestinationVariable("userId") Long userId, @RequestBody Post post) {
|
49
|
|
- post.setUserName(userController.getUserById(userId).getUsername());
|
50
|
|
- return repository.save(post);
|
51
|
|
-
|
|
40
|
+ public Post createPost(@RequestBody Post post) {
|
|
41
|
+ post.setUserName(userController.getUserById(post.getUserId()).getUsername());
|
|
42
|
+ return postService.createPost(post);
|
52
|
43
|
}
|
53
|
44
|
|
54
|
45
|
@GetMapping("/posts/all")
|
55
|
46
|
public Iterable<Post> getAllPosts() {
|
56
|
|
- return repository.findAll();
|
|
47
|
+ return postService.getAllPosts();
|
57
|
48
|
}
|
58
|
49
|
|
59
|
|
-
|
60
|
|
-
|
61
|
50
|
@GetMapping("/posts/{userId}")
|
62
|
|
- public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
|
|
51
|
+ public Iterable<Post> getAllPostsByUserId(@PathVariable Long userId) {
|
63
|
52
|
return postService.findByUserId(userId);
|
64
|
53
|
}
|
65
|
54
|
|
66
|
|
-// @PutMapping("/users/{userId/posts")
|
67
|
|
-// public ResponseEntity<Post> updatePost(@PathVariable Long userId, @RequestBody Post post){
|
68
|
|
-// return postService.updatePost(userId, post);
|
69
|
|
-// }
|
|
55
|
+ @DeleteMapping("/posts/delete")
|
|
56
|
+ public ResponseEntity deletePost(@RequestBody Long postId) {
|
|
57
|
+ return postService.deletePost(postId);
|
|
58
|
+ }
|
70
|
59
|
|
|
60
|
+ @DeleteMapping("/posts/deleteAll")
|
|
61
|
+ public ResponseEntity deleteAllPosts(@RequestBody Long userId) {
|
|
62
|
+ return postService.deleteAllPosts(userId);
|
|
63
|
+ }
|
71
|
64
|
|
|
65
|
+ @PutMapping("/posts/update/{postId}")
|
|
66
|
+ public Post updatePost(@PathVariable Long postId, @RequestBody String message) {
|
|
67
|
+ return postService.updatePost(postId,message);
|
|
68
|
+ }
|
72
|
69
|
|
73
|
|
-// @PutMapping("/users/{userId}/posts/{postId}")
|
74
|
|
-// public Post updateComment(@PathVariable (value = "userId") Long userId,
|
75
|
|
-// @PathVariable (value = "postId") Long postId,
|
76
|
|
-// @Valid @RequestBody Post postRequest) {
|
77
|
|
-// if(!userRepository.existsById(userId)) {
|
78
|
|
-// throw new ResourceNotFoundException("UserId " + userId + " not found");
|
79
|
|
-// }
|
80
|
|
-//
|
81
|
|
-// return postRepository.findById(postId).map(post -> {
|
82
|
|
-// post.setMessage(postRequest.getMessage());
|
83
|
|
-// return postRepository.save(post);
|
84
|
|
-// }).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + "not found"));
|
85
|
|
-// }
|
86
|
|
-//
|
87
|
|
-// @DeleteMapping("/posts/{postId}/comments/{commentId}")
|
88
|
|
-// public ResponseEntity<?> deleteComment(@PathVariable (value = "userId") Long userId,
|
89
|
|
-// @PathVariable (value = "postId") Long postId) {
|
90
|
|
-// if(!userRepository.existsById(userId)) {
|
91
|
|
-// throw new ResourceNotFoundException("UserId " + userId + " not found");
|
92
|
|
-// }
|
93
|
|
-//
|
94
|
|
-// return postRepository.findById(postId).map(post -> {
|
95
|
|
-// postRepository.delete(post);
|
96
|
|
-// return ResponseEntity.ok().build();
|
97
|
|
-// }).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + " not found"));
|
98
|
|
-// }
|
99
|
70
|
}
|