From 1b5698b3ca21890fcabbed7d3a1091580ea18169 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Tue, 16 May 2017 16:09:06 +0100 Subject: [PATCH] Reworked the TV model AGAIN #865 --- src/Ombi.Core/Engine/TvRequestEngine.cs | 102 +++++++++--------- src/Ombi.Core/Engine/TvSearchEngine.cs | 32 +++--- .../Models/Requests/Tv/TvRequestModel.cs | 27 +++-- src/Ombi.Helpers/ByteConverterHelper.cs | 9 +- src/Ombi/.gitignore | 2 + src/Ombi/Controllers/RequestController.cs | 14 ++- src/Ombi/Ombi.csproj | 5 + .../wwwroot/app/interfaces/IRequestModel.ts | 21 ++-- .../app/requests/tvrequests.component.html | 43 ++++---- src/Ombi/wwwroot/systemjs.config.ts | 2 +- 10 files changed, 142 insertions(+), 115 deletions(-) diff --git a/src/Ombi.Core/Engine/TvRequestEngine.cs b/src/Ombi.Core/Engine/TvRequestEngine.cs index 610b52629..d288737d3 100644 --- a/src/Ombi.Core/Engine/TvRequestEngine.cs +++ b/src/Ombi.Core/Engine/TvRequestEngine.cs @@ -33,10 +33,11 @@ namespace Ombi.Core.Engine var showInfo = await TvApi.ShowLookupByTheTvDbId(tv.Id); DateTime.TryParse(showInfo.premiered, out DateTime firstAir); - + // For some reason the poster path is always http var posterPath = showInfo.image?.medium.Replace("http:", "https:"); - var model = new TvRequestModel + + var childRequest = new ChildTvRequest { Id = tv.Id, Type = RequestType.TvShow, @@ -49,30 +50,50 @@ namespace Ombi.Core.Engine Approved = false, RequestedUsers = new List { Username }, Issues = IssueState.None, + ProviderId = tv.Id, + RequestAll = tv.RequestAll, + SeasonRequests = tv.SeasonRequests, + }; + + var model = new TvRequestModel + { + Id = tv.Id, + Type = RequestType.TvShow, + Overview = showInfo.summary.RemoveHtml(), + PosterPath = posterPath, + Title = showInfo.name, + ReleaseDate = firstAir, + Status = showInfo.status, + Approved = false, ImdbId = showInfo.externals?.imdb ?? string.Empty, TvDbId = tv.Id.ToString(), ProviderId = tv.Id, - RequestAll = tv.RequestAll + }; - var episodes = await TvApi.EpisodeLookup(showInfo.id); + model.ChildRequests.Add(childRequest); - foreach (var e in episodes) + if (childRequest.SeasonRequests.Any()) { - var season = model.SeasonRequests.FirstOrDefault(x => x.SeasonNumber == e.season); - season?.Episodes.Add(new EpisodesRequested + var episodes = await TvApi.EpisodeLookup(showInfo.id); + + foreach (var e in episodes) { - Url = e.url, - Title = e.name, - AirDate = DateTime.Parse(e.airstamp), - EpisodeNumber = e.number, - }); + var season = childRequest.SeasonRequests.FirstOrDefault(x => x.SeasonNumber == e.season); + season?.Episodes.Add(new EpisodesRequested + { + Url = e.url, + Title = e.name, + AirDate = DateTime.Parse(e.airstamp), + EpisodeNumber = e.number, + }); + } } if (tv.LatestSeason) { var latest = showInfo.Season.OrderBy(x => x.SeasonNumber).FirstOrDefault(); - foreach (var modelSeasonRequest in model.SeasonRequests) + foreach (var modelSeasonRequest in childRequest.SeasonRequests) { if (modelSeasonRequest.SeasonNumber == latest.SeasonNumber) { @@ -86,7 +107,7 @@ namespace Ombi.Core.Engine if (tv.FirstSeason) { var first = showInfo.Season.OrderByDescending(x => x.SeasonNumber).FirstOrDefault(); - foreach (var modelSeasonRequest in model.SeasonRequests) + foreach (var modelSeasonRequest in childRequest.SeasonRequests) { if (modelSeasonRequest.SeasonNumber == first.SeasonNumber) { @@ -124,20 +145,8 @@ namespace Ombi.Core.Engine { var allRequests = await TvRequestService.GetAllAsync(); var results = allRequests.FirstOrDefault(x => x.Id == request.Id); - - results.Approved = request.Approved; - results.Available = request.Available; - results.Denied = request.Denied; - results.DeniedReason = request.DeniedReason; - results.AdminNote = request.AdminNote; - results.ImdbId = request.ImdbId; - results.IssueId = request.IssueId; - results.Issues = request.Issues; - results.OtherMessage = request.OtherMessage; - results.Overview = request.Overview; - results.PosterPath = request.PosterPath; - results.RequestedUsers = request.RequestedUsers?.ToList() ?? new List(); - + results = Mapper.Map(request); + var model = TvRequestService.UpdateRequest(results); return model; } @@ -149,38 +158,25 @@ namespace Ombi.Core.Engine private async Task AddExistingRequest(TvRequestModel newRequest, TvRequestModel existingRequest) { - var episodeDifference = new List(); - if (existingRequest.HasChildRequests) + + var child = newRequest.ChildRequests.FirstOrDefault(); // There will only be 1 + var episodeDiff = new List(); + foreach (var existingChild in existingRequest.ChildRequests) { - // Let's check if this has already been requested as a child! - foreach (var children in existingRequest.ChildRequests) + var difference = GetListDifferences(existingChild.SeasonRequests, child.SeasonRequests).ToList(); + if (difference.Any()) { - var difference = GetListDifferences(children.SeasonRequests, newRequest.SeasonRequests).ToList(); - if (difference.Any()) - { - episodeDifference = difference; - } + episodeDiff = difference; } } - if (episodeDifference.Any()) + if (episodeDiff.Any()) { // This is where there are some episodes that have been requested, but this list contains the 'new' requests - newRequest.SeasonRequests = episodeDifference; + child.SeasonRequests = episodeDiff; } - if (!existingRequest.HasChildRequests) - { - // So this is the first child request, we will want to convert the original request to a child - var originalRequest = Mapper.Map(existingRequest); - existingRequest.ChildRequests.Add(originalRequest); - existingRequest.RequestedUsers.Clear(); - existingRequest.Approved = false; - existingRequest.Available = false; - } - - existingRequest.ChildRequests.Add(newRequest); - + existingRequest.ChildRequests.AddRange(newRequest.ChildRequests); TvRequestService.UpdateRequest(existingRequest); if (ShouldAutoApprove(RequestType.TvShow)) @@ -207,10 +203,10 @@ namespace Ombi.Core.Engine { await TvRequestService.AddRequestAsync(model); - return await AfterRequest(model); + return await AfterRequest(model); } - private async Task AfterRequest(BaseRequestModel model) + private async Task AfterRequest(TvRequestModel model) { if (ShouldSendNotification(model.Type)) { diff --git a/src/Ombi.Core/Engine/TvSearchEngine.cs b/src/Ombi.Core/Engine/TvSearchEngine.cs index 04c66aa99..9dc0a7b99 100644 --- a/src/Ombi.Core/Engine/TvSearchEngine.cs +++ b/src/Ombi.Core/Engine/TvSearchEngine.cs @@ -21,7 +21,7 @@ namespace Ombi.Core.Engine { public TvSearchEngine(IPrincipal identity, IRequestServiceMain service, ITvMazeApi tvMaze, IMapper mapper, ISettingsService plexSettings, - ISettingsService embySettings) + ISettingsService embySettings) : base(identity, service) { TvMazeApi = tvMaze; @@ -41,7 +41,7 @@ namespace Ombi.Core.Engine public async Task> Search(string searchTerm) { var searchResult = await TvMazeApi.Search(searchTerm); - + if (searchResult != null) { return await ProcessResults(searchResult); @@ -73,7 +73,7 @@ namespace Ombi.Core.Engine EpisodeNumber = e.number, }); mapped.SeasonRequests.Add(newSeason); - + } else { @@ -166,19 +166,23 @@ namespace Ombi.Core.Engine // Let's modify the seasonsrequested to reflect what we have requested... foreach (var season in item.SeasonRequests) { - // Find the existing request season - var existingSeason = - existingRequest.SeasonRequests.FirstOrDefault(x => x.SeasonNumber == season.SeasonNumber); - - foreach (var ep in existingSeason.Episodes) + foreach (var existingRequestChildRequest in existingRequest.ChildRequests) { - // Find the episode from what we are searching - var episodeSearching = season.Episodes.FirstOrDefault(x => x.EpisodeNumber == ep.EpisodeNumber); - episodeSearching.Requested = ep.Requested; - episodeSearching.Available = ep.Available; - episodeSearching.Approved = ep.Approved; + + // Find the existing request season + var existingSeason = + existingRequestChildRequest.SeasonRequests.FirstOrDefault(x => x.SeasonNumber == season.SeasonNumber); + + foreach (var ep in existingSeason.Episodes) + { + // Find the episode from what we are searching + var episodeSearching = season.Episodes.FirstOrDefault(x => x.EpisodeNumber == ep.EpisodeNumber); + episodeSearching.Requested = ep.Requested; + episodeSearching.Available = ep.Available; + episodeSearching.Approved = ep.Approved; + } } - + } } //if (sonarrCached.Select(x => x.TvdbId).Contains(tvdbid) || sickRageCache.Contains(tvdbid)) diff --git a/src/Ombi.Core/Models/Requests/Tv/TvRequestModel.cs b/src/Ombi.Core/Models/Requests/Tv/TvRequestModel.cs index 323137305..fb32da24c 100644 --- a/src/Ombi.Core/Models/Requests/Tv/TvRequestModel.cs +++ b/src/Ombi.Core/Models/Requests/Tv/TvRequestModel.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using Ombi.Store.Entities; namespace Ombi.Core.Models.Requests { @@ -6,23 +8,13 @@ namespace Ombi.Core.Models.Requests { public TvRequestModel() { - SeasonRequests = new List(); - ChildRequests = new List(); + ChildRequests = new List(); } - public string ImdbId { get; set; } public string TvDbId { get; set; } - public bool RequestAll { get; set; } - public List SeasonRequests { get; set; } - - /// - /// This is for TV requests, If there is more than 1 request for a show then it should be a child - /// e.g. Request 1 is for Season 1, Request 2 is for season 5. There should be two child items. - /// - public List ChildRequests { get; set; } - - public bool HasChildRequests => ChildRequests.Count > 0; + public List ChildRequests { get; set; } + /// /// For TV Shows with a custom root folder /// @@ -31,4 +23,11 @@ namespace Ombi.Core.Models.Requests /// public int RootFolderSelected { get; set; } } + + public class ChildTvRequest : BaseRequestModel + { + public bool RequestAll { get; set; } + public List SeasonRequests { get; set; } = new List(); + + } } \ No newline at end of file diff --git a/src/Ombi.Helpers/ByteConverterHelper.cs b/src/Ombi.Helpers/ByteConverterHelper.cs index 07db8fd7d..147cd41aa 100644 --- a/src/Ombi.Helpers/ByteConverterHelper.cs +++ b/src/Ombi.Helpers/ByteConverterHelper.cs @@ -6,9 +6,14 @@ namespace Ombi.Helpers { public class ByteConverterHelper { + private static readonly JsonSerializerSettings Settings = new JsonSerializerSettings + { + ReferenceLoopHandling = ReferenceLoopHandling.Serialize, + PreserveReferencesHandling = PreserveReferencesHandling.Objects + }; public static byte[] ReturnBytes(object obj) { - var json = JsonConvert.SerializeObject(obj); + var json = JsonConvert.SerializeObject(obj, Settings); var bytes = Encoding.UTF8.GetBytes(json); return bytes; @@ -17,7 +22,7 @@ namespace Ombi.Helpers public static T ReturnObject(byte[] bytes) { var json = Encoding.UTF8.GetString(bytes); - var model = JsonConvert.DeserializeObject(json); + var model = JsonConvert.DeserializeObject(json, Settings); return model; } public static string ReturnFromBytes(byte[] bytes) diff --git a/src/Ombi/.gitignore b/src/Ombi/.gitignore index 460a72440..3c9623388 100644 --- a/src/Ombi/.gitignore +++ b/src/Ombi/.gitignore @@ -4,6 +4,8 @@ /wwwroot/maps/** /wwwroot/app/**/*.js /wwwroot/app/**/*.js.map +/wwwroot/*.js.map +/wwwroot/*.js # dependencies /node_modules diff --git a/src/Ombi/Controllers/RequestController.cs b/src/Ombi/Controllers/RequestController.cs index eb41fc4eb..a1c36144d 100644 --- a/src/Ombi/Controllers/RequestController.cs +++ b/src/Ombi/Controllers/RequestController.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -55,7 +56,16 @@ namespace Ombi.Controllers [HttpGet("tv/{count:int}/{position:int}")] public async Task> GetTvRequests(int count, int position) { - return await TvRequestEngine.GetTvRequests(count, position); + try + { + + return await TvRequestEngine.GetTvRequests(count, position); + } + catch (Exception e) + { + Console.WriteLine(e); + throw; + } } [HttpPost("tv")] diff --git a/src/Ombi/Ombi.csproj b/src/Ombi/Ombi.csproj index 4c69ef8d4..5f2fa24ea 100644 --- a/src/Ombi/Ombi.csproj +++ b/src/Ombi/Ombi.csproj @@ -12,6 +12,11 @@ + + + + + diff --git a/src/Ombi/wwwroot/app/interfaces/IRequestModel.ts b/src/Ombi/wwwroot/app/interfaces/IRequestModel.ts index 81eb37fbc..22b41e153 100644 --- a/src/Ombi/wwwroot/app/interfaces/IRequestModel.ts +++ b/src/Ombi/wwwroot/app/interfaces/IRequestModel.ts @@ -18,7 +18,7 @@ issueId: number, denied: boolean, deniedReason: string, - released:boolean + released: boolean } export interface IMovieRequestModel extends IMediaBase { @@ -28,16 +28,17 @@ export interface IMovieRequestModel extends IMediaBase { export interface ITvRequestModel extends IMediaBase { imdbId: string, tvDbId: string, - requestAll: boolean, - seasonRequests: ISeasonRequests[], - childRequests: ITvRequestModel[], - hasChildRequests: boolean, + childRequests: IChildTvRequest[] rootFolderSelected: number, - firstAired:string, + firstAired: string, +} + +export interface IChildTvRequest extends IMediaBase { + requestAll: boolean, + seasonRequests: ISeasonRequests[], } -export interface ISeasonRequests -{ +export interface ISeasonRequests { seasonNumber: number, episodes: IEpisodesRequested[], } @@ -49,7 +50,7 @@ export interface IEpisodesRequested { url: string, requested: boolean, status: string, - available:boolean + available: boolean } @@ -60,5 +61,5 @@ export enum RequestType { export interface IRequestsPageScroll { count: number, - position:number + position: number } \ No newline at end of file diff --git a/src/Ombi/wwwroot/app/requests/tvrequests.component.html b/src/Ombi/wwwroot/app/requests/tvrequests.component.html index becd164bb..2a35f045e 100644 --- a/src/Ombi/wwwroot/app/requests/tvrequests.component.html +++ b/src/Ombi/wwwroot/app/requests/tvrequests.component.html @@ -77,29 +77,29 @@ -
- - -
- -
-
-
Requested By: {{user}} -
-
Seasons Requested: {{s}} -
-
- Request status: - Available - Processing Request - Request Denied - - Pending Approval -
+ +
+ +
+
+
+ Requested By: {{user}} +
+
+ Seasons Requested: {{s.seasonNumber}}
+
+ Request status: + Available + Processing Request + Request Denied + + Pending Approval +
+
@@ -114,6 +114,7 @@ Toggle Dropdown +