-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.java
More file actions
140 lines (127 loc) · 5.8 KB
/
UserService.java
File metadata and controls
140 lines (127 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.podzilla.auth.service;
import com.podzilla.auth.dto.CustomUserDetails;
import com.podzilla.auth.dto.DeliveryAddress;
import com.podzilla.auth.dto.UpdateRequest;
import com.podzilla.auth.dto.UserDetailsRequest;
import com.podzilla.auth.exception.NotFoundException;
import com.podzilla.auth.exception.ValidationException;
import com.podzilla.auth.model.Address;
import com.podzilla.auth.model.User;
import com.podzilla.auth.repository.AddressRepository;
import com.podzilla.auth.repository.UserRepository;
import jakarta.transaction.Transactional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.UUID;
@Service
public class UserService {
private static final Logger LOGGER =
LoggerFactory.getLogger(UserService.class);
private final UserRepository userRepository;
private final AddressRepository addressRepository;
public UserService(final UserRepository userRepository,
final AddressRepository addressRepository) {
this.userRepository = userRepository;
this.addressRepository = addressRepository;
}
@Transactional
public void updateUserProfile(final UpdateRequest updateRequest) {
LOGGER.debug("Updating user profile");
CustomUserDetails customUserDetails =
AuthenticationService.getCurrentUserDetails();
if (updateRequest.getName() != null
&& !updateRequest.getName().isBlank()) {
User user = getUserOrThrow(customUserDetails.getId());
LOGGER.debug("Updating user with id={}", user.getId());
user.setName(updateRequest.getName());
userRepository.save(user);
}
if (updateRequest.getMobileNumber() != null
&& !updateRequest.getMobileNumber().isBlank()
&& mobileNumberIsUnique(updateRequest.getMobileNumber())) {
User user = getUserOrThrow(customUserDetails.getId());
LOGGER.debug("Updating mobile number for user with id={}",
user.getId());
user.setMobileNumber(updateRequest.getMobileNumber());
userRepository.save(user);
}
if (updateRequest.getAddress() != null
&& isValidAddress(updateRequest.getAddress())) {
Address address = getAddressOrThrow(
customUserDetails.getId());
LOGGER.debug("Updating address for user with id={}",
address.getUser().getId());
address.setStreet(updateRequest.getAddress().getStreet());
address.setCity(updateRequest.getAddress().getCity());
address.setState(updateRequest.getAddress().getState());
address.setCountry(updateRequest.getAddress().getCountry());
address.setPostalCode(updateRequest.getAddress()
.getPostalCode());
addressRepository.save(address);
}
}
public UserDetailsRequest getUserDetails() {
CustomUserDetails customUserDetails =
AuthenticationService.getCurrentUserDetails();
LOGGER.debug("Fetching user details for user with id={}",
customUserDetails.getId());
User user = getUserOrThrow(customUserDetails.getId());
DeliveryAddress address = new DeliveryAddress();
address.setStreet(user.getAddress().getStreet());
address.setCity(user.getAddress().getCity());
address.setState(user.getAddress().getState());
address.setCountry(user.getAddress().getCountry());
address.setPostalCode(user.getAddress().getPostalCode());
return UserDetailsRequest.builder()
.name(user.getName())
.email(user.getEmail())
.mobileNumber(user.getMobileNumber())
.address(address)
.build();
}
private boolean isValidAddress(final DeliveryAddress address) {
if (address.getStreet() == null || address.getStreet().isBlank()) {
throw new ValidationException("Street is required");
}
if (address.getCity() == null || address.getCity().isBlank()) {
throw new ValidationException("City is required");
}
if (address.getState() == null || address.getState().isBlank()) {
throw new ValidationException("State is required");
}
if (address.getCountry() == null || address.getCountry().isBlank()) {
throw new ValidationException("Country is required");
}
if (address.getPostalCode() == null
|| address.getPostalCode().isBlank()) {
throw new ValidationException("Postal code is required");
}
return true;
}
private boolean mobileNumberIsUnique(final String mobileNumber) {
if (userRepository.existsByMobileNumber(mobileNumber)) {
throw new ValidationException("Mobile number already exists");
}
return true;
}
public User getUserOrThrow(final UUID userId) {
LOGGER.debug("Fetching user with id={}", userId);
return userRepository.findById(userId)
.orElseThrow(() -> {
LOGGER.warn("User not found with id={}", userId);
return new NotFoundException("User with id "
+ userId + " does not exist.");
});
}
public Address getAddressOrThrow(final UUID userId) {
LOGGER.debug("Fetching address for user with id={}", userId);
return addressRepository.findByUserId(userId)
.orElseThrow(() -> {
LOGGER.warn("Address not found for user with id={}",
userId);
return new NotFoundException("Address for user with id "
+ userId + " does not exist.");
});
}
}