|
@@ -2,64 +2,76 @@ package com.ziplinegreen.vault.Controller;
|
2
|
2
|
|
3
|
3
|
import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
|
4
|
4
|
import com.ziplinegreen.vault.Model.Post;
|
|
5
|
+import com.ziplinegreen.vault.Model.User;
|
5
|
6
|
import com.ziplinegreen.vault.Repository.PostRepository;
|
6
|
7
|
import com.ziplinegreen.vault.Repository.UserRepository;
|
|
8
|
+import com.ziplinegreen.vault.Service.PostService;
|
|
9
|
+import com.ziplinegreen.vault.Service.UserService;
|
7
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
8
|
11
|
import org.springframework.data.domain.Page;
|
9
|
12
|
import org.springframework.data.domain.Pageable;
|
|
13
|
+import org.springframework.http.HttpStatus;
|
10
|
14
|
import org.springframework.http.ResponseEntity;
|
11
|
15
|
import org.springframework.web.bind.annotation.*;
|
12
|
16
|
|
13
|
17
|
import javax.validation.Valid;
|
|
18
|
+import java.util.Set;
|
14
|
19
|
|
15
|
20
|
|
16
|
21
|
public class PostController {
|
17
|
22
|
|
18
|
|
- @Autowired
|
19
|
|
- private PostRepository postRepository;
|
|
23
|
+ private PostService postService;
|
|
24
|
+ private UserController userController;
|
20
|
25
|
|
21
|
26
|
@Autowired
|
22
|
|
- private UserRepository userRepository;
|
23
|
|
-
|
24
|
|
- @GetMapping("/users/{userId}/posts")
|
25
|
|
- public Page<Post> getAllPostsByUserId(@PathVariable(value = "userId") Long userId,
|
26
|
|
- Pageable pageable) {
|
27
|
|
- return postRepository.findByUserId(userId, pageable);
|
|
27
|
+ public PostController(PostService postService) {
|
|
28
|
+ this.postService = postService;
|
28
|
29
|
}
|
29
|
30
|
|
30
|
|
- @PostMapping("/users/{userId}/posts")
|
31
|
|
- public Post createPost(@PathVariable (value = "userId") Long userId,
|
32
|
|
- @Valid @RequestBody Post post) {
|
33
|
|
- return userRepository.findById(userId).map(user -> {
|
34
|
|
- post.setUser(user);
|
35
|
|
- return postRepository.save(post);
|
36
|
|
- }).orElseThrow(() -> new ResourceNotFoundException("UserId " + userId + " not found"));
|
|
31
|
+ @PostMapping("/posts/{userId}")
|
|
32
|
+ public ResponseEntity createPost(@PathVariable Long userId, @RequestBody Post post) {
|
|
33
|
+ userController.updatePosts(userId,post);
|
|
34
|
+ return new ResponseEntity(HttpStatus.OK);
|
|
35
|
+
|
37
|
36
|
}
|
38
|
37
|
|
39
|
|
- @PutMapping("/users/{userId}/posts/{postId}")
|
40
|
|
- public Post updateComment(@PathVariable (value = "userId") Long userId,
|
41
|
|
- @PathVariable (value = "postId") Long postId,
|
42
|
|
- @Valid @RequestBody Post postRequest) {
|
43
|
|
- if(!userRepository.existsById(userId)) {
|
44
|
|
- throw new ResourceNotFoundException("UserId " + userId + " not found");
|
45
|
|
- }
|
46
|
38
|
|
47
|
|
- return postRepository.findById(postId).map(post -> {
|
48
|
|
- post.setMessage(postRequest.getMessage());
|
49
|
|
- return postRepository.save(post);
|
50
|
|
- }).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + "not found"));
|
|
39
|
+ @GetMapping("/posts/{userId}")
|
|
40
|
+ public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
|
|
41
|
+ return postService.findByUserId(userId);
|
51
|
42
|
}
|
52
|
43
|
|
53
|
|
- @DeleteMapping("/posts/{postId}/comments/{commentId}")
|
54
|
|
- public ResponseEntity<?> deleteComment(@PathVariable (value = "userId") Long userId,
|
55
|
|
- @PathVariable (value = "postId") Long postId) {
|
56
|
|
- if(!userRepository.existsById(userId)) {
|
57
|
|
- throw new ResourceNotFoundException("UserId " + userId + " not found");
|
58
|
|
- }
|
|
44
|
+// @PutMapping("/users/{userId/posts")
|
|
45
|
+// public ResponseEntity<Post> updatePost(@PathVariable Long userId, @RequestBody Post post){
|
|
46
|
+// return postService.updatePost(userId, post);
|
|
47
|
+// }
|
59
|
48
|
|
60
|
|
- return postRepository.findById(postId).map(post -> {
|
61
|
|
- postRepository.delete(post);
|
62
|
|
- return ResponseEntity.ok().build();
|
63
|
|
- }).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + " not found"));
|
64
|
|
- }
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+// @PutMapping("/users/{userId}/posts/{postId}")
|
|
52
|
+// public Post updateComment(@PathVariable (value = "userId") Long userId,
|
|
53
|
+// @PathVariable (value = "postId") Long postId,
|
|
54
|
+// @Valid @RequestBody Post postRequest) {
|
|
55
|
+// if(!userRepository.existsById(userId)) {
|
|
56
|
+// throw new ResourceNotFoundException("UserId " + userId + " not found");
|
|
57
|
+// }
|
|
58
|
+//
|
|
59
|
+// return postRepository.findById(postId).map(post -> {
|
|
60
|
+// post.setMessage(postRequest.getMessage());
|
|
61
|
+// return postRepository.save(post);
|
|
62
|
+// }).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + "not found"));
|
|
63
|
+// }
|
|
64
|
+//
|
|
65
|
+// @DeleteMapping("/posts/{postId}/comments/{commentId}")
|
|
66
|
+// public ResponseEntity<?> deleteComment(@PathVariable (value = "userId") Long userId,
|
|
67
|
+// @PathVariable (value = "postId") Long postId) {
|
|
68
|
+// if(!userRepository.existsById(userId)) {
|
|
69
|
+// throw new ResourceNotFoundException("UserId " + userId + " not found");
|
|
70
|
+// }
|
|
71
|
+//
|
|
72
|
+// return postRepository.findById(postId).map(post -> {
|
|
73
|
+// postRepository.delete(post);
|
|
74
|
+// return ResponseEntity.ok().build();
|
|
75
|
+// }).orElseThrow(() -> new ResourceNotFoundException("PostId " + postId + " not found"));
|
|
76
|
+// }
|
65
|
77
|
}
|