diff --git a/Kyoo.Common/Controllers/ILibraryManager.cs b/Kyoo.Common/Controllers/ILibraryManager.cs index 84ad41e5..f97718db 100644 --- a/Kyoo.Common/Controllers/ILibraryManager.cs +++ b/Kyoo.Common/Controllers/ILibraryManager.cs @@ -34,6 +34,7 @@ namespace Kyoo.Controllers // Other get helpers Show GetShowByPath(string path); IEnumerable GetLibrariesPath(); + IEnumerable GetEpisodes(string showSlug, long seasonNumber); //Register values void Register(object obj); diff --git a/Kyoo.Common/Models/WatchItem.cs b/Kyoo.Common/Models/WatchItem.cs index d93c069c..3278f313 100644 --- a/Kyoo.Common/Models/WatchItem.cs +++ b/Kyoo.Common/Models/WatchItem.cs @@ -1,6 +1,7 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; +using System.Linq; namespace Kyoo.Models { @@ -16,7 +17,7 @@ namespace Kyoo.Models public string Link; public DateTime? ReleaseDate; [JsonIgnore] public string Path; - public string PreviousEpisode; + public Episode PreviousEpisode; public Episode NextEpisode; public bool IsMovie; @@ -65,15 +66,34 @@ namespace Kyoo.Models Subtitles = subtitles; } - public WatchItem(Episode episode) - : this(episode.ID, - episode.Show.Title, - episode.Show.Slug, - episode.SeasonNumber, - episode.EpisodeNumber, - episode.Title, - episode.ReleaseDate, - episode.Path) - { } + public WatchItem(Episode episode) + : this(episode.ID, + episode.Show.Title, + episode.Show.Slug, + episode.SeasonNumber, + episode.EpisodeNumber, + episode.Title, + episode.ReleaseDate, + episode.Path) + { + if (EpisodeNumber > 1) + PreviousEpisode = episode.Season.Episodes.FirstOrDefault(x => x.EpisodeNumber == EpisodeNumber - 1); + else if (SeasonNumber > 1) + { + Season previousSeason = episode.Show.Seasons + .FirstOrDefault(x => x.SeasonNumber == SeasonNumber - 1); + PreviousEpisode = previousSeason?.Episodes + .FirstOrDefault(x => x.EpisodeNumber == previousSeason.Episodes.Count()); + } + + if (EpisodeNumber >= episode.Season.Episodes.Count()) + { + NextEpisode = episode.Show.Seasons + .FirstOrDefault(x => x.SeasonNumber == SeasonNumber - 1)?.Episodes + .FirstOrDefault(x => x.EpisodeNumber == 1); + } + else + NextEpisode = episode.Season.Episodes.FirstOrDefault(x => x.EpisodeNumber == EpisodeNumber + 1); + } } } diff --git a/Kyoo/Controllers/LibraryManager.cs b/Kyoo/Controllers/LibraryManager.cs index d34ee0f6..1ad9d559 100644 --- a/Kyoo/Controllers/LibraryManager.cs +++ b/Kyoo/Controllers/LibraryManager.cs @@ -100,6 +100,11 @@ namespace Kyoo.Controllers { return _database.Shows.FirstOrDefault(show => show.Path == path); } + + public IEnumerable GetEpisodes(string showSlug, long seasonNumber) + { + return _database.Episodes.Where(x => x.Show.Slug == showSlug && x.SeasonNumber == seasonNumber); + } #endregion #region Search @@ -170,7 +175,6 @@ namespace Kyoo.Controllers #endregion #region Edit - public void Edit(Library edited, bool resetOld) { Edit(() => @@ -209,7 +213,6 @@ namespace Kyoo.Controllers Validate(old); }); } - public void Edit(Show edited, bool resetOld) { Edit(() => @@ -499,141 +502,6 @@ namespace Kyoo.Controllers } #endregion - public long EditShow(Show edited) - { - if (edited == null) - throw new ArgumentNullException(nameof(edited)); - - _database.ChangeTracker.LazyLoadingEnabled = false; - _database.ChangeTracker.AutoDetectChangesEnabled = false; - - try - { - var query = _database.Shows.Include(x => x.GenreLinks) - .Include(x => x.People) - .Include(x => x.ExternalIDs); - Show show = _database.Entry(edited).IsKeySet - ? query.FirstOrDefault(x => x.ID == edited.ID) - : query.FirstOrDefault(x => x.Slug == edited.Slug); - - if (show == null) - throw new ItemNotFound($"No show could be found with the id {edited.ID} or the slug {edited.Slug}"); - - Utility.Complete(show, edited); - - _database.ChangeTracker.DetectChanges(); - _database.SaveChanges(); - } - finally - { - _database.ChangeTracker.LazyLoadingEnabled = true; - _database.ChangeTracker.AutoDetectChangesEnabled = true; - } - - return edited.ID; - } - - public long RegisterMovie(Episode movie) - { - if (movie == null) - return 0; - if (_database.Entry(movie).State == EntityState.Detached) - _database.Episodes.Add(movie); - _database.SaveChanges(); - return movie.ID; - } - - public long RegisterSeason(Season season) - { - if (season == null) - return 0; - if (_database.Entry(season).State == EntityState.Detached) - _database.Seasons.Add(season); - _database.SaveChanges(); - return season.ID; - } - - public long EditSeason(Season edited) - { - if (edited == null) - throw new ArgumentNullException(nameof(edited)); - - _database.ChangeTracker.LazyLoadingEnabled = false; - _database.ChangeTracker.AutoDetectChangesEnabled = false; - - try - { - var query = _database.Seasons - .Include(x => x.ExternalIDs) - .Include(x => x.Episodes); - Season season = _database.Entry(edited).IsKeySet - ? query.FirstOrDefault(x => x.ID == edited.ID) - : query.FirstOrDefault(x => x.Slug == edited.Slug); - - if (season == null) - throw new ItemNotFound($"No season could be found with the id {edited.ID} or the slug {edited.Slug}"); - - Utility.Complete(season, edited); - - - - _database.ChangeTracker.DetectChanges(); - _database.SaveChanges(); - } - finally - { - _database.ChangeTracker.LazyLoadingEnabled = true; - _database.ChangeTracker.AutoDetectChangesEnabled = true; - } - - return edited.ID; - } - - public long RegisterEpisode(Episode episode) - { - if (episode == null) - return 0; - if (!_database.Entry(episode).IsKeySet) - _database.Add(episode); - _database.SaveChanges(); - return episode.ID; - } - - public long EditEpisode(Episode edited) - { - if (edited == null) - throw new ArgumentNullException(nameof(edited)); - - _database.ChangeTracker.LazyLoadingEnabled = false; - _database.ChangeTracker.AutoDetectChangesEnabled = false; - - try - { - var query = _database.Episodes - .Include(x => x.Tracks) - .Include(x => x.Season) - .Include(x => x.ExternalIDs); - Episode episode = query.FirstOrDefault(x => x.ID == edited.ID); - - if (episode == null) - throw new ItemNotFound($"No episode could be found with the id {edited.ID}"); - - Utility.Complete(episode, edited); - - - - _database.ChangeTracker.DetectChanges(); - _database.SaveChanges(); - } - finally - { - _database.ChangeTracker.LazyLoadingEnabled = true; - _database.ChangeTracker.AutoDetectChangesEnabled = true; - } - - return edited.ID; - } - #region Remove public void RemoveShow(Show show) { diff --git a/Kyoo/Tasks/Crawler.cs b/Kyoo/Tasks/Crawler.cs index 1f8a99f6..73cb25b2 100644 --- a/Kyoo/Tasks/Crawler.cs +++ b/Kyoo/Tasks/Crawler.cs @@ -40,6 +40,7 @@ namespace Kyoo.Controllers public async Task Run(IServiceProvider serviceProvider, CancellationToken cancellationToken, string argument = null) { + // TODO Should use more scopes of the library manager (one per episodes to register). using IServiceScope serviceScope = serviceProvider.CreateScope(); _libraryManager = serviceScope.ServiceProvider.GetService(); _thumbnailsManager = serviceScope.ServiceProvider.GetService(); diff --git a/Kyoo/Views/API/LibrariesAPI.cs b/Kyoo/Views/API/LibrariesAPI.cs index 544252a0..ec32921d 100644 --- a/Kyoo/Views/API/LibrariesAPI.cs +++ b/Kyoo/Views/API/LibrariesAPI.cs @@ -42,7 +42,8 @@ namespace Kyoo.Api return BadRequest(new {error = "The library should have a least one path."}); if (_libraryManager.GetLibrary(library.Slug) != null) return BadRequest(new {error = "Duplicated library slug"}); - _libraryManager.RegisterLibrary(library); + _libraryManager.Register(library); + _libraryManager.SaveChanges(); _taskManager.StartTask("scan", library.Slug); return Ok(); } diff --git a/Kyoo/Views/API/ShowsAPI.cs b/Kyoo/Views/API/ShowsAPI.cs index 71385fd3..652773eb 100644 --- a/Kyoo/Views/API/ShowsAPI.cs +++ b/Kyoo/Views/API/ShowsAPI.cs @@ -66,7 +66,7 @@ namespace Kyoo.Api show.ID = old.ID; show.Slug = slug; show.Path = old.Path; - _libraryManager.EditShow(show); + _libraryManager.Edit(show, false); return Ok(); } @@ -79,7 +79,7 @@ namespace Kyoo.Api Show show = _database.Shows.Include(x => x.ExternalIDs).FirstOrDefault(x => x.Slug == slug); if (show == null) return NotFound(); - show.ExternalIDs = _libraryManager.ValidateExternalIDs(externalIDs); + show.ExternalIDs = _libraryManager.Validate(externalIDs); _database.SaveChanges(); _taskManager.StartTask("re-scan", $"show/{slug}"); return Ok(); diff --git a/Kyoo/Views/API/VideoAPI.cs b/Kyoo/Views/API/VideoAPI.cs index c1fd57b0..57a77833 100644 --- a/Kyoo/Views/API/VideoAPI.cs +++ b/Kyoo/Views/API/VideoAPI.cs @@ -87,7 +87,7 @@ namespace Kyoo.Api [Authorize(Policy="Play")] public IActionResult Index(string movieSlug) { - WatchItem episode = _libraryManager.GetMovieWatchItem(movieSlug); + Episode episode = _libraryManager.GetMovieEpisode(movieSlug); if (episode != null && System.IO.File.Exists(episode.Path)) return PhysicalFile(episode.Path, "video/webm", true); diff --git a/Kyoo/Views/API/WatchAPI.cs b/Kyoo/Views/API/WatchAPI.cs index 6c04d74c..d8cc84c2 100644 --- a/Kyoo/Views/API/WatchAPI.cs +++ b/Kyoo/Views/API/WatchAPI.cs @@ -20,23 +20,23 @@ namespace Kyoo.Api [Authorize(Policy="Read")] public ActionResult Index(string showSlug, long seasonNumber, long episodeNumber) { - WatchItem item = _libraryManager.GetWatchItem(showSlug, seasonNumber, episodeNumber); + Episode item = _libraryManager.GetEpisode(showSlug, seasonNumber, episodeNumber); if(item == null) return NotFound(); - return item; + return new WatchItem(item);; } [HttpGet("{movieSlug}")] [Authorize(Policy="Read")] public ActionResult Index(string movieSlug) { - WatchItem item = _libraryManager.GetMovieWatchItem(movieSlug); + Episode item = _libraryManager.GetMovieEpisode(movieSlug); if(item == null) return NotFound(); - return item; + return new WatchItem(item); } } }