package com.example.application.data.service; import com.example.application.data.entity.Matchday; import com.example.application.data.entity.Season; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; import org.springframework.stereotype.Service; import org.vaadin.artur.helpers.CrudService; import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; @Service public class MatchdayService extends CrudService { private final MatchdayRepository repository; public MatchdayService(@Autowired MatchdayRepository repository) { this.repository = repository; } @Override protected MatchdayRepository getRepository() { return repository; } @NonNull public List getMatchdaysForSeasonSorted(@NonNull Season season) { return repository.findAll().stream() .filter(matchday -> matchday.getSeason().equals(season)) .sorted(Comparator.comparingInt(Matchday::getNumber)) .collect(Collectors.toList()); } @NonNull public List getMatchdaysWithActivityForSeasonSorted(@NonNull Season season) { return repository.findAll().stream() .filter(matchday -> matchday.getSeason().equals(season)) .filter(this::hasActivity) .sorted(Comparator.comparingInt(Matchday::getNumber)) .collect(Collectors.toList()); } @Nullable public Matchday getLastMatchdayWithActivityForSeason(@NonNull Season season) { List matchdaysWithActivity = getMatchdaysWithActivityForSeasonSorted(season); if (matchdaysWithActivity.isEmpty()) { return null; } return matchdaysWithActivity.get(matchdaysWithActivity.size()-1); } public boolean hasActivity(@NonNull Matchday matchday) { return matchday.getMatches().stream().mapToInt(match -> match.getGames().size()).sum() != 0; } }