mirror of
				https://github.com/zoriya/Kyoo.git
				synced 2025-11-03 19:17:16 -05:00 
			
		
		
		
	Prevent 500 when specifing invalid show id on episode create (#367)
This commit is contained in:
		
						commit
						5e054e12f7
					
				@ -431,20 +431,13 @@ public static class DapperHelper
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class SqlVariableContext
 | 
					public class SqlVariableContext(IHttpContextAccessor accessor)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	private readonly IHttpContextAccessor _accessor;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public SqlVariableContext(IHttpContextAccessor accessor)
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		_accessor = accessor;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	public object? ReadVar(string var)
 | 
						public object? ReadVar(string var)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		return var switch
 | 
							return var switch
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			"current_user" => _accessor.HttpContext?.User.GetId(),
 | 
								"current_user" => accessor.HttpContext?.User.GetId(),
 | 
				
			||||||
			_ => throw new ArgumentException($"Invalid sql variable name: {var}")
 | 
								_ => throw new ArgumentException($"Invalid sql variable name: {var}")
 | 
				
			||||||
		};
 | 
							};
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -32,15 +32,12 @@ namespace Kyoo.Core.Controllers;
 | 
				
			|||||||
/// <summary>
 | 
					/// <summary>
 | 
				
			||||||
/// A local repository to handle episodes.
 | 
					/// A local repository to handle episodes.
 | 
				
			||||||
/// </summary>
 | 
					/// </summary>
 | 
				
			||||||
public class EpisodeRepository : LocalRepository<Episode>
 | 
					public class EpisodeRepository(
 | 
				
			||||||
 | 
						DatabaseContext database,
 | 
				
			||||||
 | 
						IRepository<Show> shows,
 | 
				
			||||||
 | 
						IThumbnailsManager thumbs
 | 
				
			||||||
 | 
					) : LocalRepository<Episode>(database, thumbs)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	/// <summary>
 | 
					 | 
				
			||||||
	/// The database handle
 | 
					 | 
				
			||||||
	/// </summary>
 | 
					 | 
				
			||||||
	private readonly DatabaseContext _database;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	private readonly IRepository<Show> _shows;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	static EpisodeRepository()
 | 
						static EpisodeRepository()
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		// Edit episode slugs when the show's slug changes.
 | 
							// Edit episode slugs when the show's slug changes.
 | 
				
			||||||
