Bläddra i källkod

updated users

Rachelle 6 år sedan
förälder
incheckning
db2fe97804

Binär
.DS_Store Visa fil


Binär
src/.DS_Store Visa fil


Binär
src/main/.DS_Store Visa fil


+ 12
- 21
src/main/java/com/ziplinegreen/vault/Controller/PostController.java Visa fil

@@ -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,40 +22,32 @@ 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
-
33
-    @Autowired
34 30
     private PostRepository repository;
35 31
 
36 32
     @Autowired
37
-    public PostController(PostService postService) {
33
+    public PostController(PostService postService, UserController userController, PostRepository repository) {
38 34
         this.postService = postService;
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
+
39 43
     }
40 44
 
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 45
 
48 46
     @MessageMapping("/posts/{userId}")
49
-    //@MessageMapping("/posts")
50 47
     @SendTo("/topic/posts")
51 48
     public Post createPost(@DestinationVariable("userId") Long userId, @RequestBody Post post) {
52
-    //public Post createPost(@RequestBody Post post) {
53
-        System.out.println(post.getMessage());
54
-        post.setUserName(userController.getUserById(userId).getBody().getUsername());
55
-        //return userController.updatePosts(userId,post);
49
+        post.setUserName(userController.getUserById(userId).getUsername());
56 50
         return repository.save(post);
57
-        //return post;
58
-        //return new ResponseEntity(userController.updatePosts(userId, post), HttpStatus.OK);
59 51
 
60 52
     }
61 53
 
@@ -67,7 +59,6 @@ public class PostController {
67 59
 
68 60
 
69 61
     @GetMapping("/posts/{userId}")
70
-    //@SendTo("/topic/posts")
71 62
     public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
72 63
         return postService.findByUserId(userId);
73 64
     }

+ 12
- 10
src/main/java/com/ziplinegreen/vault/Controller/UserController.java Visa fil

@@ -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,32 @@ 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 48
 
50
-    @PutMapping("/users/{userId}/updatepost")
51
-    public ResponseEntity<?> updatePosts(@PathVariable Long userId, @RequestBody Post post){
52
-        return userService.updatePosts(userId, post);
53
-    }
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
+//    }
54 56
 
55 57
 
56 58
 }

+ 0
- 2
src/main/java/com/ziplinegreen/vault/Model/Post.java Visa fil

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

+ 2
- 4
src/main/java/com/ziplinegreen/vault/Model/User.java Visa fil

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

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

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

+ 18
- 20
src/main/java/com/ziplinegreen/vault/Service/UserService.java Visa fil

@@ -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,41 +18,39 @@ import java.util.List;
18 18
 public class UserService {
19 19
 
20 20
     @Autowired
21
-    private UserRepository userRepository;
21
+    private UsersRepository userRepository;
22 22
     //private PostController postController;
23 23
 
24 24
     @Autowired
25
-    public UserService(UserRepository userRepository) {
25
+    public UserService(UsersRepository userRepository) {
26 26
         this.userRepository = userRepository;
27 27
     }
28 28
 
29
-    public ResponseEntity<User> createUser(User user){
30
-        return new ResponseEntity<>(userRepository.save(user), HttpStatus.CREATED);
29
+    public User createUser(User user){
30
+        return userRepository.save(user);
31 31
     }
32 32
 
33
-    public ResponseEntity<User> findUserById(Long id){
34
-        return new ResponseEntity<>(userRepository.findById(id).get(),HttpStatus.OK);
33
+    public User findUserById(Long id){
34
+        return userRepository.findById(id).get();
35 35
     }
36 36
 
37
-    public User updateUsername(Long id, User updatedUser){return userRepository.findById(id).map(user -> {
38
-        user.setUsername(updatedUser.getUsername());
37
+    public User updateUsername(Long userId, String username){
38
+        User user = findUserById(userId);
39
+        user.setUsername(username);
39 40
         return userRepository.save(user);
40
-    }).orElseThrow(() -> new ResourceNotFoundException("UserId " + id + " not found"));
41 41
     }
42 42
 
43
-    public ResponseEntity deleteUser(Long id){return userRepository.findById(id).map(user -> {
44
-        userRepository.delete(user);
43
+    public ResponseEntity deleteUser(Long id){
44
+        userRepository.delete(userRepository.findById(id).get());
45 45
         return new ResponseEntity(HttpStatus.OK);
46
-    }).orElseThrow(() -> new ResourceNotFoundException("UserId " + id + " not found"));
47
-
48 46
     }
49 47
 
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
-    }
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
+//    }
56 54
 }
57 55
 
58 56
 

Binär
src/main/resources/.DS_Store Visa fil