Skip to content

Commit 61eeee2

Browse files
committed
refactor: update user
decomposition method
1 parent ba17f2b commit 61eeee2

File tree

1 file changed

+40
-32
lines changed

1 file changed

+40
-32
lines changed

src/main/java/com/thisaster/testtask/user/service/UserService.java

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import java.util.HashSet;
1616
import java.util.Set;
17+
import java.util.stream.Collectors;
1718

1819
@Service
1920
@RequiredArgsConstructor
@@ -31,6 +32,11 @@ public User getUserByLogin(String login) {
3132
return user;
3233
}
3334

35+
public User getUserById(Long id) {
36+
return userRepository.findById(id)
37+
.orElseThrow(() -> new EntityNotFoundException("User not found with id: " + id));
38+
}
39+
3440
@Transactional
3541
public void createUser(User user) {
3642
if (userRepository.findByUsername(user.getUsername()).isPresent()) {
@@ -44,42 +50,12 @@ public User updateUser(Long userId, User user) {
4450
User existingUser = userRepository.findById(userId)
4551
.orElseThrow(() -> new EntityNotFoundException("User not found with id: " + userId));
4652

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());
7455

7556
return existingUser;
7657
}
7758

78-
public User getUserById(Long id) {
79-
return userRepository.findById(id)
80-
.orElseThrow(() -> new EntityNotFoundException("User not found with id: " + id));
81-
}
82-
8359
@Transactional
8460
public void subscribeUserToSub(Long userId, Subscription subscription) {
8561
User user = userRepository.findById(userId)
@@ -130,4 +106,36 @@ public void deleteUser(Long userId) {
130106
userRepository.deleteById(userId);
131107
}
132108

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

0 commit comments

Comments
 (0)