@ -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 . Get Async ( 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 . GetSettings Async ( ) ;
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 . UpdateRequest Async ( 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 . GetSettings Async ( ) ;
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 . UpdateRequest Async ( 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 . UpdateRequest Async ( 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 . GetSettings Async ( ) ;
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 . UpdateRequest Async ( 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 . UpdateRequest Async ( 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 . GetSettings Async ( ) ;
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 . UpdateRequest Async ( 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 UpdateRequests Async ( 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 UpdateRequests Async ( 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 DeleteRequests Async ( 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 UpdateRequests Async ( 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 . BatchDelete Async ( 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 > UpdateRequests Async ( RequestedModel [ ] requestedModels )
{
{
var cpSettings = CpService . GetSettings ( ) ;
var cpSettings = await CpService . GetSettings Async ( ) ;
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 . GetSettings Async ( ) ;
var sonarr = SonarrSettings . GetSettings ( ) ;
var sonarr = await SonarrSettings . GetSettings Async ( ) ;
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 . BatchUpdate Async ( 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." } ) ;