| Conditions | 10 |
| Total Lines | 117 |
| Code Lines | 110 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like com.osomapps.pt.reportworkout.ReportWorkoutService.create(String,WorkoutReportRequestDTO) often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
| 1 | package com.osomapps.pt.reportworkout; |
||
| 56 | @Transactional |
||
| 57 | public WorkoutReportResponseDTO create( |
||
| 58 | String token, WorkoutReportRequestDTO workoutReportRequestDTO) { |
||
| 59 | if (!token.isEmpty()) { |
||
| 60 | final InUser inUser = userService.checkUserToken(token).getInUser(); |
||
| 61 | final InWorkout inWorkout = |
||
| 62 | inWorkoutRepository.findById(workoutReportRequestDTO.getId()).orElse(null); |
||
| 63 | if (inWorkout == null) { |
||
| 64 | throw new ResourceNotFoundException( |
||
| 65 | "Workout with id (" + workoutReportRequestDTO.getId() + ") not found"); |
||
| 66 | } |
||
| 67 | final List<Long> inWorkoutItemIds = |
||
| 68 | inWorkout.getInWorkoutItems().stream() |
||
| 69 | .map(inWorkoutItem -> inWorkoutItem.getId()) |
||
| 70 | .collect(Collectors.toList()); |
||
| 71 | final int workoutsSize = inWorkout.getInProgram().getInWorkouts().size(); |
||
| 72 | inWorkout |
||
| 73 | .getInProgram() |
||
| 74 | .setCurrent_workout_index( |
||
| 75 | (inWorkout.getInProgram().getCurrent_workout_index() + 1) |
||
| 76 | % workoutsSize); |
||
| 77 | inProgramRepository.save(inWorkout.getInProgram()); |
||
| 78 | final WorkoutReportResponseDTO workoutReportResponseDTO = |
||
| 79 | new WorkoutReportResponseDTO(); |
||
| 80 | workoutReportResponseDTO.setItems(new ArrayList<>()); |
||
| 81 | for (WorkoutItemReportRequestDTO workoutItemReportRequestDTO : |
||
| 82 | workoutReportRequestDTO.getItems()) { |
||
| 83 | if (!inWorkoutItemIds.contains(workoutItemReportRequestDTO.getId())) { |
||
| 84 | log.warn( |
||
| 85 | "Id {} for inWorkoutItem not found", |
||
| 86 | workoutItemReportRequestDTO.getId()); |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | final InWorkoutItem inWorkoutItem = |
||
| 90 | inWorkoutItemRepository |
||
| 91 | .findById(workoutItemReportRequestDTO.getId()) |
||
| 92 | .orElse(null); |
||
| 93 | if (inWorkoutItem == null) { |
||
| 94 | throw new ResourceNotFoundException( |
||
| 95 | "Workout item with id (" |
||
| 96 | + workoutItemReportRequestDTO.getId() |
||
| 97 | + ") not found"); |
||
| 98 | } |
||
| 99 | if (!inWorkoutItem |
||
| 100 | .getInWorkout() |
||
| 101 | .getInProgram() |
||
| 102 | .getInUser() |
||
| 103 | .getId() |
||
| 104 | .equals(inUser.getId())) { |
||
| 105 | log.warn( |
||
| 106 | "User with id {} tries to update workout for user with id {}", |
||
| 107 | inUser.getId(), |
||
| 108 | inWorkoutItem.getInWorkout().getInProgram().getInUser().getId()); |
||
| 109 | throw new UnauthorizedException( |
||
| 110 | "This workout item with id " |
||
| 111 | + inWorkoutItem.getId() |
||
| 112 | + " belong to other user"); |
||
| 113 | } |
||
| 114 | final InWorkoutItemReport inWorkoutItemReport = new InWorkoutItemReport(); |
||
| 115 | inWorkoutItemReport.setInWorkoutItem(inWorkoutItem); |
||
| 116 | inWorkoutItemReport.setInWorkoutItemSetReports(new ArrayList<>()); |
||
| 117 | InWorkoutItemReport savedInWorkoutItemReport = |
||
| 118 | inWorkoutItemReportRepository.save(inWorkoutItemReport); |
||
| 119 | for (WorkoutItemSetReportRequestDTO workoutItemSetReportRequestDTO : |
||
| 120 | workoutItemReportRequestDTO.getSets()) { |
||
| 121 | final InWorkoutItemSetReport inWorkoutItemSetReport = |
||
| 122 | new InWorkoutItemSetReport(); |
||
| 123 | inWorkoutItemSetReport.setInWorkoutItemReport(savedInWorkoutItemReport); |
||
| 124 | inWorkoutItemSetReport.setRepetitions( |
||
| 125 | workoutItemSetReportRequestDTO.getRepetitions()); |
||
| 126 | inWorkoutItemSetReport.setWeight( |
||
| 127 | workoutItemSetReportRequestDTO.getWeight() == null |
||
| 128 | ? null |
||
| 129 | : workoutItemSetReportRequestDTO.getWeight().floatValue()); |
||
| 130 | inWorkoutItemSetReport.setBodyweight( |
||
| 131 | BooleanUtils.isTrue(workoutItemSetReportRequestDTO.getBodyweight())); |
||
| 132 | inWorkoutItemSetReport.setTime_in_sec( |
||
| 133 | workoutItemSetReportRequestDTO.getTime_in_sec()); |
||
| 134 | inWorkoutItemSetReport.setSpeed(workoutItemSetReportRequestDTO.getSpeed()); |
||
| 135 | inWorkoutItemSetReport.setIncline(workoutItemSetReportRequestDTO.getIncline()); |
||
| 136 | inWorkoutItemSetReport.setResistance( |
||
| 137 | workoutItemSetReportRequestDTO.getResistance()); |
||
| 138 | inWorkoutItemSetReportRepository.save(inWorkoutItemSetReport); |
||
| 139 | inWorkoutItemReport.getInWorkoutItemSetReports().add(inWorkoutItemSetReport); |
||
| 140 | } |
||
| 141 | final WorkoutItemReportResponseDTO workoutItemReportResponseDTO = |
||
| 142 | new WorkoutItemReportResponseDTO(); |
||
| 143 | workoutItemReportResponseDTO.setId(savedInWorkoutItemReport.getId()); |
||
| 144 | workoutItemReportResponseDTO.setSets( |
||
| 145 | inWorkoutItemReport.getInWorkoutItemSetReports().stream() |
||
| 146 | .map( |
||
| 147 | itemSetReport -> |
||
| 148 | new WorkoutItemSetReportResponseDTO() |
||
| 149 | .setId(itemSetReport.getId()) |
||
| 150 | .setRepetitions( |
||
| 151 | itemSetReport.getRepetitions()) |
||
| 152 | .setWeight( |
||
| 153 | itemSetReport.getWeight() == null |
||
| 154 | ? null |
||
| 155 | : itemSetReport |
||
| 156 | .getWeight() |
||
| 157 | .intValue()) |
||
| 158 | .setBodyweight( |
||
| 159 | itemSetReport.getBodyweight()) |
||
| 160 | .setTime_in_sec( |
||
| 161 | itemSetReport.getTime_in_sec()) |
||
| 162 | .setSpeed(itemSetReport.getSpeed()) |
||
| 163 | .setIncline(itemSetReport.getIncline()) |
||
| 164 | .setResistance( |
||
| 165 | itemSetReport.getResistance())) |
||
| 166 | .collect(Collectors.toList())); |
||
| 167 | workoutReportResponseDTO.setId(inWorkoutItem.getInWorkout().getId()); |
||
| 168 | workoutReportResponseDTO.getItems().add(workoutItemReportResponseDTO); |
||
| 169 | } |
||
| 170 | return workoutReportResponseDTO; |
||
| 171 | } |
||
| 172 | return new WorkoutReportResponseDTO(); |
||
| 173 | } |
||
| 175 |