|
@@ -5,7 +5,7 @@ import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
|
5
|
5
|
import com.ziplinegreen.vault.Model.Post;
|
6
|
6
|
import com.ziplinegreen.vault.Model.User;
|
7
|
7
|
import com.ziplinegreen.vault.Repository.PostRepository;
|
8
|
|
-import com.ziplinegreen.vault.Repository.UserRepository;
|
|
8
|
+import com.ziplinegreen.vault.Repository.UsersRepository;
|
9
|
9
|
import com.ziplinegreen.vault.Service.PostService;
|
10
|
10
|
import com.ziplinegreen.vault.Service.UserService;
|
11
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -22,86 +22,50 @@ import org.springframework.web.bind.annotation.*;
|
22
|
22
|
import javax.validation.Valid;
|
23
|
23
|
import java.util.Set;
|
24
|
24
|
|
25
|
|
-@Controller
|
|
25
|
+@RestController
|
26
|
26
|
public class PostController {
|
27
|
27
|
|
28
|
|
- @Autowired
|
29
|
28
|
private PostService postService;
|
30
|
|
- @Autowired
|
31
|
29
|
private UserController userController;
|
32
|
30
|
|
33
|
|
- @Autowired
|
34
|
|
- private PostRepository repository;
|
35
|
31
|
|
36
|
32
|
@Autowired
|
37
|
|
- public PostController(PostService postService) {
|
|
33
|
+ public PostController(PostService postService, UserController userController) {
|
38
|
34
|
this.postService = postService;
|
|
35
|
+ this.userController = userController;
|
39
|
36
|
}
|
40
|
37
|
|
41
|
|
-// @PostMapping("/posts/{userId}")
|
42
|
|
-// public ResponseEntity createPost(@PathVariable Long userId, @RequestBody Post post) {
|
43
|
|
-// //userController.updatePosts(userId,post);
|
44
|
|
-// return new ResponseEntity(userController.updatePosts(userId, post), HttpStatus.OK);
|
45
|
|
-//
|
46
|
|
-// }
|
47
|
38
|
|
48
|
|
- @MessageMapping("/posts/{userId}")
|
|
39
|
+ @MessageMapping("/posts")
|
49
|
40
|
@SendTo("/topic/posts")
|
50
|
|
- public Post createPost(@DestinationVariable("userId") Long userId, @RequestBody Post post) {
|
51
|
|
- //public Post createPost(@RequestBody Post post) {
|
52
|
|
- System.out.println(post.getMessage());
|
53
|
|
- post.setUserName(userController.getUserById(userId).getBody().getUsername());
|
54
|
|
- //return userController.updatePosts(userId,post);
|
55
|
|
- return repository.save(post);
|
56
|
|
- //return post;
|
57
|
|
- //return new ResponseEntity(userController.updatePosts(userId, post), HttpStatus.OK);
|
58
|
|
-
|
|
41
|
+ public Post createPost(@RequestBody Post post) {
|
|
42
|
+ post.setUserName(userController.getUserById(post.getUserId()).getUsername());
|
|
43
|
+ return postService.createPost(post);
|
59
|
44
|
}
|
60
|
45
|
|
61
|
46
|
@GetMapping("/posts/all")
|
62
|
47
|
public Iterable<Post> getAllPosts() {
|
63
|
|
- return repository.findAll();
|
|
48
|
+ return postService.getAllPosts();
|
64
|
49
|
}
|
65
|
50
|
|
66
|
|
-
|
67
|
|
-
|
68
|
51
|
@GetMapping("/posts/{userId}")
|
69
|
|
- //@SendTo("/topic/posts")
|
70
|
|
- public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
|
|
52
|
+ public Iterable<Post> getAllPostsByUserId(@PathVariable Long userId) {
|
71
|
53
|
return postService.findByUserId(userId);
|
72
|
54
|
}
|
73
|
55
|
|
74
|
|
-// @PutMapping("/users/{userId/posts")
|
75
|
|
-// public ResponseEntity<Post> updatePost(@PathVariable Long userId, @RequestBody Post post){
|
76
|
|
-// return postService.updatePost(userId, post);
|
77
|
|
-// }
|
|
56
|
+ @DeleteMapping("/posts/delete")
|
|
57
|
+ public ResponseEntity deletePost(@RequestBody Long postId) {
|
|
58
|
+ return postService.deletePost(postId);
|
|
59
|
+ }
|
78
|
60
|
|
|
61
|
+ @DeleteMapping("/posts/deleteAll")
|
|
62
|
+ public ResponseEntity deleteAllPosts(@RequestBody Long userId) {
|
|
63
|
+ return postService.deleteAllPosts(userId);
|
|
64
|
+ }
|
79
|
65
|
|
|
66
|
+ @PutMapping("/posts/update/{postId}")
|
|
67
|
+ public Post updatePost(@PathVariable Long postId, @RequestBody String message) {
|
|
68
|
+ return postService.updatePost(postId,message);
|
|
69
|
+ }
|
80
|
70
|
|
81
|
|
-// @PutMapping("/users/{userId}/posts/{postId}")
|
82
|
|
-// public Post updateComment(@PathVariable (value = "userId") Long userId,
|
83
|
|
-// @PathVariable (value = "postId") Long postId,
|
84
|
|
-// @Valid @RequestBody Post postRequest) {
|
85
|
|
-// if(!userRepository.existsById(userId)) {
|
86
|
|
-// throw new ResourceNotFoundException("UserId " + userId + " not found");
|
87
|
|
-// }
|
88
|
|
-//
|
89
|
|
-// return postRepository.findById(postId).map(post -> {
|
90
|
|
-// post.setMessage(postRequest.getMessage());
|
91
|
|
-// return postRepository.save(post);
|
92
|
|
-// }).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + "not found"));
|
93
|
|
-// }
|
94
|
|
-//
|
95
|
|
-// @DeleteMapping("/posts/{postId}/comments/{commentId}")
|
96
|
|
-// public ResponseEntity<?> deleteComment(@PathVariable (value = "userId") Long userId,
|
97
|
|
-// @PathVariable (value = "postId") Long postId) {
|
98
|
|
-// if(!userRepository.existsById(userId)) {
|
99
|
|
-// throw new ResourceNotFoundException("UserId " + userId + " not found");
|
100
|
|
-// }
|
101
|
|
-//
|
102
|
|
-// return postRepository.findById(postId).map(post -> {
|
103
|
|
-// postRepository.delete(post);
|
104
|
|
-// return ResponseEntity.ok().build();
|
105
|
|
-// }).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + " not found"));
|
106
|
|
-// }
|
107
|
71
|
}
|