Procházet zdrojové kódy

update posts, delete posts

Rachelle před 6 roky
rodič
revize
8c9412d8c1

+ 20
- 49
src/main/java/com/ziplinegreen/vault/Controller/PostController.java Zobrazit soubor

@@ -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
 }

+ 1
- 11
src/main/java/com/ziplinegreen/vault/Controller/UserController.java Zobrazit soubor

@@ -38,21 +38,11 @@ public class UserController {
38 38
 
39 39
     @PutMapping("/users/{userId}")
40 40
     public User updateUsername(@PathVariable Long userId, @RequestBody String username) {
41
-        return userService.updateUsername(userId,username);
41
+        return userService.updateUsername(userId, username);
42 42
     }
43 43
 
44 44
     @DeleteMapping("/users/{userId}")
45 45
     public ResponseEntity<?> deleteUser(@PathVariable Long userId) {
46 46
         return userService.deleteUser(userId);
47 47
     }
48
-
49
-
50
-
51
-
52
-//    @PutMapping("/users/{userId}/updatepost")
53
-//    public ResponseEntity<?> updatePosts(@PathVariable Long userId, @RequestBody Post post){
54
-//        return userService.updatePosts(userId, post);
55
-//    }
56
-
57
-
58 48
 }

+ 9
- 0
src/main/java/com/ziplinegreen/vault/Model/Post.java Zobrazit soubor

@@ -52,4 +52,13 @@ public class Post extends AuditModel{
52 52
         this.userName = userName;
53 53
     }
54 54
 
55
+    @Override
56
+    public String toString() {
57
+        return "Post{" +
58
+                "Id=" + Id +
59
+                ", message='" + message + '\'' +
60
+                ", userId=" + userId +
61
+                ", userName='" + userName + '\'' +
62
+                '}';
63
+    }
55 64
 }

+ 24
- 10
src/main/java/com/ziplinegreen/vault/Service/PostService.java Zobrazit soubor

@@ -19,22 +19,36 @@ public class PostService {
19 19
         this.postRepository = postRepository;
20 20
     }
21 21
 
22
-    public ResponseEntity<Post> createPost(Post post){
23
-        return new ResponseEntity<>(postRepository.save(post), HttpStatus.CREATED);
22
+    public Post createPost(Post post){
23
+        return postRepository.save(post);
24 24
     }
25 25
 
26
-    public ResponseEntity<Post> deletePost(Long id){
27
-        Post post = postRepository.getOne(id);
28
-        postRepository.delete(post);
26
+    public Iterable<Post> getAllPosts(){
27
+        return postRepository.findAll();
28
+    }
29
+
30
+    public ResponseEntity deletePost(Long postId){
31
+        postRepository.delete(postRepository.findById(postId).get());
29 32
         return new ResponseEntity<>(HttpStatus.OK);
30 33
     }
31 34
 
32
-    public ResponseEntity<Iterable<Post>> findByUserId(Long userId) {
33
-        return new ResponseEntity<>(postRepository.findByUserId(userId), HttpStatus.OK);
35
+    public ResponseEntity deleteAllPosts(Long userId){
36
+        Iterable<Post> posts = findByUserId(userId);
37
+        for(Post post: posts){
38
+            deletePost(post.getId());
39
+        }
40
+        return new ResponseEntity<>(HttpStatus.OK);
41
+    }
42
+
43
+    public Iterable<Post> findByUserId(Long userId) {
44
+        return postRepository.findByUserId(userId);
45
+    }
46
+
47
+    public Post updatePost(Long postId, String message){
48
+        Post post = postRepository.findById(postId).get();
49
+        post.setMessage(message);
50
+        return postRepository.save(post);
34 51
     }
35 52
 
36
-//    public ResponseEntity<Post> updatePost(Long userId, Post post) {
37
-//
38
-//    }
39 53
 }
40 54
 

+ 6
- 10
src/main/java/com/ziplinegreen/vault/Service/UserService.java Zobrazit soubor

@@ -19,11 +19,13 @@ public class UserService {
19 19
 
20 20
     @Autowired
21 21
     private UsersRepository userRepository;
22
+    private PostService postService;
22 23
     //private PostController postController;
23 24
 
24 25
     @Autowired
25
-    public UserService(UsersRepository userRepository) {
26
+    public UserService(UsersRepository userRepository, PostService postService) {
26 27
         this.userRepository = userRepository;
28
+        this.postService = postService;
27 29
     }
28 30
 
29 31
     public User createUser(User user){
@@ -40,17 +42,11 @@ public class UserService {
40 42
         return userRepository.save(user);
41 43
     }
42 44
 
43
-    public ResponseEntity deleteUser(Long id){
44
-        userRepository.delete(userRepository.findById(id).get());
45
+    public ResponseEntity deleteUser(Long userId){
46
+        postService.deleteAllPosts(userId);
47
+        userRepository.delete(userRepository.findById(userId).get());
45 48
         return new ResponseEntity(HttpStatus.OK);
46 49
     }
47
-
48
-//    public ResponseEntity<User> updatePosts(Long userId, Post post) {
49
-//        User user = findUserById(userId).getBody();
50
-//        List<Post> posts = user.getPosts();
51
-//        posts.add(post);
52
-//        return new ResponseEntity<>(userRepository.save(user), HttpStatus.OK);
53
-//    }
54 50
 }
55 51
 
56 52
 

+ 1
- 2
src/main/resources/static/app.js Zobrazit soubor

@@ -34,7 +34,7 @@ function disconnect() {
34 34
 }
35 35
 
36 36
 var userId = 1;
37
-var userPostUrl = "/app/posts/1";
37
+var userPostUrl = "/app/posts";
38 38
 
39 39
 function user1() {
40 40
     $('#user1').prop("disabled", true);
@@ -46,7 +46,6 @@ function user2() {
46 46
     $('#user1').prop("disabled", false);
47 47
     $('#user2').prop("disabled", true);
48 48
     userId = 2;
49
-    userPostUrl = "/app/posts/2";
50 49
 }
51 50
 
52 51
 // function sendName() {