Got mostly everything working for #254 Ready for testing

This commit is contained in:
tidusjar 2016-08-07 18:54:49 +01:00
commit 0699d777d6
7 changed files with 84 additions and 40 deletions

View file

@ -592,6 +592,7 @@ function tvLoad() {
context.episodes = tvObject;
var html = searchTemplate(context);
$tvl.append(html);
tvObject = new Array();
});

View file

@ -75,7 +75,7 @@ namespace PlexRequests.UI.Helpers
var seriesTask = GetSonarrSeries(sonarrSettings, model.ProviderId);
if (episodeRequest)
{
{
// Does series exist?
var series = await seriesTask;
if (series != null)
@ -83,6 +83,7 @@ namespace PlexRequests.UI.Helpers
// Series Exists
// Request the episodes in the existing series
await RequestEpisodesWithExistingSeries(model, series, sonarrSettings);
return new SonarrAddSeries {title = series.title};
}
else
{
@ -91,29 +92,22 @@ namespace PlexRequests.UI.Helpers
sonarrSettings.SeasonFolders, sonarrSettings.RootPath, 0, new int[0], sonarrSettings.ApiKey,
sonarrSettings.FullUri, false));
if (string.IsNullOrEmpty(addResult?.title))
// Get the series that was just added
series = await GetSonarrSeries(sonarrSettings, model.ProviderId);
series.monitored = false; // Un-monitor the series
// Un-monitor all seasons
foreach (var season in series.seasons)
{
var sw = new Stopwatch();
sw.Start();
while (series == null)
{
await Task.Delay(TimeSpan.FromSeconds(5));
series = await GetSonarrSeries(sonarrSettings, model.ProviderId);
// Check how long we have been doing this for
if (sw.Elapsed > TimeSpan.FromSeconds(30))
{
// 30 seconds is a long time, it's not going to work.
throw new ApiRequestException("Sonarr still didn't have the series added after 30 seconds.");
}
}
sw.Stop();
// Update the series, Since we cannot add as unmonitoed due to the following bug: https://github.com/Sonarr/Sonarr/issues/1404
SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
season.monitored = false;
}
// We now have the series in Sonarr
// Update the series, Since we cannot add as un-monitored due to the following bug: https://github.com/Sonarr/Sonarr/issues/1404
SonarrApi.UpdateSeries(series, sonarrSettings.ApiKey, sonarrSettings.FullUri);
// We now have the series in Sonarr, update it to request the episodes.
await RequestEpisodesWithExistingSeries(model, series, sonarrSettings);
return addResult;

View file

@ -585,8 +585,16 @@ namespace PlexRequests.UI.Modules
if (episodeRequest)
{
difference = GetListDifferences(existingRequest.Episodes, episodeModel.Episodes).ToList();
if (!difference.Any())
if (difference.Any())
{
existingRequest.Episodes = episodeModel.Episodes
.Select(r =>
new EpisodesModel
{
SeasonNumber = r.SeasonNumber,
EpisodeNumber = r.EpisodeNumber
}).ToList();
return await AddUserToRequest(existingRequest, settings, fullShowName);
}
// We have an episode that has not yet been requested, let's continue
@ -730,7 +738,7 @@ namespace PlexRequests.UI.Modules
private async Task<Response> AddUserToRequest(RequestedModel existingRequest, PlexRequestSettings settings, string fullShowName)
{
// check if the current user is already marked as a requester for this show, if not, add them
if (!existingRequest.UserHasRequested(Username))
if (!existingRequest.UserHasRequested(Username) || existingRequest.Episodes.Any())
{
existingRequest.RequestedUsers.Add(Username);
await RequestService.UpdateRequestAsync(existingRequest);
@ -1092,17 +1100,17 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(new JsonResponseModel { Result = true, Message = message });
}
private IEnumerable<Store.EpisodesModel> GetListDifferences(IEnumerable<Store.EpisodesModel> model, IEnumerable<Models.EpisodesModel> request)
private IEnumerable<Store.EpisodesModel> GetListDifferences(IEnumerable<EpisodesModel> existing, IEnumerable<Models.EpisodesModel> request)
{
var newRequest = request
.Select(r =>
new Store.EpisodesModel
new EpisodesModel
{
SeasonNumber = r.SeasonNumber,
EpisodeNumber = r.EpisodeNumber
}).ToList();
return newRequest.Except(model);
return newRequest.Except(existing);
}
}
}