Finished #221 and added more async #278

pull/332/head
tidusjar 9 years ago
parent 4114a8e9ba
commit 5b7e4aa5e3

@ -62,7 +62,7 @@ namespace PlexRequests.Api
request.AddUrlSegment("imdbid", imdbid); request.AddUrlSegment("imdbid", imdbid);
request.AddUrlSegment("title", title); request.AddUrlSegment("title", title);
var obj = RetryHandler.Execute<JObject>(() => Api.ExecuteJson<JObject> (request, baseUrl),new TimeSpan[] { var obj = RetryHandler.Execute(() => Api.ExecuteJson<JObject> (request, baseUrl),new[] {
TimeSpan.FromSeconds (2), TimeSpan.FromSeconds (2),
TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(5),
TimeSpan.FromSeconds(10)}, TimeSpan.FromSeconds(10)},

@ -49,7 +49,9 @@ namespace PlexRequests.Core
Task<RequestedModel> GetAsync(int id); Task<RequestedModel> GetAsync(int id);
IEnumerable<RequestedModel> GetAll(); IEnumerable<RequestedModel> GetAll();
Task<IEnumerable<RequestedModel>> GetAllAsync(); Task<IEnumerable<RequestedModel>> GetAllAsync();
bool BatchUpdate(List<RequestedModel> model); bool BatchUpdate(IEnumerable<RequestedModel> model);
bool BatchDelete(List<RequestedModel> model); Task<bool> BatchUpdateAsync(IEnumerable<RequestedModel> model);
bool BatchDelete(IEnumerable<RequestedModel> model);
Task<bool> BatchDeleteAsync(IEnumerable<RequestedModel> model);
} }
} }

@ -161,16 +161,26 @@ namespace PlexRequests.Core
.ToList(); .ToList();
} }
public bool BatchUpdate(List<RequestedModel> model) public bool BatchUpdate(IEnumerable<RequestedModel> model)
{ {
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList(); var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();
return Repo.UpdateAll(entities); return Repo.UpdateAll(entities);
} }
public async Task<bool> BatchUpdateAsync(IEnumerable<RequestedModel> model)
public bool BatchDelete(List<RequestedModel> model) {
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();
return await Repo.UpdateAllAsync(entities);
}
public bool BatchDelete(IEnumerable<RequestedModel> model)
{ {
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList(); var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();
return Repo.DeleteAll(entities); return Repo.DeleteAll(entities);
} }
public async Task<bool> BatchDeleteAsync(IEnumerable<RequestedModel> model)
{
var entities = model.Select(m => new RequestBlobs { Type = m.Type, Content = ByteConverterHelper.ReturnBytes(m), ProviderId = m.ProviderId, Id = m.Id }).ToList();
return await Repo.DeleteAllAsync(entities);
}
} }
} }

