Skip to content

Commit

Permalink
Update event for saving videos
Browse files Browse the repository at this point in the history
VideoData is going to be saved only if it has changed or is not present for the given video
  • Loading branch information
leingenm committed Oct 9, 2024
1 parent c6e145a commit 4cfd021
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions src/main/java/com/ypm/service/youtube/event/VideoEventListener.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ypm.service.youtube.event;

import com.ypm.constant.ProcessingStatus;
import com.ypm.dto.VideoDto;
import com.ypm.persistence.entity.Video;
import com.ypm.persistence.entity.VideoData;
import com.ypm.persistence.event.VideosSavedEvent;
import com.ypm.persistence.repository.VideoRepository;
Expand All @@ -27,22 +29,12 @@ public void handleVideosSavedEvent(VideosSavedEvent event) {
var videoDtoList = videoService.getVideoData(event.getVideoIds());

var videosToSave = videoDtoList.stream()
.filter(dto -> !dto.title().isEmpty() && !dto.description().isEmpty())
.map(dto -> videoRepository.findVideoByYoutubeId(dto.id()).map(video -> {
String tagsAsString = "";
if (dto.tags() != null) {
tagsAsString = String.join(", ", dto.tags());
}

var videoData = new VideoData(dto.title(), dto.description(), dto.channelName(), tagsAsString);
video.setVideoData(videoData);

if (video.getVideoData() != null) {
video.getVideoData().setVideo(video);
}

return video;
}).orElse(null)).filter(Objects::nonNull).toList();
.filter(this::isValidDtoData)
.map(dto -> videoRepository.findVideoByYoutubeId(dto.id())
.map(video -> addOrUpdateVideoData(dto, video))
.orElse(null))
.filter(Objects::nonNull)
.toList();

videoRepository.saveAll(videosToSave);
BatchStatusManager.updateBatchStatus(event.processingId(), ProcessingStatus.COMPLETED);
Expand All @@ -51,4 +43,38 @@ public void handleVideosSavedEvent(VideosSavedEvent event) {
throw new RuntimeException(e);
}
}

private Video addOrUpdateVideoData(VideoDto dto, Video video) {
if (video.getVideoData() == null) {
var videoData = new VideoData(dto.title(), dto.description(), dto.channelName(), tagsToString(dto));
video.setVideoData(videoData);
video.getVideoData().setVideo(video);
} else if (hasVideoDataChanged(video.getVideoData(), dto)) {
updateVideoData(video.getVideoData(), dto);
}

return video;
}

private boolean isValidDtoData(VideoDto dto) {
return !dto.title().isEmpty() && !dto.description().isEmpty();
}

private boolean hasVideoDataChanged(VideoData videoData, VideoDto dto) {
return !Objects.equals(videoData.getTitle(), dto.title())
|| !Objects.equals(videoData.getDescription(), dto.description())
|| !Objects.equals(videoData.getChannelName(), dto.channelName())
|| !Objects.equals(videoData.getTags(), dto.tags() != null ? tagsToString(dto) : null);
}

private void updateVideoData(VideoData videoData, VideoDto videoDto) {
videoData.setTitle(videoDto.title());
videoData.setDescription(videoDto.description());
videoData.setChannelName(videoDto.channelName());
videoData.setTags(videoDto.tags() != null ? tagsToString(videoDto) : null);
}

private String tagsToString(VideoDto videoDto) {
return String.join(", ", videoDto.tags());
}
}

0 comments on commit 4cfd021

Please sign in to comment.