Skip to content

Commit b6a0df7

Browse files
committed
refactor: update user
1 parent 0d696d4 commit b6a0df7

File tree

3 files changed

+37
-20
lines changed

3 files changed

+37
-20
lines changed

src/main/java/com/thisaster/testtask/user/controller/UserController.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ public ResponseEntity<UserDTO> updateUser(@PathVariable Long id, @RequestBody @V
5252
newUser.setRoleId(newUser.getRoleId());
5353
newUser.setSubscriptions(subscriptionMapper.toEntitySet(userDTO.getSubscriptions()));
5454

55-
userService.updateUser(id, newUser);
56-
UserDTO modificationUser = userMapper.toDTO(userService.getUserById(id));
57-
modificationUser.setSubscriptions(userDTO.getSubscriptions());
58-
modificationUser.setRole(userDTO.getRole());
59-
return ResponseEntity.ok(modificationUser);
55+
User modificationUser = userService.updateUser(id, newUser);
56+
UserDTO modificationUserDTO = userMapper.toDTO(modificationUser);
57+
modificationUserDTO.setSubscriptions(subscriptionMapper.toDTOSet(modificationUser.getSubscriptions()));
58+
modificationUserDTO.setRole(userDTO.getRole());
59+
return ResponseEntity.ok(modificationUserDTO);
6060
}
6161

6262
@PostMapping("/{id}/subscriptions")

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.springframework.security.crypto.password.PasswordEncoder;
1313
import org.springframework.stereotype.Service;
1414

15+
import java.util.HashSet;
1516
import java.util.Set;
1617

1718
@Service
@@ -39,37 +40,43 @@ public void createUser(User user) {
3940
}
4041

4142
@Transactional
42-
public void updateUser(Long userId, User user) {
43+
public User updateUser(Long userId, User user) {
4344
User existingUser = userRepository.findById(userId)
4445
.orElseThrow(() -> new EntityNotFoundException("User not found with id: " + userId));
4546

4647
existingUser.setUsername(user.getUsername());
47-
existingUser.setPassword(passwordEncoder.encode(user.getPassword()));
4848
existingUser.setEmail(user.getEmail());
4949

50-
for (Subscription subscription : user.getSubscriptions()) {
51-
Subscription existingSubscription = subscriptionRepository.findByName(subscription.getName())
52-
.orElseGet(() -> {
53-
Subscription newSubscription = new Subscription();
54-
newSubscription.setName(subscription.getName());
55-
return subscriptionRepository.save(newSubscription);
56-
});
57-
58-
if (!existingUser.getSubscriptions().contains(existingSubscription)) {
59-
existingUser.getSubscriptions().add(existingSubscription);
60-
existingSubscription.getUsers().add(existingUser);
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+
Set<Subscription> updatedSubscriptions = new HashSet<>();
56+
if (user.getSubscriptions() != null) {
57+
for (Subscription subscription : user.getSubscriptions()) {
58+
subscribeUserToSub(userId, subscription);
59+
subscriptionRepository.findByName(subscription.getName())
60+
.ifPresent(updatedSubscriptions::add);
6161
}
6262
}
6363

64-
existingUser.setRole(roleService.getByRoleId(user.getRoleId()));
65-
userRepository.save(existingUser);
64+
Set<Subscription> currentSubscriptions = new HashSet<>(existingUser.getSubscriptions());
65+
for (Subscription currentSub : currentSubscriptions) {
66+
if (!updatedSubscriptions.contains(currentSub)) {
67+
removeSubFromUser(userId, currentSub.getId());
68+
}
69+
}
70+
71+
return existingUser;
6672
}
6773

6874
public User getUserById(Long id) {
6975
return userRepository.findById(id)
7076
.orElseThrow(() -> new EntityNotFoundException("User not found with id: " + id));
7177
}
7278

79+
@Transactional
7380
public void subscribeUserToSub(Long userId, Subscription subscription) {
7481
User user = userRepository.findById(userId)
7582
.orElseThrow(() -> new EntityNotFoundException("User not found with id: " + userId));

src/test/java/com/thisaster/testtask/user/controller/UserControllerTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
import com.thisaster.testtask.subscription.repository.SubscriptionRepository;
44
import com.thisaster.testtask.user.repository.UserRepository;
55
import lombok.extern.slf4j.Slf4j;
6+
import org.junit.jupiter.api.MethodOrderer;
7+
import org.junit.jupiter.api.Order;
68
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestMethodOrder;
710
import org.springframework.beans.factory.annotation.Autowired;
811
import org.springframework.beans.factory.annotation.Value;
912
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
@@ -27,6 +30,7 @@
2730
@SpringBootTest
2831
@ActiveProfiles("test")
2932
@AutoConfigureMockMvc
33+
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
3034
@Sql(value = {
3135
"/sql/test.sql"
3236
}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_CLASS)
@@ -46,6 +50,7 @@ public class UserControllerTest {
4650
UserRepository userRepository;
4751

4852
@Test
53+
@Order(1)
4954
void shouldGetUserById() throws Exception {
5055
Long userId = 1L;
5156

@@ -60,6 +65,7 @@ void shouldGetUserById() throws Exception {
6065
}
6166

6267
@Test
68+
@Order(2)
6369
void shouldAddSubscriptionToUser(@Value("classpath:user/createSubscription.json") Resource json) throws Exception {
6470
Long userId = 1L;
6571
mockMvc.perform(post("/api/users/{id}/subscriptions", userId)
@@ -71,6 +77,7 @@ void shouldAddSubscriptionToUser(@Value("classpath:user/createSubscription.json"
7177
}
7278

7379
@Test
80+
@Order(3)
7481
void shouldDeleteUserById() throws Exception {
7582
Long userId = 2L;
7683
mockMvc.perform(delete("/api/users/{id}", userId)
@@ -80,6 +87,7 @@ void shouldDeleteUserById() throws Exception {
8087
}
8188

8289
@Test
90+
@Order(4)
8391
void shouldGetUserSubscriptions() throws Exception {
8492
final Long userId = 3L;
8593

@@ -92,6 +100,7 @@ void shouldGetUserSubscriptions() throws Exception {
92100
}
93101

94102
@Test
103+
@Order(5)
95104
void shouldDeleteUserSubscription() throws Exception {
96105
final Long userId = 3L;
97106
final Long subscriptionId = 3L;
@@ -103,6 +112,7 @@ void shouldDeleteUserSubscription() throws Exception {
103112

104113

105114
@Test
115+
@Order(6)
106116
void shouldUpdateUser(@Value("classpath:user/updateUser.json") Resource json) throws Exception {
107117
Long userId = 3L;
108118
mockMvc.perform(put("/api/users/{userId}", userId)

0 commit comments

Comments
 (0)