@ -61,30 +58,13 @@ public class EpisodeRepository : LocalRepository<Episode>
 | 
				
			|||||||
		};
 | 
							};
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/// <summary>
 | 
					 | 
				
			||||||
	/// Create a new <see cref="EpisodeRepository"/>.
 | 
					 | 
				
			||||||
	/// </summary>
 | 
					 | 
				
			||||||
	/// <param name="database">The database handle to use.</param>
 | 
					 | 
				
			||||||
	/// <param name="shows">A show repository</param>
 | 
					 | 
				
			||||||
	/// <param name="thumbs">The thumbnail manager used to store images.</param>
 | 
					 | 
				
			||||||
	public EpisodeRepository(
 | 
					 | 
				
			||||||
		DatabaseContext database,
 | 
					 | 
				
			||||||
		IRepository<Show> shows,
 | 
					 | 
				
			||||||
		IThumbnailsManager thumbs
 | 
					 | 
				
			||||||
	)
 | 
					 | 
				
			||||||
		: base(database, thumbs)
 | 
					 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		_database = database;
 | 
					 | 
				
			||||||
		_shows = shows;
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/// <inheritdoc />
 | 
						/// <inheritdoc />
 | 
				
			||||||
	public override async Task<ICollection<Episode>> Search(
 | 
						public override async Task<ICollection<Episode>> Search(
 | 
				
			||||||
		string query,
 | 
							string query,
 | 
				
			||||||
		Include<Episode>? include = default
 | 
							Include<Episode>? include = default
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		return await AddIncludes(_database.Episodes, include)
 | 
							return await AddIncludes(database.Episodes, include)
 | 
				
			||||||
			.Where(x => EF.Functions.ILike(x.Name!, $"%{query}%"))
 | 
								.Where(x => EF.Functions.ILike(x.Name!, $"%{query}%"))
 | 
				
			||||||
			.Take(20)
 | 
								.Take(20)
 | 
				
			||||||
			.ToListAsync();
 | 
								.ToListAsync();
 | 
				
			||||||
@ -93,12 +73,12 @@ public class EpisodeRepository : LocalRepository<Episode>
 | 
				
			|||||||
	protected override Task<Episode?> GetDuplicated(Episode item)
 | 
						protected override Task<Episode?> GetDuplicated(Episode item)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		if (item is { SeasonNumber: not null, EpisodeNumber: not null })
 | 
							if (item is { SeasonNumber: not null, EpisodeNumber: not null })
 | 
				
			||||||
			return _database.Episodes.FirstOrDefaultAsync(x =>
 | 
								return database.Episodes.FirstOrDefaultAsync(x =>
 | 
				
			||||||
				x.ShowId == item.ShowId
 | 
									x.ShowId == item.ShowId
 | 
				
			||||||
				&& x.SeasonNumber == item.SeasonNumber
 | 
									&& x.SeasonNumber == item.SeasonNumber
 | 
				
			||||||
				&& x.EpisodeNumber == item.EpisodeNumber
 | 
									&& x.EpisodeNumber == item.EpisodeNumber
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
		return _database.Episodes.FirstOrDefaultAsync(x =>
 | 
							return database.Episodes.FirstOrDefaultAsync(x =>
 | 
				
			||||||
			x.ShowId == item.ShowId && x.AbsoluteNumber == item.AbsoluteNumber
 | 
								x.ShowId == item.ShowId && x.AbsoluteNumber == item.AbsoluteNumber
 | 
				
			||||||
		);
 | 
							);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -106,11 +86,10 @@ public class EpisodeRepository : LocalRepository<Episode>
 | 
				
			|||||||
	/// <inheritdoc />
 | 
						/// <inheritdoc />
 | 
				
			||||||
	public override async Task<Episode> Create(Episode obj)
 | 
						public override async Task<Episode> Create(Episode obj)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		obj.ShowSlug =
 | 
							obj.ShowSlug = obj.Show?.Slug ?? (await shows.Get(obj.ShowId)).Slug;
 | 
				
			||||||
			obj.Show?.Slug ?? (await _database.Shows.FirstAsync(x => x.Id == obj.ShowId)).Slug;
 | 
					 | 
				
			||||||
		await base.Create(obj);
 | 
							await base.Create(obj);
 | 
				
			||||||
		_database.Entry(obj).State = EntityState.Added;
 | 
							database.Entry(obj).State = EntityState.Added;
 | 
				
			||||||
		await _database.SaveChangesAsync(() => GetDuplicated(obj));
 | 
							await database.SaveChangesAsync(() => GetDuplicated(obj));
 | 
				
			||||||
		await IRepository<Episode>.OnResourceCreated(obj);
 | 
							await IRepository<Episode>.OnResourceCreated(obj);
 | 
				
			||||||
		return obj;
 | 
							return obj;
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@ -132,7 +111,7 @@ public class EpisodeRepository : LocalRepository<Episode>
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
		if (resource.SeasonId == null && resource.SeasonNumber != null)
 | 
							if (resource.SeasonId == null && resource.SeasonNumber != null)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			resource.Season = await _database.Seasons.FirstOrDefaultAsync(x =>
 | 
								resource.Season = await database.Seasons.FirstOrDefaultAsync(x =>
 | 
				
			||||||
				x.ShowId == resource.ShowId && x.SeasonNumber == resource.SeasonNumber
 | 
									x.ShowId == resource.ShowId && x.SeasonNumber == resource.SeasonNumber
 | 
				
			||||||
			);
 | 
								);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
@ -141,14 +120,14 @@ public class EpisodeRepository : LocalRepository<Episode>
 | 
				
			|||||||
	/// <inheritdoc />
 | 
						/// <inheritdoc />
 | 
				
			||||||
	public override async Task Delete(Episode obj)
 | 
						public override async Task Delete(Episode obj)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		int epCount = await _database
 | 
							int epCount = await database
 | 
				
			||||||
			.Episodes.Where(x => x.ShowId == obj.ShowId)
 | 
								.Episodes.Where(x => x.ShowId == obj.ShowId)
 | 
				
			||||||
			.Take(2)
 | 
								.Take(2)
 | 
				
			||||||
			.CountAsync();
 | 
								.CountAsync();
 | 
				
			||||||
		_database.Entry(obj).State = EntityState.Deleted;
 | 
							database.Entry(obj).State = EntityState.Deleted;
 | 
				
			||||||
		await _database.SaveChangesAsync();
 | 
							await database.SaveChangesAsync();
 | 
				
			||||||
		await base.Delete(obj);
 | 
							await base.Delete(obj);
 | 
				
			||||||
		if (epCount == 1)
 | 
							if (epCount == 1)
 | 
				
			||||||
			await _shows.Delete(obj.ShowId);
 | 
								await shows.Delete(obj.ShowId);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -489,7 +489,7 @@ public class WatchStatusRepository(
 | 
				
			|||||||
		int? percent
 | 
							int? percent
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		Episode episode = await database.Episodes.FirstAsync(x => x.Id == episodeId);
 | 
							Episode episode = await episodes.Get(episodeId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (percent == null && watchedTime != null && episode.Runtime > 0)
 | 
							if (percent == null && watchedTime != null && episode.Runtime > 0)
 | 
				
			||||||
			percent = (int)Math.Round(watchedTime.Value / (episode.Runtime.Value * 60f) * 100f);
 | 
								percent = (int)Math.Round(watchedTime.Value / (episode.Runtime.Value * 60f) * 100f);
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user