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