method updateImages split into 3 methods
@@ -193,6 +193,18 @@ public class EventServiceImpl implements EventService {
private void updateImages(Event toUpdate, UpdateEventDto updateEventDto, MultipartFile[] images) {
if ((images == null || images.length == 0) && updateEventDto.getImagesToDelete() == null) {
+ changeOldImagesWithoutRemovingAndAdding(toUpdate, updateEventDto);
+ } else if (images == null || images.length == 0) {
+ deleteOldImages(toUpdate, updateEventDto);
+ } else if (updateEventDto.getImagesToDelete() == null) {
+ addNewImages(toUpdate, updateEventDto, images);
+ } else {
+ updateEventDto.getImagesToDelete().forEach(fileService::delete);
+ addNewImages(toUpdate, updateEventDto, images);
+ }
+ }
+
+ private void changeOldImagesWithoutRemovingAndAdding(Event toUpdate, UpdateEventDto updateEventDto) {
if (updateEventDto.getTitleImage() != null) {
toUpdate.setTitleImage(updateEventDto.getTitleImage());
} else {
@@ -204,7 +216,9 @@ public class EventServiceImpl implements EventService {
} else {
toUpdate.setAdditionalImages(null);
}
- } else if (images == null || images.length == 0) {
+ }
+
+ private void deleteOldImages(Event toUpdate, UpdateEventDto updateEventDto) {
updateEventDto.getImagesToDelete().forEach(fileService::delete);
if (updateEventDto.getTitleImage() != null) {
toUpdate.setTitleImage(updateEventDto.getTitleImage());
@@ -218,12 +232,6 @@ public class EventServiceImpl implements EventService {
} else {
toUpdate.setTitleImage(DEFAULT_TITLE_IMAGE_PATH);
}
- } else if (updateEventDto.getImagesToDelete() == null) {
- addNewImages(toUpdate, updateEventDto, images);
- } else {
- updateEventDto.getImagesToDelete().forEach(fileService::delete);
- addNewImages(toUpdate, updateEventDto, images);
- }
}
private void addNewImages(Event toUpdate, UpdateEventDto updateEventDto, MultipartFile[] images) {