@ -190,6 +190,7 @@ $('#deleteMovies').click(function (e) {
} }
}); });
}); });
$('#deleteTVShows').click(function (e) { $('#deleteTVShows').click(function (e) {
e.preventDefault(); e.preventDefault();
if (!confirm("Are you sure you want to delete all TV show requests?")) return; if (!confirm("Are you sure you want to delete all TV show requests?")) return;
@ -223,6 +224,39 @@ $('#deleteTVShows').click(function (e) {
}); });
}); });
$('#deleteMusic').click(function (e) {
e.preventDefault();
if (!confirm("Are you sure you want to delete all album requests?")) return;
var buttonId = e.target.id;
var origHtml = $(this).html();
if ($('#' + buttonId).text() === " Loading...") {
return;
}
loadingButton(buttonId, "warning");
var url = createBaseUrl(base, '/approval/deletealltvshows');
$.ajax({
type: 'post',
url: url,
dataType: "json",
success: function (response) {
if (checkJsonResponse(response)) {
generateNotify("Success! All TV Show requests deleted!", "success");
tvLoad();
}
},
error: function (e) {
console.log(e);
generateNotify("Something went wrong!", "danger");
},
complete: function (e) {
finishLoading(buttonId, "success", origHtml);
}
});
});
// filtering/sorting // filtering/sorting
$('.filter,.sort', '.dropdown-menu').click(function (e) { $('.filter,.sort', '.dropdown-menu').click(function (e) {
var $this = $(this); var $this = $(this);

@ -28,6 +28,8 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Threading.Tasks;
using Nancy; using Nancy;
using Nancy.Security; using Nancy.Security;
@ -62,12 +64,13 @@ namespace PlexRequests.UI.Modules
HeadphonesSettings = hpSettings; HeadphonesSettings = hpSettings;
HeadphoneApi = hpApi; HeadphoneApi = hpApi;
Post["/approve"] = parameters => Approve((int)Request.Form.requestid, (string)Request.Form.qualityId); Post["/approve", true] = async (x, ct) => await Approve((int)Request.Form.requestid, (string)Request.Form.qualityId);
Post["/approveall"] = x => ApproveAll(); Post["/approveall", true] = async (x, ct) => await ApproveAll();
Post["/approveallmovies"] = x => ApproveAllMovies(); Post["/approveallmovies", true] = async (x, ct) => await ApproveAllMovies();
Post["/approvealltvshows"] = x => ApproveAllTVShows(); Post["/approvealltvshows", true] = async (x, ct) => await ApproveAllTVShows();
Post["/deleteallmovies"] = x => DeleteAllMovies(); Post["/deleteallmovies", true] = async (x, ct) => await DeleteAllMovies();
Post["/deletealltvshows"] = x => DeleteAllTVShows(); Post["/deletealltvshows", true] = async (x, ct) => await DeleteAllTVShows();
Post["/deleteallalbums", true] = async (x, ct) => await DeleteAllAlbums();
} }
private IRequestService Service { get; } private IRequestService Service { get; }
@ -87,12 +90,12 @@ namespace PlexRequests.UI.Modules
/// </summary> /// </summary>
/// <param name="requestId">The request identifier.</param> /// <param name="requestId">The request identifier.</param>
/// <returns></returns> /// <returns></returns>
private Response Approve(int requestId, string qualityId) private async Task<Response> Approve(int requestId, string qualityId)
{ {
Log.Info("approving request {0}", requestId); Log.Info("approving request {0}", requestId);
// Get the request from the DB // Get the request from the DB
var request = Service.Get(requestId); var request = await Service.GetAsync(requestId);
if (request == null) if (request == null)
{ {
@ -103,21 +106,21 @@ namespace PlexRequests.UI.Modules
switch (request.Type) switch (request.Type)
{ {
case RequestType.Movie: case RequestType.Movie:
return RequestMovieAndUpdateStatus(request, qualityId); return await RequestMovieAndUpdateStatus(request, qualityId);
case RequestType.TvShow: case RequestType.TvShow:
return RequestTvAndUpdateStatus(request, qualityId); return await RequestTvAndUpdateStatus(request, qualityId);
case RequestType.Album: case RequestType.Album:
return RequestAlbumAndUpdateStatus(request); return await RequestAlbumAndUpdateStatus(request);
default: default:
throw new ArgumentOutOfRangeException(nameof(request)); throw new ArgumentOutOfRangeException(nameof(request));
} }
} }
private Response RequestTvAndUpdateStatus(RequestedModel request, string qualityId) private async Task<Response> RequestTvAndUpdateStatus(RequestedModel request, string qualityId)
{ {
var sender = new TvSender(SonarrApi, SickRageApi); var sender = new TvSender(SonarrApi, SickRageApi);
var sonarrSettings = SonarrSettings.GetSettings(); var sonarrSettings = await SonarrSettings.GetSettingsAsync();
if (sonarrSettings.Enabled) if (sonarrSettings.Enabled)
{ {
Log.Trace("Sending to Sonarr"); Log.Trace("Sending to Sonarr");
@ -128,7 +131,7 @@ namespace PlexRequests.UI.Modules
{ {
Log.Info("Sent successfully, Approving request now."); Log.Info("Sent successfully, Approving request now.");
request.Approved = true; request.Approved = true;
var requestResult = Service.UpdateRequest(request); var requestResult = await Service.UpdateRequestAsync(request);
Log.Trace("Approval result: {0}", requestResult); Log.Trace("Approval result: {0}", requestResult);
if (requestResult) if (requestResult)
{ {
@ -145,7 +148,7 @@ namespace PlexRequests.UI.Modules
} }
var srSettings = SickRageSettings.GetSettings(); var srSettings = await SickRageSettings.GetSettingsAsync();
if (srSettings.Enabled) if (srSettings.Enabled)
{ {
Log.Trace("Sending to SickRage"); Log.Trace("Sending to SickRage");
@ -156,7 +159,7 @@ namespace PlexRequests.UI.Modules
{ {
Log.Info("Sent successfully, Approving request now."); Log.Info("Sent successfully, Approving request now.");
request.Approved = true; request.Approved = true;
var requestResult = Service.UpdateRequest(request); var requestResult = await Service.UpdateRequestAsync(request);
Log.Trace("Approval result: {0}", requestResult); Log.Trace("Approval result: {0}", requestResult);
return Response.AsJson(requestResult return Response.AsJson(requestResult
? new JsonResponseModel { Result = true } ? new JsonResponseModel { Result = true }
@ -171,15 +174,15 @@ namespace PlexRequests.UI.Modules
request.Approved = true; request.Approved = true;
var res = Service.UpdateRequest(request); var res = await Service.UpdateRequestAsync(request);
return Response.AsJson(res return Response.AsJson(res
? new JsonResponseModel { Result = true, Message = "This has been approved, but It has not been sent to Sonarr/SickRage because it has not been configured" } ? new JsonResponseModel { Result = true, Message = "This has been approved, but It has not been sent to Sonarr/SickRage because it has not been configured" }
: new JsonResponseModel { Result = false, Message = "Updated SickRage but could not approve it in PlexRequests :(" }); : new JsonResponseModel { Result = false, Message = "Updated SickRage but could not approve it in PlexRequests :(" });
} }
private Response RequestMovieAndUpdateStatus(RequestedModel request, string qualityId) private async Task<Response> RequestMovieAndUpdateStatus(RequestedModel request, string qualityId)
{ {
var cpSettings = CpService.GetSettings(); var cpSettings = await CpService.GetSettingsAsync();
Log.Info("Adding movie to CouchPotato : {0}", request.Title); Log.Info("Adding movie to CouchPotato : {0}", request.Title);
if (!cpSettings.Enabled) if (!cpSettings.Enabled)
@ -189,7 +192,7 @@ namespace PlexRequests.UI.Modules
Log.Warn("We approved movie: {0} but could not add it to CouchPotato because it has not been setup", request.Title); Log.Warn("We approved movie: {0} but could not add it to CouchPotato because it has not been setup", request.Title);
// Update the record // Update the record
var inserted = Service.UpdateRequest(request); var inserted = await Service.UpdateRequestAsync(request);
return Response.AsJson(inserted return Response.AsJson(inserted
? new JsonResponseModel { Result = true, Message = "This has been approved, but It has not been sent to CouchPotato because it has not been configured." } ? new JsonResponseModel { Result = true, Message = "This has been approved, but It has not been sent to CouchPotato because it has not been configured." }
: new JsonResponseModel : new JsonResponseModel
@ -207,7 +210,7 @@ namespace PlexRequests.UI.Modules
request.Approved = true; request.Approved = true;
// Update the record // Update the record
var inserted = Service.UpdateRequest(request); var inserted = await Service.UpdateRequestAsync(request);
return Response.AsJson(inserted return Response.AsJson(inserted
? new JsonResponseModel { Result = true } ? new JsonResponseModel { Result = true }
@ -227,9 +230,9 @@ namespace PlexRequests.UI.Modules
}); });
} }
private Response RequestAlbumAndUpdateStatus(RequestedModel request) private async Task<Response> RequestAlbumAndUpdateStatus(RequestedModel request)
{ {
var hpSettings = HeadphonesSettings.GetSettings(); var hpSettings = await HeadphonesSettings.GetSettingsAsync();
Log.Info("Adding album to Headphones : {0}", request.Title); Log.Info("Adding album to Headphones : {0}", request.Title);
if (!hpSettings.Enabled) if (!hpSettings.Enabled)
{ {
@ -238,7 +241,7 @@ namespace PlexRequests.UI.Modules
Log.Warn("We approved Album: {0} but could not add it to Headphones because it has not been setup", request.Title); Log.Warn("We approved Album: {0} but could not add it to Headphones because it has not been setup", request.Title);
// Update the record // Update the record
var inserted = Service.UpdateRequest(request); var inserted = await Service.UpdateRequestAsync(request);
return Response.AsJson(inserted return Response.AsJson(inserted
? new JsonResponseModel { Result = true, Message = "This has been approved, but It has not been sent to Headphones because it has not been configured." } ? new JsonResponseModel { Result = true, Message = "This has been approved, but It has not been sent to Headphones because it has not been configured." }
: new JsonResponseModel : new JsonResponseModel
@ -255,10 +258,11 @@ namespace PlexRequests.UI.Modules
return Response.AsJson(new JsonResponseModel { Result = true, Message = "We have sent the approval to Headphones for processing, This can take a few minutes." }); return Response.AsJson(new JsonResponseModel { Result = true, Message = "We have sent the approval to Headphones for processing, This can take a few minutes." });
} }
private Response ApproveAllMovies() private async Task<Response> ApproveAllMovies()
{ {
var requests = Service.GetAll().Where(x => x.CanApprove && x.Type == RequestType.Movie); var requests = await Service.GetAllAsync();
requests = requests.Where(x => x.CanApprove && x.Type == RequestType.Movie);
var requestedModels = requests as RequestedModel[] ?? requests.ToArray(); var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!requestedModels.Any()) if (!requestedModels.Any())
{ {
@ -267,7 +271,7 @@ namespace PlexRequests.UI.Modules
try try
{ {
return UpdateRequests(requestedModels); return await UpdateRequestsAsync(requestedModels);
} }
catch (Exception e) catch (Exception e)
{ {
@ -276,10 +280,11 @@ namespace PlexRequests.UI.Modules
} }
} }
private Response DeleteAllMovies() private async Task<Response> DeleteAllMovies()
{ {
var requests = Service.GetAll().Where(x => x.Type == RequestType.Movie); var requests = await Service.GetAllAsync();
requests = requests.Where(x => x.Type == RequestType.Movie);
var requestedModels = requests as RequestedModel[] ?? requests.ToArray(); var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!requestedModels.Any()) if (!requestedModels.Any())
{ {
@ -288,7 +293,29 @@ namespace PlexRequests.UI.Modules
try try
{ {
return DeleteRequests(requestedModels); return await DeleteRequestsAsync(requestedModels);
}
catch (Exception e)
{
Log.Fatal(e);
return Response.AsJson(new JsonResponseModel { Result = false, Message = "Something bad happened, please check the logs!" });
}
}
private async Task<Response> DeleteAllAlbums()
{
var requests = await Service.GetAllAsync();
requests = requests.Where(x => x.Type == RequestType.Album);
var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!requestedModels.Any())
{
return Response.AsJson(new JsonResponseModel { Result = false, Message = "There are no album requests to delete. Please refresh." });
}
try
{
return await DeleteRequestsAsync(requestedModels);
} }
catch (Exception e) catch (Exception e)
{ {
@ -297,9 +324,10 @@ namespace PlexRequests.UI.Modules
} }
} }
private Response ApproveAllTVShows() private async Task<Response> ApproveAllTVShows()
{ {
var requests = Service.GetAll().Where(x => x.CanApprove && x.Type == RequestType.TvShow); var requests = await Service.GetAllAsync();
requests = requests.Where(x => x.CanApprove && x.Type == RequestType.TvShow);
var requestedModels = requests as RequestedModel[] ?? requests.ToArray(); var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!requestedModels.Any()) if (!requestedModels.Any())
{ {
@ -308,7 +336,7 @@ namespace PlexRequests.UI.Modules
try try
{ {
return UpdateRequests(requestedModels); return await UpdateRequestsAsync(requestedModels);
} }
catch (Exception e) catch (Exception e)
{ {
@ -317,10 +345,11 @@ namespace PlexRequests.UI.Modules
} }
} }
private Response DeleteAllTVShows() private async Task<Response> DeleteAllTVShows()
{ {
var requests = Service.GetAll().Where(x => x.Type == RequestType.TvShow); var requests = await Service.GetAllAsync();
requests = requests.Where(x => x.Type == RequestType.TvShow);
var requestedModels = requests as RequestedModel[] ?? requests.ToArray(); var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!requestedModels.Any()) if (!requestedModels.Any())
{ {
@ -329,7 +358,7 @@ namespace PlexRequests.UI.Modules
try try
{ {
return DeleteRequests(requestedModels); return await DeleteRequestsAsync(requestedModels);
} }
catch (Exception e) catch (Exception e)
{ {
@ -342,9 +371,10 @@ namespace PlexRequests.UI.Modules
/// Approves all. /// Approves all.
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
private Response ApproveAll() private async Task<Response> ApproveAll()
{ {
var requests = Service.GetAll().Where(x => x.CanApprove); var requests = await Service.GetAllAsync();
requests = requests.Where(x => x.CanApprove);
var requestedModels = requests as RequestedModel[] ?? requests.ToArray(); var requestedModels = requests as RequestedModel[] ?? requests.ToArray();
if (!requestedModels.Any()) if (!requestedModels.Any())
{ {
@ -353,7 +383,7 @@ namespace PlexRequests.UI.Modules
try try
{ {
return UpdateRequests(requestedModels); return await UpdateRequestsAsync(requestedModels);
} }
catch (Exception e) catch (Exception e)
{ {
@ -363,11 +393,11 @@ namespace PlexRequests.UI.Modules
} }
private Response DeleteRequests(RequestedModel[] requestedModels) private async Task<Response> DeleteRequestsAsync(IEnumerable<RequestedModel> requestedModels)
{ {
try try
{ {
var result = Service.BatchDelete(requestedModels.ToList()); var result = await Service.BatchDeleteAsync(requestedModels);
return Response.AsJson(result return Response.AsJson(result
? new JsonResponseModel { Result = true } ? new JsonResponseModel { Result = true }
: new JsonResponseModel { Result = false, Message = "We could not delete all of the requests. Please try again or check the logs." }); : new JsonResponseModel { Result = false, Message = "We could not delete all of the requests. Please try again or check the logs." });
@ -379,9 +409,9 @@ namespace PlexRequests.UI.Modules
} }
} }
private Response UpdateRequests(RequestedModel[] requestedModels) private async Task<Response> UpdateRequestsAsync(RequestedModel[] requestedModels)
{ {
var cpSettings = CpService.GetSettings(); var cpSettings = await CpService.GetSettingsAsync();
var updatedRequests = new List<RequestedModel>(); var updatedRequests = new List<RequestedModel>();
foreach (var r in requestedModels) foreach (var r in requestedModels)
{ {
@ -409,8 +439,8 @@ namespace PlexRequests.UI.Modules
if (r.Type == RequestType.TvShow) if (r.Type == RequestType.TvShow)
{ {
var sender = new TvSender(SonarrApi, SickRageApi); var sender = new TvSender(SonarrApi, SickRageApi);
var sr = SickRageSettings.GetSettings(); var sr = await SickRageSettings.GetSettingsAsync();
var sonarr = SonarrSettings.GetSettings(); var sonarr = await SonarrSettings.GetSettingsAsync();
if (sr.Enabled) if (sr.Enabled)
{ {
var res = sender.SendToSickRage(sr, r); var res = sender.SendToSickRage(sr, r);
@ -449,7 +479,7 @@ namespace PlexRequests.UI.Modules
} }
try try
{ {
var result = Service.BatchUpdate(updatedRequests); var result = await Service.BatchUpdateAsync(updatedRequests);
return Response.AsJson(result return Response.AsJson(result
? new JsonResponseModel { Result = true } ? new JsonResponseModel { Result = true }
: new JsonResponseModel { Result = false, Message = "We could not approve all of the requests. Please try again or check the logs." }); : new JsonResponseModel { Result = false, Message = "We could not approve all of the requests. Please try again or check the logs." });

@ -50,7 +50,7 @@
} }
@if (Model.SearchForMusic) @if (Model.SearchForMusic)
{ {
<button id="deleteMusics" class="btn btn-warning-outline delete-category" type="submit" style="display: none;"><i class="fa fa-trash"></i> Delete Music</button> <button id="deleteMusic" class="btn btn-warning-outline delete-category" type="submit" style="display: none;"><i class="fa fa-trash"></i> Delete Music</button>
<button id="approveMusic" class="btn btn-success-outline approve-category" type="submit" style="display: none;"><i class="fa fa-plus"></i> Approve Music</button> <button id="approveMusic" class="btn btn-success-outline approve-category" type="submit" style="display: none;"><i class="fa fa-plus"></i> Approve Music</button>
} }
} }

Loading…
Cancel
Save