diff --git a/src/Ombi.Core/Engine/Interfaces/IMovieRequestEngine.cs b/src/Ombi.Core/Engine/Interfaces/IMovieRequestEngine.cs index 056632efc..5c3d4bcd3 100644 --- a/src/Ombi.Core/Engine/Interfaces/IMovieRequestEngine.cs +++ b/src/Ombi.Core/Engine/Interfaces/IMovieRequestEngine.cs @@ -23,6 +23,7 @@ namespace Ombi.Core.Engine.Interfaces Task> GetUnavailableRequests(int count, int position, string sortProperty, string sortOrder); + Task> GetRequestsByStatus(int count, int position, string sortProperty, string sortOrder, RequestStatus status); Task UpdateAdvancedOptions(MovieAdvancedOptions options); } } \ No newline at end of file diff --git a/src/Ombi.Core/Engine/Interfaces/ITvRequestEngine.cs b/src/Ombi.Core/Engine/Interfaces/ITvRequestEngine.cs index 1747351f4..e4402cede 100644 --- a/src/Ombi.Core/Engine/Interfaces/ITvRequestEngine.cs +++ b/src/Ombi.Core/Engine/Interfaces/ITvRequestEngine.cs @@ -25,5 +25,6 @@ namespace Ombi.Core.Engine.Interfaces Task UpdateQualityProfile(int requestId, int profileId); Task UpdateRootPath(int requestId, int rootPath); Task> GetRequests(int count, int position, string sortProperty, string sortOrder); + Task> GetRequests(int count, int position, string sortProperty, string sortOrder, RequestStatus status); } } \ No newline at end of file diff --git a/src/Ombi.Core/Engine/MovieRequestEngine.cs b/src/Ombi.Core/Engine/MovieRequestEngine.cs index ce7d1d1d4..08c2f1a9f 100644 --- a/src/Ombi.Core/Engine/MovieRequestEngine.cs +++ b/src/Ombi.Core/Engine/MovieRequestEngine.cs @@ -240,6 +240,69 @@ namespace Ombi.Core.Engine Total = total }; } + + public async Task> GetRequestsByStatus(int count, int position, string sortProperty, string sortOrder, RequestStatus status) + { + var shouldHide = await HideFromOtherUsers(); + IQueryable allRequests; + if (shouldHide.Hide) + { + allRequests = + MovieRepository.GetWithUser(shouldHide + .UserId); + } + else + { + allRequests = + MovieRepository + .GetWithUser(); + } + + switch (status) + { + case RequestStatus.PendingApproval: + allRequests = allRequests.Where(x => !x.Approved && !x.Available && (!x.Denied.HasValue || !x.Denied.Value)); + break; + case RequestStatus.ProcessingRequest: + allRequests = allRequests.Where(x => x.Approved && !x.Available && (!x.Denied.HasValue || !x.Denied.Value)); + break; + case RequestStatus.Available: + allRequests = allRequests.Where(x => x.Available && (!x.Denied.HasValue || !x.Denied.Value)); + break; + case RequestStatus.Denied: + allRequests = allRequests.Where(x => x.Denied.HasValue && x.Denied.Value); + break; + default: + break; + } + + var prop = TypeDescriptor.GetProperties(typeof(MovieRequests)).Find(sortProperty, true); + + if (sortProperty.Contains('.')) + { + // This is a navigation property currently not supported + prop = TypeDescriptor.GetProperties(typeof(MovieRequests)).Find("RequestedDate", true); + //var properties = sortProperty.Split(new []{'.'}, StringSplitOptions.RemoveEmptyEntries); + //var firstProp = TypeDescriptor.GetProperties(typeof(MovieRequests)).Find(properties[0], true); + //var propType = firstProp.PropertyType; + //var secondProp = TypeDescriptor.GetProperties(propType).Find(properties[1], true); + } + + // TODO fix this so we execute this on the server + var requests = sortOrder.Equals("asc", StringComparison.InvariantCultureIgnoreCase) + ? allRequests.ToList().OrderBy(x => x.RequestedDate).ToList() + : allRequests.ToList().OrderByDescending(x => prop.GetValue(x)).ToList(); + var total = requests.Count(); + requests = requests.Skip(position).Take(count).ToList(); + + await CheckForSubscription(shouldHide, requests); + return new RequestsViewModel + { + Collection = requests, + Total = total + }; + } + public async Task> GetUnavailableRequests(int count, int position, string sortProperty, string sortOrder) { var shouldHide = await HideFromOtherUsers(); diff --git a/src/Ombi.Core/Engine/TvRequestEngine.cs b/src/Ombi.Core/Engine/TvRequestEngine.cs index 8eb24f0b5..873b7064c 100644 --- a/src/Ombi.Core/Engine/TvRequestEngine.cs +++ b/src/Ombi.Core/Engine/TvRequestEngine.cs @@ -248,6 +248,79 @@ namespace Ombi.Core.Engine var total = allRequests.Count; + var prop = TypeDescriptor.GetProperties(typeof(ChildRequests)).Find(sortProperty, true); + + if (sortProperty.Contains('.')) + { + // This is a navigation property currently not supported + prop = TypeDescriptor.GetProperties(typeof(ChildRequests)).Find("Title", true); + //var properties = sortProperty.Split(new []{'.'}, StringSplitOptions.RemoveEmptyEntries); + //var firstProp = TypeDescriptor.GetProperties(typeof(MovieRequests)).Find(properties[0], true); + //var propType = firstProp.PropertyType; + //var secondProp = TypeDescriptor.GetProperties(propType).Find(properties[1], true); + } + allRequests = sortOrder.Equals("asc", StringComparison.InvariantCultureIgnoreCase) + ? allRequests.OrderBy(x => prop.GetValue(x)).ToList() + : allRequests.OrderByDescending(x => prop.GetValue(x)).ToList(); + + await CheckForSubscription(shouldHide, allRequests); + + // Make sure we do not show duplicate child requests + allRequests = allRequests.DistinctBy(x => x.ParentRequest.Title).ToList(); + + allRequests = allRequests.Skip(position).Take(count).ToList(); + + return new RequestsViewModel + { + Collection = allRequests, + Total = total, + }; + } + + public async Task> GetRequests(int count, int position, string sortProperty, string sortOrder, RequestStatus status) + { + var shouldHide = await HideFromOtherUsers(); + List allRequests; + if (shouldHide.Hide) + { + allRequests = await TvRepository.GetChild(shouldHide.UserId).ToListAsync(); + + // Filter out children + + FilterChildren(allRequests, shouldHide); + } + else + { + allRequests = await TvRepository.GetChild().ToListAsync(); + + } + + switch (status) + { + case RequestStatus.PendingApproval: + allRequests = allRequests.Where(x => !x.Approved && !x.Available && (!x.Denied.HasValue || !x.Denied.Value)).ToList(); + break; + case RequestStatus.ProcessingRequest: + allRequests = allRequests.Where(x => x.Approved && !x.Available && (!x.Denied.HasValue || !x.Denied.Value)).ToList(); + break; + case RequestStatus.Available: + allRequests = allRequests.Where(x => x.Available && (!x.Denied.HasValue || !x.Denied.Value)).ToList(); + break; + case RequestStatus.Denied: + allRequests = allRequests.Where(x => x.Denied.HasValue && x.Denied.Value).ToList(); + break; + default: + break; + } + + if (allRequests == null) + { + return new RequestsViewModel(); + } + + var total = allRequests.Count; + + var prop = TypeDescriptor.GetProperties(typeof(ChildRequests)).Find(sortProperty, true); if (sortProperty.Contains('.')) diff --git a/src/Ombi.Core/Models/Requests/RequestStatus.cs b/src/Ombi.Core/Models/Requests/RequestStatus.cs new file mode 100644 index 000000000..6810b139d --- /dev/null +++ b/src/Ombi.Core/Models/Requests/RequestStatus.cs @@ -0,0 +1,10 @@ +namespace Ombi.Core.Models.Requests +{ + public enum RequestStatus + { + PendingApproval, + ProcessingRequest, + Available, + Denied + } +} diff --git a/src/Ombi/Controllers/V2/RequestsController.cs b/src/Ombi/Controllers/V2/RequestsController.cs index 73b4f26af..ec6745721 100644 --- a/src/Ombi/Controllers/V2/RequestsController.cs +++ b/src/Ombi/Controllers/V2/RequestsController.cs @@ -36,6 +36,30 @@ namespace Ombi.Controllers.V2 return await _movieRequestEngine.GetRequests(count, position, sort, sortOrder); } + [HttpGet("movie/availble/{count:int}/{position:int}/{sort}/{sortOrder}")] + public async Task> GetAvailableRequests(int count, int position, string sort, string sortOrder) + { + return await _movieRequestEngine.GetRequestsByStatus(count, position, sort, sortOrder, RequestStatus.Available); + } + + [HttpGet("movie/processing/{count:int}/{position:int}/{sort}/{sortOrder}")] + public async Task> GetProcessingRequests(int count, int position, string sort, string sortOrder) + { + return await _movieRequestEngine.GetRequestsByStatus(count, position, sort, sortOrder, RequestStatus.ProcessingRequest); + } + + [HttpGet("movie/pending/{count:int}/{position:int}/{sort}/{sortOrder}")] + public async Task> GetPendingRequests(int count, int position, string sort, string sortOrder) + { + return await _movieRequestEngine.GetRequestsByStatus(count, position, sort, sortOrder, RequestStatus.PendingApproval); + } + + [HttpGet("movie/denied/{count:int}/{position:int}/{sort}/{sortOrder}")] + public async Task> GetDeniedRequests(int count, int position, string sort, string sortOrder) + { + return await _movieRequestEngine.GetRequestsByStatus(count, position, sort, sortOrder, RequestStatus.Denied); + } + /// /// Gets the unavailable movie requests. /// @@ -62,6 +86,30 @@ namespace Ombi.Controllers.V2 return await _tvRequestEngine.GetRequests(count, position, sort, sortOrder); } + [HttpGet("tv/pending/{count:int}/{position:int}/{sort}/{sortOrder}")] + public async Task> GetPendingTvRequests(int count, int position, string sort, string sortOrder) + { + return await _tvRequestEngine.GetRequests(count, position, sort, sortOrder, RequestStatus.PendingApproval); + } + + [HttpGet("tv/processing/{count:int}/{position:int}/{sort}/{sortOrder}")] + public async Task> GetProcessingTvRequests(int count, int position, string sort, string sortOrder) + { + return await _tvRequestEngine.GetRequests(count, position, sort, sortOrder, RequestStatus.ProcessingRequest); + } + + [HttpGet("tv/available/{count:int}/{position:int}/{sort}/{sortOrder}")] + public async Task> GetAvailableTvRequests(int count, int position, string sort, string sortOrder) + { + return await _tvRequestEngine.GetRequests(count, position, sort, sortOrder, RequestStatus.Available); + } + + [HttpGet("tv/denied/{count:int}/{position:int}/{sort}/{sortOrder}")] + public async Task> GetDeniedTvRequests(int count, int position, string sort, string sortOrder) + { + return await _tvRequestEngine.GetRequests(count, position, sort, sortOrder, RequestStatus.Denied); + } + /// /// Gets unavailable Tv requests. ///