1
|
|
|
package com.osomapps.pt.admin.exercise; |
2
|
|
|
|
3
|
|
|
import com.osomapps.pt.ResourceNotFoundException; |
4
|
|
|
import com.osomapps.pt.UnauthorizedException; |
5
|
|
|
import com.osomapps.pt.dictionary.DictionaryName; |
6
|
|
|
import com.osomapps.pt.dictionary.DictionaryService; |
7
|
|
|
import com.osomapps.pt.exercises.Exercise; |
8
|
|
|
import com.osomapps.pt.exercises.ExerciseBodypart; |
9
|
|
|
import com.osomapps.pt.exercises.ExerciseEquipmentType; |
10
|
|
|
import com.osomapps.pt.exercises.ExerciseFile; |
11
|
|
|
import com.osomapps.pt.exercises.ExerciseFilePreview; |
12
|
|
|
import com.osomapps.pt.exercises.ExerciseFilePreviewRepository; |
13
|
|
|
import com.osomapps.pt.exercises.ExerciseFileRepository; |
14
|
|
|
import com.osomapps.pt.exercises.ExerciseRepository; |
15
|
|
|
import com.osomapps.pt.tokenemail.DataurlValidator; |
16
|
|
|
import java.util.HashMap; |
17
|
|
|
import java.util.List; |
18
|
|
|
import java.util.stream.Collectors; |
19
|
|
|
import org.springframework.data.domain.Sort; |
20
|
|
|
import org.springframework.stereotype.Service; |
21
|
|
|
import org.springframework.validation.MapBindingResult; |
22
|
|
|
|
23
|
|
|
@Service |
24
|
|
|
class AdminExerciseService { |
25
|
|
|
private final ExerciseRepository exerciseRepository; |
26
|
|
|
private final ExerciseBodypartRepository exerciseBodypartRepository; |
27
|
|
|
private final ExerciseEquipmentTypeRepository exerciseEquipmentTypeRepository; |
28
|
|
|
private final ExerciseTypeRepository exerciseTypeRepository; |
29
|
|
|
private final DictionaryService dictionaryService; |
30
|
|
|
private final ExerciseInputRepository exerciseInputRepository; |
31
|
|
|
private final ExerciseOutputRepository exerciseOutputRepository; |
32
|
|
|
private final ExerciseFileRepository exerciseFileRepository; |
33
|
|
|
private final ExerciseFilePreviewRepository exerciseFilePreviewRepository; |
34
|
|
|
private final DataurlValidator dataurlValidator; |
35
|
|
|
|
36
|
|
|
AdminExerciseService( |
|
|
|
|
37
|
|
|
ExerciseRepository exerciseRepository, |
38
|
|
|
ExerciseBodypartRepository exerciseBodypartRepository, |
39
|
|
|
ExerciseEquipmentTypeRepository exerciseEquipmentTypeRepository, |
40
|
|
|
ExerciseTypeRepository exerciseTypeRepository, |
41
|
|
|
DictionaryService dictionaryService, |
42
|
|
|
ExerciseInputRepository exerciseInputRepository, |
43
|
|
|
ExerciseOutputRepository exerciseOutputRepository, |
44
|
|
|
ExerciseFileRepository exerciseFileRepository, |
45
|
|
|
ExerciseFilePreviewRepository exerciseFilePreviewRepository, |
46
|
|
|
DataurlValidator dataurlValidator) { |
47
|
|
|
this.exerciseRepository = exerciseRepository; |
48
|
|
|
this.exerciseBodypartRepository = exerciseBodypartRepository; |
49
|
|
|
this.exerciseEquipmentTypeRepository = exerciseEquipmentTypeRepository; |
50
|
|
|
this.exerciseTypeRepository = exerciseTypeRepository; |
51
|
|
|
this.dictionaryService = dictionaryService; |
52
|
|
|
this.exerciseInputRepository = exerciseInputRepository; |
53
|
|
|
this.exerciseOutputRepository = exerciseOutputRepository; |
54
|
|
|
this.exerciseFileRepository = exerciseFileRepository; |
55
|
|
|
this.exerciseFilePreviewRepository = exerciseFilePreviewRepository; |
56
|
|
|
this.dataurlValidator = dataurlValidator; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
List<ExerciseResponseDTO> findAll() { |
60
|
|
|
return exerciseRepository.findAll(sortByIdAsc()).stream() |
61
|
|
|
.map(exercise -> exerciseToDto(exercise)) |
62
|
|
|
.collect(Collectors.toList()); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private Sort sortByIdAsc() { |
66
|
|
|
return Sort.by(Sort.Direction.ASC, "exerciseId"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private ExerciseResponseDTO exerciseToDto(Exercise exercise) { |
70
|
|
|
return ExerciseResponseDTO.builder() |
71
|
|
|
.id(exercise.getId()) |
72
|
|
|
.exerciseId(exercise.getExerciseId()) |
73
|
|
|
.nameEn( |
74
|
|
|
dictionaryService.getEnValue( |
75
|
|
|
DictionaryName.exercise_name, exercise.getDExerciseName(), "")) |
76
|
|
|
.nameNo( |
77
|
|
|
dictionaryService.getNoValue( |
78
|
|
|
DictionaryName.exercise_name, exercise.getDExerciseName(), "")) |
79
|
|
|
.descriptionEn( |
80
|
|
|
dictionaryService.getEnValue( |
81
|
|
|
DictionaryName.exercise_description, |
82
|
|
|
exercise.getDExerciseDescription(), |
83
|
|
|
"")) |
84
|
|
|
.descriptionNo( |
85
|
|
|
dictionaryService.getNoValue( |
86
|
|
|
DictionaryName.exercise_description, |
87
|
|
|
exercise.getDExerciseDescription(), |
88
|
|
|
"")) |
89
|
|
|
.bodypart( |
90
|
|
|
exercise.getExerciseBodypart() == null |
91
|
|
|
? null |
92
|
|
|
: ExerciseBodypartResponseDTO.builder() |
93
|
|
|
.id(exercise.getExerciseBodypart().getId()) |
94
|
|
|
.nameEn( |
95
|
|
|
dictionaryService.getEnValue( |
96
|
|
|
DictionaryName.exercise_bodypart_name, |
97
|
|
|
exercise.getExerciseBodypart() |
98
|
|
|
.getDExerciseBodypartName(), |
99
|
|
|
"")) |
100
|
|
|
.nameNo( |
101
|
|
|
dictionaryService.getNoValue( |
102
|
|
|
DictionaryName.exercise_bodypart_name, |
103
|
|
|
exercise.getExerciseBodypart() |
104
|
|
|
.getDExerciseBodypartName(), |
105
|
|
|
"")) |
106
|
|
|
.build()) |
107
|
|
|
.equipmentType( |
108
|
|
|
exercise.getExerciseEquipmentType() == null |
109
|
|
|
? null |
110
|
|
|
: ExerciseEquipmentTypeResponseDTO.builder() |
111
|
|
|
.id(exercise.getExerciseEquipmentType().getId()) |
112
|
|
|
.nameEn( |
113
|
|
|
dictionaryService.getEnValue( |
114
|
|
|
DictionaryName.exercise_equipment_type_name, |
115
|
|
|
exercise.getExerciseEquipmentType() |
116
|
|
|
.getDExerciseEquipmentTypeName(), |
117
|
|
|
"")) |
118
|
|
|
.nameNo( |
119
|
|
|
dictionaryService.getNoValue( |
120
|
|
|
DictionaryName.exercise_equipment_type_name, |
121
|
|
|
exercise.getExerciseEquipmentType() |
122
|
|
|
.getDExerciseEquipmentTypeName(), |
123
|
|
|
"")) |
124
|
|
|
.build()) |
125
|
|
|
.types( |
126
|
|
|
exercise.getExerciseTypes().stream() |
127
|
|
|
.map( |
128
|
|
|
type -> |
129
|
|
|
ExerciseTypeResponseDTO.builder() |
130
|
|
|
.id(type.getId()) |
131
|
|
|
.name(type.getName()) |
132
|
|
|
.build()) |
133
|
|
|
.collect(Collectors.toList())) |
134
|
|
|
.inputs( |
135
|
|
|
exercise.getExerciseInputs().stream() |
136
|
|
|
.map( |
137
|
|
|
input -> |
138
|
|
|
ExerciseInputResponseDTO.builder() |
139
|
|
|
.id(input.getId()) |
140
|
|
|
.name(input.getName()) |
141
|
|
|
.build()) |
142
|
|
|
.collect(Collectors.toList())) |
143
|
|
|
.outputs( |
144
|
|
|
exercise.getExerciseOutputs().stream() |
145
|
|
|
.map( |
146
|
|
|
output -> |
147
|
|
|
ExerciseOutputResponseDTO.builder() |
148
|
|
|
.id(output.getId()) |
149
|
|
|
.name(output.getName()) |
150
|
|
|
.build()) |
151
|
|
|
.collect(Collectors.toList())) |
152
|
|
|
.files( |
153
|
|
|
exercise.getExerciseFiles().stream() |
154
|
|
|
.map( |
155
|
|
|
file -> |
156
|
|
|
ExerciseFileResponseDTO.builder() |
157
|
|
|
.id(file.getId()) |
158
|
|
|
.file_name(file.getFile_name()) |
159
|
|
|
.file_size(file.getFile_size()) |
160
|
|
|
.file_type(file.getFile_type()) |
161
|
|
|
.build()) |
162
|
|
|
.collect(Collectors.toList())) |
163
|
|
|
.cardioPercent(exercise.getCardio_percent()) |
164
|
|
|
.build(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
ExerciseResponseDTO findOne(Long id) { |
168
|
|
|
final Exercise exercise = exerciseRepository.findById(id).orElse(null); |
169
|
|
|
if (exercise == null) { |
170
|
|
|
throw new ResourceNotFoundException("Exercise with id " + id + " not found."); |
171
|
|
|
} |
172
|
|
|
return exerciseToDto(exercise); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
ExerciseResponseDTO create(ExerciseRequestDTO exerciseRequestDTO) { |
176
|
|
|
final ExerciseBodypart exerciseBodypartDb = |
177
|
|
|
exerciseRequestDTO.getBodypart() == null |
178
|
|
|
|| exerciseRequestDTO.getBodypart().getId() == null |
179
|
|
|
? null |
180
|
|
|
: exerciseBodypartRepository |
181
|
|
|
.findById(exerciseRequestDTO.getBodypart().getId()) |
182
|
|
|
.orElse(null); |
183
|
|
|
final ExerciseEquipmentType exerciseEquipmentTypeDb = |
184
|
|
|
exerciseRequestDTO.getEquipmentType() == null |
185
|
|
|
|| exerciseRequestDTO.getEquipmentType().getId() == null |
186
|
|
|
? null |
187
|
|
|
: exerciseEquipmentTypeRepository |
188
|
|
|
.findById(exerciseRequestDTO.getEquipmentType().getId()) |
189
|
|
|
.orElse(null); |
190
|
|
|
final String dataKey = |
191
|
|
|
dictionaryService.getNewDictionaryDataKey(DictionaryName.exercise_name); |
192
|
|
|
dictionaryService.createDictionaryDataKey( |
193
|
|
|
DictionaryName.exercise_name, |
194
|
|
|
dataKey, |
195
|
|
|
exerciseRequestDTO.getNameEn(), |
196
|
|
|
exerciseRequestDTO.getNameNo()); |
197
|
|
|
final String dataDescriptionKey = |
198
|
|
|
dictionaryService.getNewDictionaryDataKey(DictionaryName.exercise_description); |
199
|
|
|
dictionaryService.createDictionaryDataKey( |
200
|
|
|
DictionaryName.exercise_description, |
201
|
|
|
dataDescriptionKey, |
202
|
|
|
exerciseRequestDTO.getDescriptionEn(), |
203
|
|
|
exerciseRequestDTO.getDescriptionNo()); |
204
|
|
|
final Exercise exercise = new Exercise(); |
205
|
|
|
exercise.setDExerciseName(dataKey); |
206
|
|
|
exercise.setDExerciseDescription(dataDescriptionKey); |
207
|
|
|
exercise.setExerciseId(exerciseRequestDTO.getExerciseId()); |
208
|
|
|
exercise.setExerciseBodypart(exerciseBodypartDb); |
209
|
|
|
exercise.setExerciseEquipmentType(exerciseEquipmentTypeDb); |
210
|
|
|
exercise.setExerciseTypes( |
211
|
|
|
exerciseTypeRepository.findAllById( |
212
|
|
|
exerciseRequestDTO.getTypes().stream() |
213
|
|
|
.map(type -> type.getId()) |
214
|
|
|
.collect(Collectors.toList()))); |
215
|
|
|
if (exerciseRequestDTO.getInputs() != null) { |
216
|
|
|
exercise.setExerciseInputs( |
217
|
|
|
exerciseInputRepository.findAllById( |
218
|
|
|
exerciseRequestDTO.getInputs().stream() |
219
|
|
|
.map(input -> input.getId()) |
220
|
|
|
.collect(Collectors.toList()))); |
221
|
|
|
} |
222
|
|
|
if (exerciseRequestDTO.getOutputs() != null) { |
223
|
|
|
exercise.setExerciseOutputs( |
224
|
|
|
exerciseOutputRepository.findAllById( |
225
|
|
|
exerciseRequestDTO.getOutputs().stream() |
226
|
|
|
.map(output -> output.getId()) |
227
|
|
|
.collect(Collectors.toList()))); |
228
|
|
|
} |
229
|
|
View Code Duplication |
if (exerciseRequestDTO.getFiles() != null) { |
|
|
|
|
230
|
|
|
List<ExerciseFile> exerciseFiles = |
231
|
|
|
exerciseRequestDTO.getFiles().stream() |
232
|
|
|
.map( |
233
|
|
|
file -> { |
234
|
|
|
final MapBindingResult errors = |
235
|
|
|
new MapBindingResult( |
236
|
|
|
new HashMap<>(), String.class.getName()); |
237
|
|
|
if (file.getData_url() == null) { |
238
|
|
|
throw new UnauthorizedException("Invalid data_url"); |
239
|
|
|
} |
240
|
|
|
dataurlValidator.validate(file.getData_url(), errors); |
241
|
|
|
if (errors.hasErrors()) { |
242
|
|
|
throw new UnauthorizedException( |
243
|
|
|
errors.getAllErrors() |
244
|
|
|
.get(0) |
245
|
|
|
.getDefaultMessage()); |
246
|
|
|
} |
247
|
|
|
return new ExerciseFile() |
248
|
|
|
.setFile_name(file.getFile_name()) |
249
|
|
|
.setFile_size(file.getFile_size()) |
250
|
|
|
.setFile_type(file.getFile_type()) |
251
|
|
|
.setData_url(file.getData_url()); |
252
|
|
|
}) |
253
|
|
|
.collect(Collectors.toList()); |
254
|
|
|
List<ExerciseFile> savedExerciseFiles = exerciseFileRepository.saveAll(exerciseFiles); |
255
|
|
|
exercise.setExerciseFiles( |
256
|
|
|
savedExerciseFiles.stream() |
257
|
|
|
.map( |
258
|
|
|
file -> |
259
|
|
|
new ExerciseFilePreview() |
260
|
|
|
.setId(file.getId()) |
261
|
|
|
.setCreated(file.getCreated()) |
262
|
|
|
.setFile_name(file.getFile_name()) |
263
|
|
|
.setFile_size(file.getFile_size()) |
264
|
|
|
.setFile_type(file.getFile_type()) |
265
|
|
|
.setExercises(file.getExercises())) |
266
|
|
|
.collect(Collectors.toList())); |
267
|
|
|
} |
268
|
|
|
exercise.setCardio_percent(exerciseRequestDTO.getCardioPercent()); |
269
|
|
|
return exerciseToDto(exerciseRepository.save(exercise)); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
ExerciseResponseDTO update(Long id, ExerciseRequestDTO exerciseRequestDTO) { |
273
|
|
|
final Exercise existedExercise = exerciseRepository.findById(id).orElse(null); |
274
|
|
|
if (existedExercise == null) { |
275
|
|
|
throw new ResourceNotFoundException("Exercise with id not found: " + id); |
276
|
|
|
} |
277
|
|
|
final String dataKey = existedExercise.getDExerciseName(); |
278
|
|
|
dictionaryService.createDictionaryDataKey( |
279
|
|
|
DictionaryName.exercise_name, |
280
|
|
|
dataKey, |
281
|
|
|
exerciseRequestDTO.getNameEn(), |
282
|
|
|
exerciseRequestDTO.getNameNo()); |
283
|
|
|
final String dataDescriptionKey = |
284
|
|
|
existedExercise.getDExerciseDescription() == null |
285
|
|
|
? dictionaryService.getNewDictionaryDataKey( |
286
|
|
|
DictionaryName.exercise_description) |
287
|
|
|
: existedExercise.getDExerciseDescription(); |
288
|
|
|
dictionaryService.createDictionaryDataKey( |
289
|
|
|
DictionaryName.exercise_description, |
290
|
|
|
dataDescriptionKey, |
291
|
|
|
exerciseRequestDTO.getDescriptionEn(), |
292
|
|
|
exerciseRequestDTO.getDescriptionNo()); |
293
|
|
|
final ExerciseBodypart exerciseBodypartDb = |
294
|
|
|
exerciseRequestDTO.getBodypart() == null |
295
|
|
|
? null |
296
|
|
|
: exerciseBodypartRepository |
297
|
|
|
.findById(exerciseRequestDTO.getBodypart().getId()) |
298
|
|
|
.orElse(null); |
299
|
|
|
final ExerciseEquipmentType exerciseEquipmentTypeDb = |
300
|
|
|
exerciseRequestDTO.getEquipmentType() == null |
301
|
|
|
? null |
302
|
|
|
: exerciseEquipmentTypeRepository |
303
|
|
|
.findById(exerciseRequestDTO.getEquipmentType().getId()) |
304
|
|
|
.orElse(null); |
305
|
|
|
existedExercise.setExerciseBodypart(exerciseBodypartDb); |
306
|
|
|
existedExercise.setExerciseEquipmentType(exerciseEquipmentTypeDb); |
307
|
|
|
existedExercise.setDExerciseName(dataKey); |
308
|
|
|
existedExercise.setDExerciseDescription(dataDescriptionKey); |
309
|
|
|
existedExercise.setExerciseId(exerciseRequestDTO.getExerciseId()); |
310
|
|
|
existedExercise.setCardio_percent(exerciseRequestDTO.getCardioPercent()); |
311
|
|
|
existedExercise.setExerciseTypes( |
312
|
|
|
exerciseTypeRepository.findAllById( |
313
|
|
|
exerciseRequestDTO.getTypes().stream() |
314
|
|
|
.map(type -> type.getId()) |
315
|
|
|
.collect(Collectors.toList()))); |
316
|
|
|
if (exerciseRequestDTO.getInputs() != null) { |
317
|
|
|
existedExercise.setExerciseInputs( |
318
|
|
|
exerciseInputRepository.findAllById( |
319
|
|
|
exerciseRequestDTO.getInputs().stream() |
320
|
|
|
.map(input -> input.getId()) |
321
|
|
|
.collect(Collectors.toList()))); |
322
|
|
|
} |
323
|
|
|
if (exerciseRequestDTO.getOutputs() != null) { |
324
|
|
|
existedExercise.setExerciseOutputs( |
325
|
|
|
exerciseOutputRepository.findAllById( |
326
|
|
|
exerciseRequestDTO.getOutputs().stream() |
327
|
|
|
.map(output -> output.getId()) |
328
|
|
|
.collect(Collectors.toList()))); |
329
|
|
|
} |
330
|
|
View Code Duplication |
if (exerciseRequestDTO.getFiles() != null) { |
|
|
|
|
331
|
|
|
exerciseFilePreviewRepository.deleteAll(existedExercise.getExerciseFiles()); |
332
|
|
|
List<ExerciseFile> exerciseFiles = |
333
|
|
|
exerciseRequestDTO.getFiles().stream() |
334
|
|
|
.map( |
335
|
|
|
file -> { |
336
|
|
|
final MapBindingResult errors = |
337
|
|
|
new MapBindingResult( |
338
|
|
|
new HashMap<>(), String.class.getName()); |
339
|
|
|
if (file.getData_url() == null) { |
340
|
|
|
throw new UnauthorizedException("Invalid data_url"); |
341
|
|
|
} |
342
|
|
|
dataurlValidator.validate(file.getData_url(), errors); |
343
|
|
|
if (errors.hasErrors()) { |
344
|
|
|
throw new UnauthorizedException( |
345
|
|
|
errors.getAllErrors() |
346
|
|
|
.get(0) |
347
|
|
|
.getDefaultMessage()); |
348
|
|
|
} |
349
|
|
|
return new ExerciseFile() |
350
|
|
|
.setFile_name(file.getFile_name()) |
351
|
|
|
.setFile_size(file.getFile_size()) |
352
|
|
|
.setFile_type(file.getFile_type()) |
353
|
|
|
.setData_url(file.getData_url()); |
354
|
|
|
}) |
355
|
|
|
.collect(Collectors.toList()); |
356
|
|
|
List<ExerciseFile> savedExerciseFiles = exerciseFileRepository.saveAll(exerciseFiles); |
357
|
|
|
existedExercise.setExerciseFiles( |
358
|
|
|
savedExerciseFiles.stream() |
359
|
|
|
.map( |
360
|
|
|
file -> |
361
|
|
|
new ExerciseFilePreview() |
362
|
|
|
.setId(file.getId()) |
363
|
|
|
.setCreated(file.getCreated()) |
364
|
|
|
.setFile_name(file.getFile_name()) |
365
|
|
|
.setFile_size(file.getFile_size()) |
366
|
|
|
.setFile_type(file.getFile_type()) |
367
|
|
|
.setExercises(file.getExercises())) |
368
|
|
|
.collect(Collectors.toList())); |
369
|
|
|
} |
370
|
|
|
final Exercise savedExercise = exerciseRepository.save(existedExercise); |
371
|
|
|
return exerciseToDto(savedExercise); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
ExerciseResponseDTO delete(Long id) { |
375
|
|
|
final Exercise exercise = exerciseRepository.findById(id).orElse(null); |
376
|
|
|
if (exercise == null) { |
377
|
|
|
throw new ResourceNotFoundException("Exercise with id " + id + " not found."); |
378
|
|
|
} |
379
|
|
|
dictionaryService.deleteDatas(DictionaryName.exercise_name, exercise.getDExerciseName()); |
380
|
|
|
dictionaryService.deleteDatas( |
381
|
|
|
DictionaryName.exercise_description, exercise.getDExerciseDescription()); |
382
|
|
|
final ExerciseResponseDTO exerciseResponseDTO = exerciseToDto(exercise); |
383
|
|
|
exerciseRepository.deleteById(id); |
384
|
|
|
return exerciseResponseDTO; |
385
|
|
|
} |
386
|
|
|
} |
387
|
|
|
|