Selaa lähdekoodia

updated users

Rachelle 6 vuotta sitten
vanhempi
commit
db2fe97804

BIN
.DS_Store Näytä tiedosto


BIN
src/.DS_Store Näytä tiedosto


BIN
src/main/.DS_Store Näytä tiedosto


+ 12
- 21
src/main/java/com/ziplinegreen/vault/Controller/PostController.java Näytä tiedosto

5
 import com.ziplinegreen.vault.Model.Post;
5
 import com.ziplinegreen.vault.Model.Post;
6
 import com.ziplinegreen.vault.Model.User;
6
 import com.ziplinegreen.vault.Model.User;
7
 import com.ziplinegreen.vault.Repository.PostRepository;
7
 import com.ziplinegreen.vault.Repository.PostRepository;
8
-import com.ziplinegreen.vault.Repository.UserRepository;
8
+import com.ziplinegreen.vault.Repository.UsersRepository;
9
 import com.ziplinegreen.vault.Service.PostService;
9
 import com.ziplinegreen.vault.Service.PostService;
10
 import com.ziplinegreen.vault.Service.UserService;
10
 import com.ziplinegreen.vault.Service.UserService;
11
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.beans.factory.annotation.Autowired;
22
 import javax.validation.Valid;
22
 import javax.validation.Valid;
23
 import java.util.Set;
23
 import java.util.Set;
24
 
24
 
25
-@Controller
25
+@RestController
26
 public class PostController {
26
 public class PostController {
27
 
27
 
28
-    @Autowired
29
     private PostService postService;
28
     private PostService postService;
30
-    @Autowired
31
     private UserController userController;
29
     private UserController userController;
32
-
33
-    @Autowired
34
     private PostRepository repository;
30
     private PostRepository repository;
35
 
31
 
36
     @Autowired
32
     @Autowired
37
-    public PostController(PostService postService) {
33
+    public PostController(PostService postService, UserController userController, PostRepository repository) {
38
         this.postService = postService;
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
     @MessageMapping("/posts/{userId}")
46
     @MessageMapping("/posts/{userId}")
49
-    //@MessageMapping("/posts")
50
     @SendTo("/topic/posts")
47
     @SendTo("/topic/posts")
51
     public Post createPost(@DestinationVariable("userId") Long userId, @RequestBody Post post) {
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
         return repository.save(post);
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
 
59
 
68
 
60
 
69
     @GetMapping("/posts/{userId}")
61
     @GetMapping("/posts/{userId}")
70
-    //@SendTo("/topic/posts")
71
     public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
62
     public ResponseEntity<Iterable<Post>> getAllPostsByUserId(@PathVariable Long userId) {
72
         return postService.findByUserId(userId);
63
         return postService.findByUserId(userId);
73
     }
64
     }

+ 12
- 10
src/main/java/com/ziplinegreen/vault/Controller/UserController.java Näytä tiedosto

3
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
3
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
4
 import com.ziplinegreen.vault.Model.Post;
4
 import com.ziplinegreen.vault.Model.Post;
5
 import com.ziplinegreen.vault.Model.User;
5
 import com.ziplinegreen.vault.Model.User;
6
-import com.ziplinegreen.vault.Repository.UserRepository;
6
+import com.ziplinegreen.vault.Repository.UsersRepository;
7
 import com.ziplinegreen.vault.Service.UserService;
7
 import com.ziplinegreen.vault.Service.UserService;
8
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.beans.factory.annotation.Autowired;
9
 import org.springframework.data.domain.Page;
9
 import org.springframework.data.domain.Page;
27
     }
27
     }
28
 
28
 
29
     @PostMapping("/users")
29
     @PostMapping("/users")
30
-    public ResponseEntity<User> createUser(@RequestBody User user) {
30
+    public User createUser(@RequestBody User user) {
31
         return userService.createUser(user);
31
         return userService.createUser(user);
32
     }
32
     }
33
 
33
 
34
     @GetMapping("/users/{userId}")
34
     @GetMapping("/users/{userId}")
35
-    public ResponseEntity<User> getUserById(@PathVariable Long userId) {
35
+    public User getUserById(@PathVariable Long userId) {
36
         return userService.findUserById(userId);
36
         return userService.findUserById(userId);
37
     }
37
     }
38
 
38
 
39
     @PutMapping("/users/{userId}")
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
     @DeleteMapping("/users/{userId}")
44
     @DeleteMapping("/users/{userId}")
46
     public ResponseEntity<?> deleteUser(@PathVariable Long userId) {
45
     public ResponseEntity<?> deleteUser(@PathVariable Long userId) {
47
         return userService.deleteUser(userId);
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 Näytä tiedosto

17
     private Long Id;
17
     private Long Id;
18
 
18
 
19
     @NotNull
19
     @NotNull
20
-    @Lob
21
     private String message;
20
     private String message;
22
     @OnDelete(action = OnDeleteAction.CASCADE)
21
     @OnDelete(action = OnDeleteAction.CASCADE)
23
-    @JsonIgnore
24
     private Long userId;
22
     private Long userId;
25
     private String userName;
23
     private String userName;
26
 
24
 

+ 2
- 4
src/main/java/com/ziplinegreen/vault/Model/User.java Näytä tiedosto

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

+ 0
- 10
src/main/java/com/ziplinegreen/vault/Repository/UserRepository.java Näytä tiedosto

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 Näytä tiedosto

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 Näytä tiedosto

4
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
4
 import com.ziplinegreen.vault.Exception.ResourceNotFoundException;
5
 import com.ziplinegreen.vault.Model.Post;
5
 import com.ziplinegreen.vault.Model.Post;
6
 import com.ziplinegreen.vault.Model.User;
6
 import com.ziplinegreen.vault.Model.User;
7
-import com.ziplinegreen.vault.Repository.UserRepository;
7
+import com.ziplinegreen.vault.Repository.UsersRepository;
8
 import org.springframework.beans.factory.annotation.Autowired;
8
 import org.springframework.beans.factory.annotation.Autowired;
9
 import org.springframework.http.HttpStatus;
9
 import org.springframework.http.HttpStatus;
10
 import org.springframework.http.ResponseEntity;
10
 import org.springframework.http.ResponseEntity;
18
 public class UserService {
18
 public class UserService {
19
 
19
 
20
     @Autowired
20
     @Autowired
21
-    private UserRepository userRepository;
21
+    private UsersRepository userRepository;
22
     //private PostController postController;
22
     //private PostController postController;
23
 
23
 
24
     @Autowired
24
     @Autowired
25
-    public UserService(UserRepository userRepository) {
25
+    public UserService(UsersRepository userRepository) {
26
         this.userRepository = userRepository;
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
         return userRepository.save(user);
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
         return new ResponseEntity(HttpStatus.OK);
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
src/main/resources/.DS_Store Näytä tiedosto