-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserService.java
More file actions
161 lines (125 loc) · 5.71 KB
/
UserService.java
File metadata and controls
161 lines (125 loc) · 5.71 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.ecommerce.user.service;
import com.ecommerce.user.dto.UserRegistrationRequest;
import com.ecommerce.user.dto.UserResponse;
import com.ecommerce.user.entity.User;
import com.ecommerce.user.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Arrays;
import java.util.Optional;
@Service
@Transactional
public class UserService {
@Autowired
private UserRepository userRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private KafkaTemplate<String, Object> kafkaTemplate;
public UserResponse registerUser(UserRegistrationRequest request) {
// Check if username or email already exists
if (userRepository.existsByUsername(request.getUsername())) {
throw new RuntimeException("Username already exists");
}
if (userRepository.existsByEmail(request.getEmail())) {
throw new RuntimeException("Email already exists");
}
// Create new user
User user = new User();
user.setUsername(request.getUsername());
user.setEmail(request.getEmail());
user.setPassword(passwordEncoder.encode(request.getPassword()));
user.setFirstName(request.getFirstName());
user.setLastName(request.getLastName());
user.setPhoneNumber(request.getPhoneNumber());
user.setStatus(User.UserStatus.ACTIVE);
user.setRoles(Arrays.asList(User.UserRole.CUSTOMER));
User savedUser = userRepository.save(user);
// Publish user registration event
publishUserEvent("user.registered", savedUser);
return new UserResponse(savedUser);
}
public Optional<UserResponse> getUserById(Long id) {
return userRepository.findById(id)
.map(UserResponse::new);
}
public Optional<UserResponse> getUserByUsername(String username) {
return userRepository.findByUsername(username)
.map(UserResponse::new);
}
public Optional<UserResponse> getUserByEmail(String email) {
return userRepository.findByEmail(email)
.map(UserResponse::new);
}
public Page<UserResponse> getAllUsers(Pageable pageable) {
return userRepository.findAll(pageable)
.map(UserResponse::new);
}
public Page<UserResponse> searchUsers(String keyword, Pageable pageable) {
return userRepository.searchUsers(keyword, pageable)
.map(UserResponse::new);
}
public UserResponse updateUser(Long id, UserRegistrationRequest request) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setFirstName(request.getFirstName());
user.setLastName(request.getLastName());
user.setPhoneNumber(request.getPhoneNumber());
User updatedUser = userRepository.save(user);
// Publish user update event
publishUserEvent("user.updated", updatedUser);
return new UserResponse(updatedUser);
}
public void deleteUser(Long id) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setStatus(User.UserStatus.INACTIVE);
userRepository.save(user);
// Publish user deletion event
publishUserEvent("user.deleted", user);
}
public UserResponse updateUserStatus(Long id, User.UserStatus status) {
User user = userRepository.findById(id)
.orElseThrow(() -> new RuntimeException("User not found"));
user.setStatus(status);
User updatedUser = userRepository.save(user);
// Publish user status change event
publishUserEvent("user.status.changed", updatedUser);
return new UserResponse(updatedUser);
}
private void publishUserEvent(String eventType, User user) {
UserEvent event = new UserEvent(eventType, user.getId(), user.getEmail(), user.getFirstName(), user.getLastName());
kafkaTemplate.send("user-events", event);
}
// Inner class for user events
public static class UserEvent {
private String eventType;
private Long userId;
private String email;
private String firstName;
private String lastName;
public UserEvent(String eventType, Long userId, String email, String firstName, String lastName) {
this.eventType = eventType;
this.userId = userId;
this.email = email;
this.firstName = firstName;
this.lastName = lastName;
}
// Getters and Setters
public String getEventType() { return eventType; }
public void setEventType(String eventType) { this.eventType = eventType; }
public Long getUserId() { return userId; }
public void setUserId(Long userId) { this.userId = userId; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getFirstName() { return firstName; }
public void setFirstName(String firstName) { this.firstName = firstName; }
public String getLastName() { return lastName; }
public void setLastName(String lastName) { this.lastName = lastName; }
}
}