-
Notifications
You must be signed in to change notification settings - Fork 80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bugfix #7319 - Fixed incorrect saving for HabitTranslation for ua fields. #7898
Changes from 2 commits
57996d1
54325f2
343e565
1dc3b17
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,37 @@ protected HabitTranslation convert(HabitTranslationDto habitTranslationDto) { | |
.build(); | ||
} | ||
|
||
/** | ||
* Additional method that build {@link HabitTranslation} from | ||
* {@link HabitTranslationDto} but from nameUa, descriptionUa, habitItemUa | ||
* fields if they not null. | ||
* | ||
* @param habitTranslationDto {@link HabitTranslationDto} | ||
* @return {@link HabitTranslation} | ||
* | ||
* @author Chernenko Vitaliy | ||
*/ | ||
public HabitTranslation convertUa(HabitTranslationDto habitTranslationDto) { | ||
HabitTranslation habitTranslation = new HabitTranslation(); | ||
if (habitTranslationDto.getNameUa() == null) { | ||
habitTranslation.setName(habitTranslationDto.getName()); | ||
} else { | ||
habitTranslation.setName(habitTranslationDto.getNameUa()); | ||
} | ||
if (habitTranslationDto.getDescriptionUa() == null) { | ||
habitTranslation.setDescription(habitTranslationDto.getDescription()); | ||
} else { | ||
habitTranslation.setDescription(habitTranslationDto.getDescriptionUa()); | ||
} | ||
if (habitTranslationDto.getHabitItemUa() == null) { | ||
habitTranslation.setHabitItem(habitTranslationDto.getHabitItem()); | ||
} else { | ||
habitTranslation.setHabitItem(habitTranslationDto.getHabitItemUa()); | ||
} | ||
|
||
return habitTranslation; | ||
} | ||
|
||
/** | ||
* Method that build {@link List} of {@link HabitTranslation} from {@link List} | ||
* of {@link HabitTranslationDto}. | ||
|
@@ -29,4 +60,22 @@ protected HabitTranslation convert(HabitTranslationDto habitTranslationDto) { | |
public List<HabitTranslation> mapAllToList(List<HabitTranslationDto> dtoList) { | ||
return dtoList.stream().map(this::convert).collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
* Method that build {@link List} of {@link HabitTranslation} from {@link List} | ||
* of {@link HabitTranslationDto} and {@link String} language. | ||
* | ||
* @param dtoList {@link List} of {@link HabitTranslationDto} | ||
* @param language {@link String} | ||
* | ||
* @return {@link List} of {@link HabitTranslation} | ||
* | ||
* @author Chernenko Vitaliy | ||
*/ | ||
public List<HabitTranslation> mapAllToList(List<HabitTranslationDto> dtoList, String language) { | ||
if (language.equals("ua")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. I'll remember it. |
||
return dtoList.stream().map(this::convertUa).collect(Collectors.toList()); | ||
} | ||
return dtoList.stream().map(this::convert).collect(Collectors.toList()); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,21 +491,26 @@ private void updateHabitTranslationsForCustomHabit(CustomHabitDtoRequest habitDt | |
} | ||
|
||
private void saveHabitTranslationListsToHabitTranslationRepo(CustomHabitDtoRequest habitDto, Habit habit) { | ||
List<HabitTranslation> habitTranslationListForUa = mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto); | ||
final String UA = "ua"; | ||
final String EN = "en"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move to the constants There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks. Done |
||
List<HabitTranslation> habitTranslationListForUa = | ||
mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto, UA); | ||
habitTranslationListForUa.forEach(habitTranslation -> habitTranslation.setHabit(habit)); | ||
habitTranslationListForUa.forEach(habitTranslation -> habitTranslation.setLanguage( | ||
languageRepo.findByCode("ua").orElseThrow(NoSuchElementException::new))); | ||
languageRepo.findByCode(UA).orElseThrow(NoSuchElementException::new))); | ||
habitTranslationRepo.saveAll(habitTranslationListForUa); | ||
|
||
List<HabitTranslation> habitTranslationListForEn = mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto); | ||
List<HabitTranslation> habitTranslationListForEn = | ||
mapHabitTranslationFromAddCustomHabitDtoRequest(habitDto, EN); | ||
habitTranslationListForEn.forEach(habitTranslation -> habitTranslation.setHabit(habit)); | ||
habitTranslationListForEn.forEach(habitTranslation -> habitTranslation.setLanguage( | ||
languageRepo.findByCode("en").orElseThrow(NoSuchElementException::new))); | ||
languageRepo.findByCode(EN).orElseThrow(NoSuchElementException::new))); | ||
habitTranslationRepo.saveAll(habitTranslationListForEn); | ||
} | ||
|
||
private List<HabitTranslation> mapHabitTranslationFromAddCustomHabitDtoRequest(CustomHabitDtoRequest habitDto) { | ||
return habitTranslationMapper.mapAllToList(habitDto.getHabitTranslations()); | ||
private List<HabitTranslation> mapHabitTranslationFromAddCustomHabitDtoRequest(CustomHabitDtoRequest habitDto, | ||
String language) { | ||
return habitTranslationMapper.mapAllToList(habitDto.getHabitTranslations(), language); | ||
} | ||
|
||
private void setTagsIdsToHabit(CustomHabitDtoRequest habitDto, Habit habit) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that logic can be simplified using defaultIfNull
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very useful thing. I think I'll use it in the future.
Thanks. Fixed.