com.osomapps.pt.exercises.ExerciseService   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 2
eloc 42
dl 0
loc 48
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ExerciseService(ExerciseViewRepository,DictionaryService) 0 5 1
A findAll() 0 36 1
1
package com.osomapps.pt.exercises;
2
3
import com.osomapps.pt.dictionary.DictionaryName;
4
import com.osomapps.pt.dictionary.DictionaryService;
5
import java.util.List;
6
import java.util.stream.Collectors;
7
import org.springframework.beans.factory.annotation.Autowired;
8
import org.springframework.data.domain.Sort;
9
import org.springframework.stereotype.Service;
10
11
@Service
12
class ExerciseService {
13
    private final ExerciseViewRepository exerciseViewRepository;
14
    private final DictionaryService dictionaryService;
15
16
    @Autowired
17
    ExerciseService(
18
            ExerciseViewRepository exerciseViewRepository, DictionaryService dictionaryService) {
19
        this.exerciseViewRepository = exerciseViewRepository;
20
        this.dictionaryService = dictionaryService;
21
    }
22
23
    List<ExerciseDTO> findAll() {
24
        List<ExerciseView> exercises =
25
                exerciseViewRepository.findAll(Sort.by(Sort.Direction.ASC, "exerciseId"));
26
        return exercises.stream()
27
                .map(
28
                        exercise -> {
29
                            ExerciseDTO exerciseDTO = new ExerciseDTO();
30
                            exerciseDTO.setId(exercise.getExerciseId());
31
                            exerciseDTO.setName(
32
                                    dictionaryService.getEnValue(
33
                                            DictionaryName.exercise_name,
34
                                            exercise.getDExerciseName(),
35
                                            ""));
36
                            exerciseDTO.setDescription(
37
                                    dictionaryService.getEnValue(
38
                                            DictionaryName.exercise_description,
39
                                            exercise.getDExerciseDescription(),
40
                                            null));
41
                            exerciseDTO.setCardio_percent(exercise.getCardio_percent());
42
                            exerciseDTO.setTypes(
43
                                    exercise.getExerciseTypes().stream()
44
                                            .map(type -> type.getName())
45
                                            .collect(Collectors.toList()));
46
                            exerciseDTO.setImages(
47
                                    exercise.getExerciseFiles().stream()
48
                                            .map(
49
                                                    file ->
50
                                                            "/api/v1/exercise-image/"
51
                                                                    + file.getId()
52
                                                                    + "/"
53
                                                                    + file.getFile_name())
54
                                            .collect(Collectors.toList()));
55
                            return exerciseDTO;
56
                        })
57
                .sorted((e1, e2) -> Long.compare(e1.getId(), e2.getId()))
58
                .collect(Collectors.toList());
59
    }
60
}
61