14
14
15
15
import java .util .HashSet ;
16
16
import java .util .Set ;
17
+ import java .util .stream .Collectors ;
17
18
18
19
@ Service
19
20
@ RequiredArgsConstructor
@@ -31,6 +32,11 @@ public User getUserByLogin(String login) {
31
32
return user ;
32
33
}
33
34
35
+ public User getUserById (Long id ) {
36
+ return userRepository .findById (id )
37
+ .orElseThrow (() -> new EntityNotFoundException ("User not found with id: " + id ));
38
+ }
39
+
34
40
@ Transactional
35
41
public void createUser (User user ) {
36
42
if (userRepository .findByUsername (user .getUsername ()).isPresent ()) {
@@ -44,42 +50,12 @@ public User updateUser(Long userId, User user) {
44
50
User existingUser = userRepository .findById (userId )
45
51
.orElseThrow (() -> new EntityNotFoundException ("User not found with id: " + userId ));
46
52
47
- existingUser .setUsername (user .getUsername ());
48
- existingUser .setEmail (user .getEmail ());
49
-
50
- if (user .getPassword () != null && !user .getPassword ().isBlank ()) {
51
- existingUser .setPassword (passwordEncoder .encode (user .getPassword ()));
52
- }
53
- existingUser .setRole (roleService .getByRoleId (user .getRoleId ()));
54
-
55
-
56
- Set <Subscription > currentSubscriptions = new HashSet <>(existingUser .getSubscriptions ());
57
- if (user .getSubscriptions () != null ) {
58
- for (Subscription newSub : user .getSubscriptions ()) {
59
- boolean alreadySubscribed = currentSubscriptions .stream ()
60
- .anyMatch (existingSub -> existingSub .getName ().equals (newSub .getName ()));
61
-
62
- if (!alreadySubscribed ) {
63
- subscribeUserToSub (userId , newSub );
64
- }
65
- }
66
- }
67
-
68
- Set <Subscription > updatedSubscriptions = existingUser .getSubscriptions ();
69
- for (Subscription currentSub : currentSubscriptions ) {
70
- if (!updatedSubscriptions .contains (currentSub )) {
71
- removeSubFromUser (userId , currentSub .getId ());
72
- }
73
- }
53
+ updateUserDetails (existingUser , user );
54
+ updateUserSubscriptions (existingUser , user .getSubscriptions ());
74
55
75
56
return existingUser ;
76
57
}
77
58
78
- public User getUserById (Long id ) {
79
- return userRepository .findById (id )
80
- .orElseThrow (() -> new EntityNotFoundException ("User not found with id: " + id ));
81
- }
82
-
83
59
@ Transactional
84
60
public void subscribeUserToSub (Long userId , Subscription subscription ) {
85
61
User user = userRepository .findById (userId )
@@ -130,4 +106,36 @@ public void deleteUser(Long userId) {
130
106
userRepository .deleteById (userId );
131
107
}
132
108
109
+ private void updateUserDetails (User existingUser , User newUser ) {
110
+ existingUser .setUsername (newUser .getUsername ());
111
+ existingUser .setEmail (newUser .getEmail ());
112
+
113
+ if (newUser .getPassword () != null && !newUser .getPassword ().isBlank ()) {
114
+ existingUser .setPassword (passwordEncoder .encode (newUser .getPassword ()));
115
+ }
116
+
117
+ existingUser .setRole (roleService .getByRoleId (newUser .getRoleId ()));
118
+ }
119
+
120
+ @ Transactional
121
+ protected void updateUserSubscriptions (User user , Set <Subscription > newSubscriptions ) {
122
+ Set <Subscription > current = new HashSet <>(user .getSubscriptions ());
123
+ Set <String > newNames = newSubscriptions == null ? Set .of () :
124
+ newSubscriptions .stream ().map (Subscription ::getName ).collect (Collectors .toSet ());
125
+
126
+ if (newSubscriptions != null ) {
127
+ for (Subscription sub : newSubscriptions ) {
128
+ if (current .stream ().noneMatch (s -> s .getName ().equals (sub .getName ()))) {
129
+ subscribeUserToSub (user .getId (), sub );
130
+ }
131
+ }
132
+ }
133
+
134
+ for (Subscription existing : current ) {
135
+ if (!newNames .contains (existing .getName ())) {
136
+ removeSubFromUser (user .getId (), existing .getId ());
137
+ }
138
+ }
139
+ }
140
+
133
141
}
0 commit comments