fix updating habits
@@ -15,6 +15,8 @@ import greencity.repository.HabitRepo;
import greencity.repository.HabitTranslationRepo;
import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
@@ -121,12 +123,12 @@ public class HabitServiceImpl implements HabitService {
Habit habit = Habit.builder()
.habitTranslations(
habitManagementDto.getHabitTranslations().stream()
- .map(translationDto -> HabitTranslation.builder()
- .description(translationDto.getDescription())
- .habitItem(translationDto.getHabitItem())
- .name(translationDto.getName())
+ .map(habitTranslationDto -> HabitTranslation.builder()
+ .description(habitTranslationDto.getDescription())
+ .habitItem(habitTranslationDto.getHabitItem())
+ .name(habitTranslationDto.getName())
.language(modelMapper.map(
- languageService.findByCode(translationDto.getLanguageCode()),
+ languageService.findByCode(habitTranslationDto.getLanguageCode()),
Language.class)).build())
.collect(Collectors.toList())
).build();
@@ -163,23 +165,40 @@ public class HabitServiceImpl implements HabitService {
Habit habit = habitRepo.findById(habitManagementDto.getId())
.orElseThrow(() -> new WrongIdException(ErrorMessage.HABIT_NOT_FOUND_BY_ID));
- habit.getHabitTranslations()
- .forEach(ht -> {
- HabitTranslationManagementDto htmd = habitManagementDto.getHabitTranslations().stream()
- .filter(e -> e.getLanguageCode().equals(ht.getLanguage().getCode())).findFirst()
- .orElseThrow(RuntimeException::new);
-
- ht.setDescription(htmd.getDescription());
- ht.setHabitItem(htmd.getHabitItem());
- ht.setName(htmd.getName());
- });
- habit.setImage(habitManagementDto.getImage());
+ Map<String, HabitTranslationManagementDto> translationDtoMap = getMapTranslationsDtos(habitManagementDto);
+ habit.getHabitTranslations().forEach(
+ ht -> updateHabitTranslation(translationDtoMap.get(ht.getLanguage().getCode()), ht));
uploadImageForHabit(habitManagementDto, image, habit);
habitRepo.save(habit);
}
+ /**
+ * Method updates {@link HabitTranslation} with {@link HabitTranslationManagementDto} fields.
+ *
+ * @param htDto {@link HabitTranslationManagementDto} instance.
+ * @param ht {@link HabitTranslation} instance.
+ */
+ private void updateHabitTranslation(HabitTranslationManagementDto htDto, HabitTranslation ht) {
+ ht.setDescription(htDto.getDescription());
+ ht.setHabitItem(htDto.getHabitItem());
+ ht.setName(htDto.getName());
+ }
+
+ /**
+ * Method returns map with {@link HabitTranslationManagementDto} as a value and it's {@link String}
+ * language code as a key.
+ *
+ * @param habitManagementDto {@link HabitManagementDto} instance.
+ * @return {@link Map} with {@link String} key and {@link HabitTranslationManagementDto} instance value.
+ */
+ private Map<String, HabitTranslationManagementDto> getMapTranslationsDtos(HabitManagementDto habitManagementDto) {
+ return habitManagementDto.getHabitTranslations().stream()
+ .collect(Collectors.toMap(HabitTranslationManagementDto::getLanguageCode,
+ Function.identity()));
+ }
+
/**
* {@inheritDoc}
*/
