refactored update method in AdviceServiceImpl
@@ -99,12 +99,11 @@ public class AdviceServiceImpl implements AdviceService {
Habit habit = habitRepo.findById(adviceDto.getHabit().getId())
.orElseThrow(() -> new WrongIdException(ErrorMessage.HABIT_NOT_FOUND_BY_ID));
advice.setHabit(habit);
- List<AdviceTranslation> adviceTranslations = modelMapper.map(adviceDto.getTranslations(),
- new TypeToken<List<AdviceTranslation>>() {
- }.getType());
- adviceTranslationRepo.deleteAllByAdvice(advice);
- advice.setTranslations(adviceTranslations);
- adviceTranslations.forEach(adviceTranslation -> adviceTranslation.setAdvice(advice));
+ advice.getTranslations().forEach(adviceTranslation ->
+ adviceTranslation.setContent(adviceDto.getTranslations().stream()
+ .filter(newTranslation -> newTranslation.getLanguage().getCode()
+ .equals(adviceTranslation.getLanguage().getCode())).findFirst().get()
+ .getContent()));
Advice updated = adviceRepo.save(advice);
return modelMapper.map(updated, AdviceVO.class);
