Skip to content

Commit

Permalink
Prevent 500 when specifing invalid show id on episode create
Browse files Browse the repository at this point in the history
  • Loading branch information
zoriya committed Mar 31, 2024
1 parent 0cf7b13 commit e8896b7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 47 deletions.
11 changes: 2 additions & 9 deletions back/src/Kyoo.Core/Controllers/Repositories/DapperHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,13 @@ public static async Task<int> Count<T>(
}
}

public class SqlVariableContext
public class SqlVariableContext(IHttpContextAccessor accessor)
{
private readonly IHttpContextAccessor _accessor;

public SqlVariableContext(IHttpContextAccessor accessor)
{
_accessor = accessor;
}

public object? ReadVar(string var)
{
return var switch
{
"current_user" => _accessor.HttpContext?.User.GetId(),
"current_user" => accessor.HttpContext?.User.GetId(),
_ => throw new ArgumentException($"Invalid sql variable name: {var}")
};
}
Expand Down
53 changes: 16 additions & 37 deletions back/src/Kyoo.Core/Controllers/Repositories/EpisodeRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,12 @@ namespace Kyoo.Core.Controllers;
/// <summary>
/// A local repository to handle episodes.
/// </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()
{
// Edit episode slugs when the show's slug changes.
Expand All @@ -61,30 +58,13 @@ static EpisodeRepository()
};
}

/// <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 />
public override async Task<ICollection<Episode>> Search(
string query,
Include<Episode>? include = default
)
{
return await AddIncludes(_database.Episodes, include)
return await AddIncludes(database.Episodes, include)
.Where(x => EF.Functions.ILike(x.Name!, $"%{query}%"))
.Take(20)
.ToListAsync();
Expand All @@ -93,24 +73,23 @@ public override async Task<ICollection<Episode>> Search(
protected override Task<Episode?> GetDuplicated(Episode item)
{
if (item is { SeasonNumber: not null, EpisodeNumber: not null })
return _database.Episodes.FirstOrDefaultAsync(x =>
return database.Episodes.FirstOrDefaultAsync(x =>
x.ShowId == item.ShowId
&& x.SeasonNumber == item.SeasonNumber
&& x.EpisodeNumber == item.EpisodeNumber
);
return _database.Episodes.FirstOrDefaultAsync(x =>
return database.Episodes.FirstOrDefaultAsync(x =>
x.ShowId == item.ShowId && x.AbsoluteNumber == item.AbsoluteNumber
);
}

/// <inheritdoc />
public override async Task<Episode> Create(Episode obj)
{
obj.ShowSlug =
obj.Show?.Slug ?? (await _database.Shows.FirstAsync(x => x.Id == obj.ShowId)).Slug;
obj.ShowSlug = obj.Show?.Slug ?? (await shows.Get(obj.ShowId)).Slug;
await base.Create(obj);
_database.Entry(obj).State = EntityState.Added;
await _database.SaveChangesAsync(() => GetDuplicated(obj));
database.Entry(obj).State = EntityState.Added;
await database.SaveChangesAsync(() => GetDuplicated(obj));
await IRepository<Episode>.OnResourceCreated(obj);
return obj;
}
Expand All @@ -132,7 +111,7 @@ protected override async Task Validate(Episode resource)
}
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
);
}
Expand All @@ -141,14 +120,14 @@ protected override async Task Validate(Episode resource)
/// <inheritdoc />
public override async Task Delete(Episode obj)
{
int epCount = await _database
int epCount = await database
.Episodes.Where(x => x.ShowId == obj.ShowId)
.Take(2)
.CountAsync();
_database.Entry(obj).State = EntityState.Deleted;
await _database.SaveChangesAsync();
database.Entry(obj).State = EntityState.Deleted;
await database.SaveChangesAsync();
await base.Delete(obj);
if (epCount == 1)
await _shows.Delete(obj.ShowId);
await shows.Delete(obj.ShowId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ await IWatchStatusRepository.OnShowStatusChanged(
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)
percent = (int)Math.Round(watchedTime.Value / (episode.Runtime.Value * 60f) * 100f);
Expand Down

0 comments on commit e8896b7

Please sign in to comment.