mirror of
https://github.com/zoriya/Kyoo.git
synced 2025-07-09 03:04:20 -04:00
Implementing the track repository
This commit is contained in:
parent
5272c75232
commit
3c90003914
@ -28,8 +28,11 @@ namespace Kyoo.Controllers
|
||||
{
|
||||
Task<Episode> Get(string showSlug, long seasonNumber, long episodeNumber);
|
||||
}
|
||||
|
||||
public interface ITrackRepository : IRepository<Track> {}
|
||||
|
||||
public interface ITrackRepository : IRepository<Track>
|
||||
{
|
||||
Task<Track> Get(long episodeID, string languageTag, bool isForced);
|
||||
}
|
||||
public interface ILibraryRepository : IRepository<Library> {}
|
||||
public interface ICollectionRepository : IRepository<Collection> {}
|
||||
public interface IGenreRepository : IRepository<Genre> {}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
|
88
Kyoo/Controllers/Repositories/TrackRepository.cs
Normal file
88
Kyoo/Controllers/Repositories/TrackRepository.cs
Normal file
@ -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<Track> Get(long id)
|
||||
{
|
||||
return await _database.Tracks.FirstOrDefaultAsync(x => x.ID == id);
|
||||
}
|
||||
|
||||
public Task<Track> Get(string slug)
|
||||
{
|
||||
throw new InvalidOperationException("Tracks do not support the get by slug method.");
|
||||
}
|
||||
|
||||
public Task<Track> Get(long episodeID, string languageTag, bool isForced)
|
||||
{
|
||||
return _database.Tracks.FirstOrDefaultAsync(x => x.EpisodeID == episodeID
|
||||
&& x.Language == languageTag
|
||||
&& x.IsForced == isForced);
|
||||
}
|
||||
|
||||
public Task<IEnumerable<Track>> Search(string query)
|
||||
{
|
||||
throw new InvalidOperationException("Tracks do not support the search method.");
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Track>> GetAll()
|
||||
{
|
||||
return await _database.Tracks.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<long> 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<long> 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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user