diff --git a/Kyoo.Common/Controllers/IRepository.cs b/Kyoo.Common/Controllers/IRepository.cs index baf7e1cc..07c8043d 100644 --- a/Kyoo.Common/Controllers/IRepository.cs +++ b/Kyoo.Common/Controllers/IRepository.cs @@ -28,8 +28,11 @@ namespace Kyoo.Controllers { Task Get(string showSlug, long seasonNumber, long episodeNumber); } - - public interface ITrackRepository : IRepository {} + + public interface ITrackRepository : IRepository + { + Task Get(long episodeID, string languageTag, bool isForced); + } public interface ILibraryRepository : IRepository {} public interface ICollectionRepository : IRepository {} public interface IGenreRepository : IRepository {} diff --git a/Kyoo/Controllers/Repositories/EpisodeRepository.cs b/Kyoo/Controllers/Repositories/EpisodeRepository.cs index 3f934f35..bc32b92c 100644 --- a/Kyoo/Controllers/Repositories/EpisodeRepository.cs +++ b/Kyoo/Controllers/Repositories/EpisodeRepository.cs @@ -63,8 +63,7 @@ namespace Kyoo.Controllers { if (obj == null) throw new ArgumentNullException(nameof(obj)); - - // TODO initialize ShowID & SeaosnID here. (same for the season repository). OR null check the ID and throw on invalid. + obj.Show = null; obj.Season = null; await Validate(obj); @@ -105,6 +104,9 @@ namespace Kyoo.Controllers private async Task Validate(Episode obj) { + if (obj.ShowID <= 0) + throw new InvalidOperationException($"Can't store an episode not related to any show (showID: {obj.ShowID})."); + obj.ExternalIDs = (await Task.WhenAll(obj.ExternalIDs.Select(async x => { x.ProviderID = await _providers.CreateIfNotExists(x.Provider); diff --git a/Kyoo/Controllers/Repositories/SeasonRepository.cs b/Kyoo/Controllers/Repositories/SeasonRepository.cs index 056d64a6..86bd5d6f 100644 --- a/Kyoo/Controllers/Repositories/SeasonRepository.cs +++ b/Kyoo/Controllers/Repositories/SeasonRepository.cs @@ -100,6 +100,9 @@ namespace Kyoo.Controllers private async Task Validate(Season obj) { + if (obj.ShowID <= 0) + throw new InvalidOperationException($"Can't store a season not related to any show (showID: {obj.ShowID})."); + obj.ExternalIDs = (await Task.WhenAll(obj.ExternalIDs.Select(async x => { x.ProviderID = await _providers.CreateIfNotExists(x.Provider); diff --git a/Kyoo/Controllers/Repositories/TrackRepository.cs b/Kyoo/Controllers/Repositories/TrackRepository.cs new file mode 100644 index 00000000..a5d58aa0 --- /dev/null +++ b/Kyoo/Controllers/Repositories/TrackRepository.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using Kyoo.Models; +using Kyoo.Models.Exceptions; +using Microsoft.EntityFrameworkCore; + +namespace Kyoo.Controllers +{ + public class TrackRepository : ITrackRepository + { + private readonly DatabaseContext _database; + + + public TrackRepository(DatabaseContext database) + { + _database = database; + } + + public async Task Get(long id) + { + return await _database.Tracks.FirstOrDefaultAsync(x => x.ID == id); + } + + public Task Get(string slug) + { + throw new InvalidOperationException("Tracks do not support the get by slug method."); + } + + public Task Get(long episodeID, string languageTag, bool isForced) + { + return _database.Tracks.FirstOrDefaultAsync(x => x.EpisodeID == episodeID + && x.Language == languageTag + && x.IsForced == isForced); + } + + public Task> Search(string query) + { + throw new InvalidOperationException("Tracks do not support the search method."); + } + + public async Task> GetAll() + { + return await _database.Tracks.ToListAsync(); + } + + public async Task Create(Track obj) + { + if (obj == null) + throw new ArgumentNullException(nameof(obj)); + + if (obj.EpisodeID <= 0) + throw new InvalidOperationException($"Can't store a track not related to any episode (episodeID: {obj.EpisodeID})."); + + obj.Episode = null; + await _database.Tracks.AddAsync(obj); + await _database.SaveChangesAsync(); + return obj.ID; + } + + public Task CreateIfNotExists(Track obj) + { + return Create(obj); + } + + public async Task Edit(Track edited, bool resetOld) + { + if (edited == null) + throw new ArgumentNullException(nameof(edited)); + + Track old = await Get(edited.ID); + + if (old == null) + throw new ItemNotFound($"No track found with the ID {edited.ID}."); + + if (resetOld) + Utility.Nullify(old); + Utility.Merge(old, edited); + await _database.SaveChangesAsync(); + } + + public async Task Delete(Track obj) + { + _database.Tracks.Remove(obj); + await _database.SaveChangesAsync(); + } + } +} \ No newline at end of file