mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-05-24 02:02:36 -04:00
Reworking WatchEpisode creation
This commit is contained in:
parent
90d0180650
commit
c8e3e92491
@ -34,6 +34,7 @@ namespace Kyoo.Controllers
|
||||
// Other get helpers
|
||||
Show GetShowByPath(string path);
|
||||
IEnumerable<string> GetLibrariesPath();
|
||||
IEnumerable<Episode> GetEpisodes(string showSlug, long seasonNumber);
|
||||
|
||||
//Register values
|
||||
void Register(object obj);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -100,6 +100,11 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
return _database.Shows.FirstOrDefault(show => show.Path == path);
|
||||
}
|
||||
|
||||
public IEnumerable<Episode> 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)
|
||||
{
|
||||
|
@ -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<ILibraryManager>();
|
||||
_thumbnailsManager = serviceScope.ServiceProvider.GetService<IThumbnailsManager>();
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
|
@ -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);
|
||||
|
@ -20,23 +20,23 @@ namespace Kyoo.Api
|
||||
[Authorize(Policy="Read")]
|
||||
public ActionResult<WatchItem> 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<WatchItem> Index(string movieSlug)
|
||||
{
|
||||
WatchItem item = _libraryManager.GetMovieWatchItem(movieSlug);
|
||||
Episode item = _libraryManager.GetMovieEpisode(movieSlug);
|
||||
|
||||
if(item == null)
|
||||
return NotFound();
|
||||
return item;
|
||||
return new WatchItem(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user