public async Task <TvShowRequestBuilder> BuildEpisodes(TvRequestViewModel tv) { if (tv.RequestAll) { var episodes = await TvApi.EpisodeLookup(ShowInfo.id); foreach (var ep in episodes) { var season = ChildRequest.SeasonRequests.FirstOrDefault(x => x.SeasonNumber == ep.season); if (season == null) { ChildRequest.SeasonRequests.Add(new SeasonRequests { Episodes = new List <EpisodeRequests> { new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url.ToHttpsUrl() } }, SeasonNumber = ep.season, }); } else { season.Episodes.Add(new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url.ToHttpsUrl() }); } } } else if (tv.LatestSeason) { var episodes = await TvApi.EpisodeLookup(ShowInfo.id); var latest = episodes.OrderByDescending(x => x.season).FirstOrDefault(); var episodesRequests = new List <EpisodeRequests>(); foreach (var ep in episodes) { if (ep.season == latest.season) { episodesRequests.Add(new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url.ToHttpsUrl() }); } } ChildRequest.SeasonRequests.Add(new SeasonRequests { Episodes = episodesRequests, SeasonNumber = latest.season, }); } else if (tv.FirstSeason) { var episodes = await TvApi.EpisodeLookup(ShowInfo.id); var first = episodes.OrderBy(x => x.season).FirstOrDefault(); var episodesRequests = new List <EpisodeRequests>(); foreach (var ep in episodes) { if (ep.season == first.season) { episodesRequests.Add(new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url.ToHttpsUrl() }); } } ChildRequest.SeasonRequests.Add(new SeasonRequests { Episodes = episodesRequests, SeasonNumber = first.season, }); } else { // It's a custom request var seasonRequests = new List <SeasonRequests>(); var episodes = await TvApi.EpisodeLookup(ShowInfo.id); foreach (var ep in episodes) { var existingSeasonRequest = seasonRequests.FirstOrDefault(x => x.SeasonNumber == ep.season); if (existingSeasonRequest != null) { var requestedSeason = tv.Seasons.FirstOrDefault(x => x.SeasonNumber == ep.season); var requestedEpisode = requestedSeason?.Episodes?.Any(x => x.EpisodeNumber == ep.number) ?? false; if (requestedSeason != null && requestedEpisode) { // We already have this, let's just add the episodes to it existingSeasonRequest.Episodes.Add(new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url.ToHttpsUrl() }); } } else { var newRequest = new SeasonRequests { SeasonNumber = ep.season }; var requestedSeason = tv.Seasons.FirstOrDefault(x => x.SeasonNumber == ep.season); var requestedEpisode = requestedSeason?.Episodes?.Any(x => x.EpisodeNumber == ep.number) ?? false; if (requestedSeason != null && requestedEpisode) { newRequest.Episodes.Add(new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url.ToHttpsUrl() }); seasonRequests.Add(newRequest); } } } foreach (var s in seasonRequests) { ChildRequest.SeasonRequests.Add(s); } } return(this); }
public async Task <TvShowRequestBuilder> BuildEpisodes(SearchTvShowViewModel tv) { if (tv.RequestAll) { var episodes = await TvApi.EpisodeLookup(ShowInfo.id); foreach (var ep in episodes) { var season = ChildRequest.SeasonRequests.FirstOrDefault(x => x.SeasonNumber == ep.season); if (season == null) { ChildRequest.SeasonRequests.Add(new SeasonRequests { Episodes = new List <EpisodeRequests> { new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url } }, SeasonNumber = ep.season, }); } else { season.Episodes.Add(new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url }); } } } else if (tv.LatestSeason) { var episodes = await TvApi.EpisodeLookup(ShowInfo.id); var latest = episodes.OrderByDescending(x => x.season).FirstOrDefault(); var episodesRequests = new List <EpisodeRequests>(); foreach (var ep in episodes) { if (ep.season == latest.season) { episodesRequests.Add(new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url }); } } ChildRequest.SeasonRequests.Add(new SeasonRequests { Episodes = episodesRequests, SeasonNumber = latest.season, }); } else if (tv.FirstSeason) { var episodes = await TvApi.EpisodeLookup(ShowInfo.id); var first = episodes.OrderBy(x => x.season).FirstOrDefault(); var episodesRequests = new List <EpisodeRequests>(); foreach (var ep in episodes) { if (ep.season == first.season) { episodesRequests.Add(new EpisodeRequests { EpisodeNumber = ep.number, AirDate = FormatDate(ep.airdate), Title = ep.name, Url = ep.url }); } } ChildRequest.SeasonRequests.Add(new SeasonRequests { Episodes = episodesRequests, SeasonNumber = first.season, }); } else { // It's a custom request ChildRequest.SeasonRequests = TvRequests; } return(this); }