浏览代码

original commit

Allison Ziegler 6 年前
父节点
当前提交
3510fbd471

二进制
.DS_Store 查看文件


二进制
src/.DS_Store 查看文件


二进制
src/main/.DS_Store 查看文件


+ 22
- 58
src/main/java/com/ziplinegreen/vault/Controller/PostController.java 查看文件

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

+ 5
- 13
src/main/java/com/ziplinegreen/vault/Controller/UserController.java 查看文件

@@ -3,7 +3,7 @@ package com.ziplinegreen.vault.Controller;
3 3
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
4 4
 import com.ziplinegreen.vault.Model.Post;
5 5
 import com.ziplinegreen.vault.Model.User;
6
-import com.ziplinegreen.vault.Repository.UserRepository;
6
+import com.ziplinegreen.vault.Repository.UsersRepository;
7 7
 import com.ziplinegreen.vault.Service.UserService;
8 8
 import org.springframework.beans.factory.annotation.Autowired;
9 9
 import org.springframework.data.domain.Page;
@@ -27,30 +27,22 @@ public class UserController {
27 27
     }
28 28
 
29 29
     @PostMapping("/users")
30
-    public ResponseEntity<User> createUser(@RequestBody User user) {
30
+    public User createUser(@RequestBody User user) {
31 31
         return userService.createUser(user);
32 32
     }
33 33
 
34 34
     @GetMapping("/users/{userId}")
35
-    public ResponseEntity<User> getUserById(@PathVariable Long userId) {
35
+    public User getUserById(@PathVariable Long userId) {
36 36
         return userService.findUserById(userId);
37 37
     }
38 38
 
39 39
     @PutMapping("/users/{userId}")
40
-    public ResponseEntity<User> updateUsername(@PathVariable Long userId, @Valid @RequestBody User userRequest) {
41
-        return new ResponseEntity<>(userService.updateUsername(userId,userRequest), HttpStatus.OK);
40
+    public User updateUsername(@PathVariable Long userId, @RequestBody String username) {
41
+        return userService.updateUsername(userId, username);
42 42
     }
43 43
 
44
-
45 44
     @DeleteMapping("/users/{userId}")
46 45
     public ResponseEntity<?> deleteUser(@PathVariable Long userId) {
47 46
         return userService.deleteUser(userId);
48 47
     }
49
-
50
-    @PutMapping("/users/{userId}/updatepost")
51
-    public ResponseEntity<?> updatePosts(@PathVariable Long userId, @RequestBody Post post){
52
-        return userService.updatePosts(userId, post);
53
-    }
54
-
55
-
56 48
 }

+ 9
- 2
src/main/java/com/ziplinegreen/vault/Model/Post.java 查看文件

@@ -17,10 +17,8 @@ public class Post extends AuditModel{
17 17
     private Long Id;
18 18
 
19 19
     @NotNull
20
-    @Lob
21 20
     private String message;
22 21
     @OnDelete(action = OnDeleteAction.CASCADE)
23
-    @JsonIgnore
24 22
     private Long userId;
25 23
     private String userName;
26 24
 
@@ -54,4 +52,13 @@ public class Post extends AuditModel{
54 52
         this.userName = userName;
55 53
     }
56 54
 
55
+    @Override
56
+    public String toString() {
57
+        return "Post{" +
58
+                "Id=" + Id +
59
+                ", message='" + message + '\'' +
60
+                ", userId=" + userId +
61
+                ", userName='" + userName + '\'' +
62
+                '}';
63
+    }
57 64
 }

+ 2
- 4
src/main/java/com/ziplinegreen/vault/Model/User.java 查看文件

@@ -19,13 +19,11 @@ public class User extends AuditModel{
19 19
     private String username;
20 20
     private String email;
21 21
     private String password;
22
-    @OneToMany(fetch = FetchType.LAZY,
22
+    @OneToMany(
23 23
             cascade= {
24 24
                 CascadeType.PERSIST,
25 25
                 CascadeType.MERGE
26
-            })
27
-    @JoinTable(name = "user_posts",
28
-            joinColumns = {@JoinColumn(name = "user_id")})
26
+            }, mappedBy = "userId")
29 27
     private List<Post> posts = new ArrayList<>();
30 28
 
31 29
     public List<Post> getPosts() {

+ 0
- 10
src/main/java/com/ziplinegreen/vault/Repository/UserRepository.java 查看文件

@@ -1,10 +0,0 @@
1
-package com.ziplinegreen.vault.Repository;
2
-
3
-import com.ziplinegreen.vault.Model.User;
4
-import org.springframework.data.jpa.repository.JpaRepository;
5
-import org.springframework.data.rest.core.annotation.RepositoryRestResource;
6
-import org.springframework.stereotype.Repository;
7
-
8
-@RepositoryRestResource
9
-public interface UserRepository extends JpaRepository<User,Long> {
10
-}

+ 7
- 0
src/main/java/com/ziplinegreen/vault/Repository/UsersRepository.java 查看文件

@@ -0,0 +1,7 @@
1
+package com.ziplinegreen.vault.Repository;
2
+
3
+import com.ziplinegreen.vault.Model.User;
4
+import org.springframework.data.jpa.repository.JpaRepository;
5
+
6
+public interface UsersRepository extends JpaRepository<User, Long> {
7
+}

+ 24
- 10
src/main/java/com/ziplinegreen/vault/Service/PostService.java 查看文件

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

+ 15
- 21
src/main/java/com/ziplinegreen/vault/Service/UserService.java 查看文件

@@ -4,7 +4,7 @@ import com.ziplinegreen.vault.Controller.PostController;
4 4
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
5 5
 import com.ziplinegreen.vault.Model.Post;
6 6
 import com.ziplinegreen.vault.Model.User;
7
-import com.ziplinegreen.vault.Repository.UserRepository;
7
+import com.ziplinegreen.vault.Repository.UsersRepository;
8 8
 import org.springframework.beans.factory.annotation.Autowired;
9 9
 import org.springframework.http.HttpStatus;
10 10
 import org.springframework.http.ResponseEntity;
@@ -18,40 +18,34 @@ import java.util.List;
18 18
 public class UserService {
19 19
 
20 20
     @Autowired
21
-    private UserRepository userRepository;
21
+    private UsersRepository userRepository;
22
+    private PostService postService;
22 23
     //private PostController postController;
23 24
 
24 25
     @Autowired
25
-    public UserService(UserRepository userRepository) {
26
+    public UserService(UsersRepository userRepository, PostService postService) {
26 27
         this.userRepository = userRepository;
28
+        this.postService = postService;
27 29
     }
28 30
 
29
-    public ResponseEntity<User> createUser(User user){
30
-        return new ResponseEntity<>(userRepository.save(user), HttpStatus.CREATED);
31
+    public User createUser(User user){
32
+        return userRepository.save(user);
31 33
     }
32 34
 
33
-    public ResponseEntity<User> findUserById(Long id){
34
-        return new ResponseEntity<>(userRepository.findById(id).get(),HttpStatus.OK);
35
+    public User findUserById(Long id){
36
+        return userRepository.findById(id).get();
35 37
     }
36 38
 
37
-    public User updateUsername(Long id, User updatedUser){return userRepository.findById(id).map(user -> {
38
-        user.setUsername(updatedUser.getUsername());
39
+    public User updateUsername(Long userId, String username){
40
+        User user = findUserById(userId);
41
+        user.setUsername(username);
39 42
         return userRepository.save(user);
40
-    }).orElseThrow(() -> new ResourceNotFoundException("UserId " + id + " not found"));
41 43
     }
42 44
 
43
-    public ResponseEntity deleteUser(Long id){return userRepository.findById(id).map(user -> {
44
-        userRepository.delete(user);
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
-    }).orElseThrow(() -> new ResourceNotFoundException("UserId " + id + " not found"));
47
-
48
-    }
49
-
50
-    public ResponseEntity<User> updatePosts(Long userId, Post post) {
51
-        User user = findUserById(userId).getBody();
52
-        List<Post> posts = user.getPosts();
53
-        posts.add(post);
54
-        return new ResponseEntity<>(userRepository.save(user), HttpStatus.OK);
55 49
     }
56 50
 }
57 51
 

二进制
src/main/resources/.DS_Store 查看文件


+ 1
- 2
src/main/resources/static/app.js 查看文件

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