use of com.furyviewer.service.dto.util.EpisodesHomeDTO in project FuryViewer by TheDoctor-95.
the class EpisodeService method getNextEpisodes.
public List<EpisodesHomeDTO> getNextEpisodes() {
List<EpisodesHomeDTO> episodes = new ArrayList<>();
seriesStatsRepository.followingSeriesUser(SecurityUtils.getCurrentUserLogin()).forEach(series -> {
Optional<Season> maxSeasonSeenOptional = chapterSeenRepository.findBySeenAndEpisodeSeasonSeriesIdAndUserLogin(true, series.getId(), SecurityUtils.getCurrentUserLogin()).stream().map(chapterSeen -> chapterSeen.getEpisode().getSeason()).max(Comparator.comparing(com.furyviewer.domain.Season::getNumber));
EpisodesHomeDTO episodesHomeDTO = new EpisodesHomeDTO();
Episode nextEpisode;
if (maxSeasonSeenOptional.isPresent()) {
Season maxSeasonSeen = maxSeasonSeenOptional.get();
Episode maxEpisodeSeen = chapterSeenRepository.findBySeenAndEpisodeSeasonSeriesIdAndUserLogin(true, series.getId(), SecurityUtils.getCurrentUserLogin()).stream().map(chapterSeen -> chapterSeen.getEpisode()).filter(episode -> episode.getSeason().equals(maxSeasonSeen)).max(Comparator.comparing(com.furyviewer.domain.Episode::getNumber)).get();
if (maxEpisodeSeen.getNumber() < maxSeasonSeen.getEpisodes().size()) {
nextEpisode = episodeRepository.findByNumberAndSeasonNumberAndSeasonSeriesId(maxEpisodeSeen.getNumber() + 1, maxSeasonSeen.getNumber(), series.getId());
} else {
nextEpisode = episodeRepository.findByNumberAndSeasonNumberAndSeasonSeriesId(1, maxSeasonSeen.getNumber() + 1, series.getId());
}
} else {
nextEpisode = episodeRepository.findByNumberAndSeasonNumberAndSeasonSeriesId(1, 1, series.getId());
}
if (nextEpisode != null && (nextEpisode.getReleaseDate().isBefore(LocalDate.now()) || nextEpisode.getReleaseDate().isEqual(LocalDate.now()))) {
episodesHomeDTO.setEpisodeNumber(nextEpisode.getNumber());
episodesHomeDTO.setId(nextEpisode.getSeason().getSeries().getId());
episodesHomeDTO.setSeasonNumber(nextEpisode.getSeason().getNumber());
episodesHomeDTO.setTitleEpisode(nextEpisode.getName());
episodesHomeDTO.setTitleSeries(nextEpisode.getSeason().getSeries().getName());
episodesHomeDTO.setUrlCartel(nextEpisode.getSeason().getSeries().getImgUrl());
episodes.add(episodesHomeDTO);
}
});
return episodes;
}
Aggregations