|
1
|
|
|
package com.osomapps.pt.admin.user; |
|
2
|
|
|
|
|
3
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
4
|
|
|
import com.osomapps.pt.ResourceNotFoundException; |
|
5
|
|
|
import com.osomapps.pt.UnauthorizedException; |
|
6
|
|
|
import com.osomapps.pt.admin.program.AdminProgramAssignService; |
|
7
|
|
|
import com.osomapps.pt.dictionary.DictionaryName; |
|
8
|
|
|
import com.osomapps.pt.dictionary.DictionaryService; |
|
9
|
|
|
import com.osomapps.pt.goals.Goal; |
|
10
|
|
|
import com.osomapps.pt.goals.GoalRepository; |
|
11
|
|
|
import com.osomapps.pt.goals.InUserGoalRepository; |
|
12
|
|
|
import com.osomapps.pt.token.InUser; |
|
13
|
|
|
import com.osomapps.pt.token.InUserFacebook; |
|
14
|
|
|
import com.osomapps.pt.token.InUserFacebookRepository; |
|
15
|
|
|
import com.osomapps.pt.token.InUserGoal; |
|
16
|
|
|
import com.osomapps.pt.token.InUserRepository; |
|
17
|
|
|
import com.osomapps.pt.tokenemail.EmailValidator; |
|
18
|
|
|
import com.osomapps.pt.tokenemail.InUserEmail; |
|
19
|
|
|
import com.osomapps.pt.tokenemail.InUserEmailRepository; |
|
20
|
|
|
import java.io.IOException; |
|
21
|
|
|
import java.util.ArrayList; |
|
22
|
|
|
import java.util.Arrays; |
|
23
|
|
|
import java.util.HashMap; |
|
24
|
|
|
import java.util.List; |
|
25
|
|
|
import java.util.stream.Collectors; |
|
26
|
|
|
import org.springframework.data.domain.Sort; |
|
27
|
|
|
import org.springframework.security.crypto.password.PasswordEncoder; |
|
28
|
|
|
import org.springframework.stereotype.Service; |
|
29
|
|
|
import org.springframework.validation.MapBindingResult; |
|
30
|
|
|
|
|
31
|
|
|
@Service |
|
32
|
|
|
class AdminUserService { |
|
33
|
|
|
|
|
34
|
|
|
private final InUserRepository inUserRepository; |
|
35
|
|
|
private final InUserEmailRepository inUserEmailRepository; |
|
36
|
|
|
private final InUserFacebookRepository inUserFacebookRepository; |
|
37
|
|
|
private final InUserTypeRepository inUserTypeRepository; |
|
38
|
|
|
private final PasswordEncoder passwordEncoder; |
|
39
|
|
|
private final EmailValidator emailValidator; |
|
40
|
|
|
private final DictionaryService dictionaryService; |
|
41
|
|
|
private final InUserGoalRepository inUserGoalRepository; |
|
42
|
|
|
private final GoalRepository goalRepository; |
|
43
|
|
|
private final AdminProgramAssignService adminProgramAssignService; |
|
44
|
|
|
|
|
45
|
|
|
AdminUserService( |
|
|
|
|
|
|
46
|
|
|
InUserRepository inUserRepository, |
|
47
|
|
|
InUserEmailRepository inUserEmailRepository, |
|
48
|
|
|
InUserFacebookRepository inUserFacebookRepository, |
|
49
|
|
|
InUserTypeRepository inUserTypeRepository, |
|
50
|
|
|
PasswordEncoder passwordEncoder, |
|
51
|
|
|
EmailValidator emailValidator, |
|
52
|
|
|
DictionaryService dictionaryService, |
|
53
|
|
|
InUserGoalRepository inUserGoalRepository, |
|
54
|
|
|
GoalRepository goalRepository, |
|
55
|
|
|
AdminProgramAssignService adminProgramAssignService) { |
|
56
|
|
|
this.inUserRepository = inUserRepository; |
|
57
|
|
|
this.inUserEmailRepository = inUserEmailRepository; |
|
58
|
|
|
this.inUserFacebookRepository = inUserFacebookRepository; |
|
59
|
|
|
this.inUserTypeRepository = inUserTypeRepository; |
|
60
|
|
|
this.passwordEncoder = passwordEncoder; |
|
61
|
|
|
this.emailValidator = emailValidator; |
|
62
|
|
|
this.dictionaryService = dictionaryService; |
|
63
|
|
|
this.inUserGoalRepository = inUserGoalRepository; |
|
64
|
|
|
this.goalRepository = goalRepository; |
|
65
|
|
|
this.adminProgramAssignService = adminProgramAssignService; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
List<UserResponseDTO> findAll() { |
|
69
|
|
|
return inUserRepository.findAll(sortByIdAsc()).stream() |
|
70
|
|
|
.map(inUser -> inUserToDto(inUser)) |
|
71
|
|
|
.collect(Collectors.toList()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
private UserResponseDTO inUserToDto(InUser inUser) { |
|
75
|
|
|
final String userName; |
|
76
|
|
|
final String userEmail; |
|
77
|
|
|
if (inUser.getInUserFacebooks() == null || inUser.getInUserFacebooks().isEmpty()) { |
|
78
|
|
|
if (inUser.getInUserEmails().isEmpty()) { |
|
79
|
|
|
userName = "?"; |
|
80
|
|
|
userEmail = "?"; |
|
81
|
|
|
} else { |
|
82
|
|
|
userName = |
|
83
|
|
|
inUser.getInUserEmails() |
|
84
|
|
|
.get(inUser.getInUserEmails().size() - 1) |
|
85
|
|
|
.getUser_name(); |
|
86
|
|
|
userEmail = |
|
87
|
|
|
inUser.getInUserEmails() |
|
88
|
|
|
.get(inUser.getInUserEmails().size() - 1) |
|
89
|
|
|
.getLogin(); |
|
90
|
|
|
} |
|
91
|
|
|
} else { |
|
92
|
|
|
userName = |
|
93
|
|
|
inUser.getInUserFacebooks() |
|
94
|
|
|
.get(inUser.getInUserFacebooks().size() - 1) |
|
95
|
|
|
.getUser_name(); |
|
96
|
|
|
userEmail = "Facebook user"; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
final List<UserProgramResponseDTO> programs; |
|
100
|
|
|
if (inUser.getInPrograms() == null) { |
|
101
|
|
|
programs = null; |
|
102
|
|
|
} else { |
|
103
|
|
|
final long inProgramsCount = inUser.getInPrograms().stream().count(); |
|
104
|
|
|
programs = |
|
105
|
|
|
inUser.getInPrograms().stream() |
|
106
|
|
|
.skip(inProgramsCount < 3 ? 0 : inProgramsCount - 3) |
|
107
|
|
|
.map(AdminUserProgramService::inProgramToDto) |
|
108
|
|
|
.collect(Collectors.toList()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return UserResponseDTO.builder() |
|
112
|
|
|
.id(inUser.getId()) |
|
113
|
|
|
.email(userEmail) |
|
114
|
|
|
.name(userName) |
|
115
|
|
|
.gender(inUser.getD_sex()) |
|
116
|
|
|
.level(inUser.getD_level() == null ? null : Integer.parseInt(inUser.getD_level())) |
|
117
|
|
|
.goals( |
|
118
|
|
|
inUser.getInUserGoals() == null |
|
119
|
|
|
? null |
|
120
|
|
|
: inUser.getInUserGoals().stream() |
|
121
|
|
|
.map( |
|
122
|
|
|
goal -> |
|
123
|
|
|
new UserGoalResponseDTO() |
|
124
|
|
|
.setId(goal.getGoalId()) |
|
125
|
|
|
.setTitle( |
|
126
|
|
|
dictionaryService |
|
127
|
|
|
.getEnValue( |
|
128
|
|
|
DictionaryName |
|
129
|
|
|
.goal_title, |
|
130
|
|
|
goal |
|
131
|
|
|
.getD_goal_title(), |
|
132
|
|
|
null)) |
|
133
|
|
|
.setTitle2( |
|
134
|
|
|
dictionaryService |
|
135
|
|
|
.getEnValue( |
|
136
|
|
|
DictionaryName |
|
137
|
|
|
.goal_title_2, |
|
138
|
|
|
goal |
|
139
|
|
|
.getD_goal_title_2(), |
|
140
|
|
|
null))) |
|
141
|
|
|
.collect(Collectors.toList())) |
|
142
|
|
|
.type( |
|
143
|
|
|
inUser.getInUserType() == null |
|
144
|
|
|
? null |
|
145
|
|
|
: UserTypeResponseDTO.builder() |
|
146
|
|
|
.id(inUser.getInUserType().getId()) |
|
147
|
|
|
.nameEn( |
|
148
|
|
|
dictionaryService.getEnValue( |
|
149
|
|
|
DictionaryName.user_type, |
|
150
|
|
|
inUser.getInUserType().getD_user_type(), |
|
151
|
|
|
"")) |
|
152
|
|
|
.nameNo( |
|
153
|
|
|
dictionaryService.getNoValue( |
|
154
|
|
|
DictionaryName.user_type, |
|
155
|
|
|
inUser.getInUserType().getD_user_type(), |
|
156
|
|
|
"")) |
|
157
|
|
|
.build()) |
|
158
|
|
|
.weight(inUser.getWeight() == null ? null : inUser.getWeight().intValue()) |
|
159
|
|
|
.programs(programs) |
|
160
|
|
|
.build(); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private Sort sortByIdAsc() { |
|
164
|
|
|
return Sort.by(Sort.Direction.ASC, "id"); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
UserResponseDTO findOne(Long id) { |
|
168
|
|
|
final InUser inUser = inUserRepository.findById(id).orElse(null); |
|
169
|
|
|
if (inUser == null) { |
|
170
|
|
|
throw new ResourceNotFoundException("User with id " + id + " not found."); |
|
171
|
|
|
} |
|
172
|
|
|
return inUserToDto(inUser); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
UserResponseDTO create(UserRequestDTO userRequestDTO) { |
|
176
|
|
|
final InUserType inUserTypeDb = |
|
177
|
|
|
userRequestDTO.getType().getId() == null |
|
178
|
|
|
? null |
|
179
|
|
|
: inUserTypeRepository |
|
180
|
|
|
.findById(userRequestDTO.getType().getId()) |
|
181
|
|
|
.orElse(null); |
|
182
|
|
|
final InUser inUser = new InUser(); |
|
183
|
|
|
final InUserEmail inUserEmail = new InUserEmail(); |
|
184
|
|
|
final MapBindingResult errors = |
|
185
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
|
186
|
|
|
emailValidator.validate(userRequestDTO.getEmail(), errors); |
|
187
|
|
|
if (errors.hasErrors()) { |
|
188
|
|
|
throw new UnauthorizedException(errors.getAllErrors().get(0).getDefaultMessage()); |
|
189
|
|
|
} |
|
190
|
|
|
inUserEmail.setLogin(userRequestDTO.getEmail()); |
|
191
|
|
|
inUserEmail.setUser_name(userRequestDTO.getName()); |
|
192
|
|
|
inUserEmail.setPassword(passwordEncoder.encode("Qwerty+1")); |
|
193
|
|
|
inUser.setInUserType(inUserTypeDb); |
|
194
|
|
|
inUser.setInUserEmails(Arrays.asList(inUserEmail)); |
|
195
|
|
|
inUser.setD_sex(userRequestDTO.getGender()); |
|
196
|
|
|
inUser.setD_level( |
|
197
|
|
|
userRequestDTO.getLevel() == null ? null : "" + userRequestDTO.getLevel()); |
|
198
|
|
|
inUser.setWeight( |
|
199
|
|
|
userRequestDTO.getWeight() == null |
|
200
|
|
|
? null |
|
201
|
|
|
: userRequestDTO.getWeight().floatValue()); |
|
202
|
|
|
setupGoals(userRequestDTO, inUser); |
|
203
|
|
|
|
|
204
|
|
|
final InUser savedInUser = inUserRepository.save(inUser); |
|
205
|
|
|
inUserEmail.setInUser(savedInUser); |
|
206
|
|
|
inUserEmailRepository.save(inUserEmail); |
|
207
|
|
|
return inUserToDto(adminProgramAssignService.assign(savedInUser)); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
private void setupGoals(UserRequestDTO userRequestDTO, final InUser inUser) { |
|
211
|
|
View Code Duplication |
if (userRequestDTO.getGoals() != null) { |
|
|
|
|
|
|
212
|
|
|
if (userRequestDTO.getGoals().size() > 2) { |
|
213
|
|
|
throw new UnauthorizedException("Amount of goals must be not more than 2"); |
|
214
|
|
|
} |
|
215
|
|
|
inUserGoalRepository.deleteAll(inUser.getInUserGoals()); |
|
216
|
|
|
inUser.setInUserGoals(new ArrayList<>()); |
|
217
|
|
|
userRequestDTO |
|
218
|
|
|
.getGoals() |
|
219
|
|
|
.forEach( |
|
220
|
|
|
(userGoalRequestDTO) -> { |
|
221
|
|
|
Goal goal = |
|
222
|
|
|
goalRepository |
|
223
|
|
|
.findById(userGoalRequestDTO.getId()) |
|
224
|
|
|
.orElse(null); |
|
225
|
|
|
if (goal == null) { |
|
226
|
|
|
throw new UnauthorizedException( |
|
227
|
|
|
"Goal with id " |
|
228
|
|
|
+ userGoalRequestDTO.getId() |
|
229
|
|
|
+ " not found"); |
|
230
|
|
|
} |
|
231
|
|
|
String value = null; |
|
232
|
|
|
try { |
|
233
|
|
|
value = |
|
234
|
|
|
new ObjectMapper() |
|
235
|
|
|
.writeValueAsString( |
|
236
|
|
|
userGoalRequestDTO.getValues()); |
|
237
|
|
|
} catch (IOException ex) { |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
inUser.getInUserGoals() |
|
240
|
|
|
.add( |
|
241
|
|
|
inUserGoalRepository.save( |
|
242
|
|
|
new InUserGoal() |
|
243
|
|
|
.setGoalId( |
|
244
|
|
|
userGoalRequestDTO.getId()) |
|
245
|
|
|
.setD_goal_title( |
|
246
|
|
|
goal.getDGoalTitle()) |
|
247
|
|
|
.setD_goal_title_2( |
|
248
|
|
|
goal.getDGoalTitle2()) |
|
249
|
|
|
.setGoal_value(value))); |
|
250
|
|
|
}); |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
UserResponseDTO update(Long id, UserRequestDTO userRequestDTO) { |
|
255
|
|
|
final InUser inUser = inUserRepository.findById(id).orElse(null); |
|
256
|
|
|
if (inUser == null) { |
|
257
|
|
|
throw new ResourceNotFoundException("User with id " + id + " not found."); |
|
258
|
|
|
} |
|
259
|
|
|
final InUserType inUserTypeDb = |
|
260
|
|
|
userRequestDTO.getType() == null || userRequestDTO.getType().getId() == null |
|
261
|
|
|
? null |
|
262
|
|
|
: inUserTypeRepository |
|
263
|
|
|
.findById(userRequestDTO.getType().getId()) |
|
264
|
|
|
.orElse(null); |
|
265
|
|
|
inUser.setInUserType(inUserTypeDb); |
|
266
|
|
|
inUser.setD_sex(userRequestDTO.getGender()); |
|
267
|
|
|
inUser.setD_level( |
|
268
|
|
|
userRequestDTO.getLevel() == null ? null : "" + userRequestDTO.getLevel()); |
|
269
|
|
|
inUser.setWeight( |
|
270
|
|
|
userRequestDTO.getWeight() == null |
|
271
|
|
|
? null |
|
272
|
|
|
: userRequestDTO.getWeight().floatValue()); |
|
273
|
|
|
setupGoals(userRequestDTO, inUser); |
|
274
|
|
|
if (inUser.getInUserEmails().isEmpty()) { |
|
275
|
|
|
final InUserFacebook inUserFacebook = |
|
276
|
|
|
inUser.getInUserFacebooks().get(inUser.getInUserFacebooks().size() - 1); |
|
277
|
|
|
inUserFacebook.setUser_name(userRequestDTO.getName()); |
|
278
|
|
|
inUserFacebookRepository.save(inUserFacebook); |
|
279
|
|
|
} else { |
|
280
|
|
|
final MapBindingResult errors = |
|
281
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
|
282
|
|
|
emailValidator.validate(userRequestDTO.getEmail(), errors); |
|
283
|
|
|
if (errors.hasErrors()) { |
|
284
|
|
|
throw new UnauthorizedException(errors.getAllErrors().get(0).getDefaultMessage()); |
|
285
|
|
|
} |
|
286
|
|
|
inUser.getInUserEmails() |
|
287
|
|
|
.get(inUser.getInUserEmails().size() - 1) |
|
288
|
|
|
.setUser_name(userRequestDTO.getName()); |
|
289
|
|
|
inUser.getInUserEmails() |
|
290
|
|
|
.get(inUser.getInUserEmails().size() - 1) |
|
291
|
|
|
.setLogin(userRequestDTO.getEmail()); |
|
292
|
|
|
inUserEmailRepository.save( |
|
293
|
|
|
inUser.getInUserEmails().get(inUser.getInUserEmails().size() - 1)); |
|
294
|
|
|
} |
|
295
|
|
|
return inUserToDto(adminProgramAssignService.assign(inUser)); |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
UserResponseDTO delete(Long id) { |
|
299
|
|
|
final InUser inUser = inUserRepository.findById(id).orElse(null); |
|
300
|
|
|
if (inUser == null) { |
|
301
|
|
|
throw new ResourceNotFoundException("User with id " + id + " not found."); |
|
302
|
|
|
} |
|
303
|
|
|
final UserResponseDTO userResponseDTO = inUserToDto(inUser); |
|
304
|
|
|
inUserRepository.deleteById(id); |
|
305
|
|
|
return userResponseDTO; |
|
306
|
|
|
} |
|
307
|
|
|
} |
|
308
|
|
|
|