Using ids of entities instead of the whole entity in create responeses

This commit is contained in:
Zoe Roux 2020-06-02 23:34:37 +02:00
parent bf66f0ca9c
commit b59f208781
5 changed files with 365 additions and 66 deletions

View File

@ -1,19 +1,18 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using Kyoo.Models; using Kyoo.Models;
namespace Kyoo.Controllers namespace Kyoo.Controllers
{ {
// ReSharper disable once PossibleInterfaceMemberAmbiguity
public interface ILibraryManager public interface ILibraryManager
{ {
// Get by slug // Get by slug
Library GetLibrary(string librarySlug); Library GetLibrary(string slug);
Collection GetCollection(string slug); Collection GetCollection(string slug);
Show GetShow(string slug); Show GetShow(string slug);
Season GetSeason(string showSlug, long seasonNumber); Season GetSeason(string showSlug, long seasonNumber);
Episode GetEpisode(string showSlug, long seasonNumber, long episodeNumber); Episode GetEpisode(string showSlug, long seasonNumber, long episodeNumber);
Episode GetMovieEpisode(string movieSlug); Episode GetMovieEpisode(string movieSlug);
Track GetTrack(string slug);
Genre GetGenre(string slug); Genre GetGenre(string slug);
Studio GetStudio(string slug); Studio GetStudio(string slug);
People GetPeople(string slug); People GetPeople(string slug);
@ -24,6 +23,7 @@ namespace Kyoo.Controllers
IEnumerable<Show> GetShows(); IEnumerable<Show> GetShows();
IEnumerable<Season> GetSeasons(); IEnumerable<Season> GetSeasons();
IEnumerable<Episode> GetEpisodes(); IEnumerable<Episode> GetEpisodes();
IEnumerable<Track> GetTracks();
IEnumerable<Studio> GetStudios(); IEnumerable<Studio> GetStudios();
IEnumerable<People> GetPeoples(); IEnumerable<People> GetPeoples();
IEnumerable<Genre> GetGenres(); IEnumerable<Genre> GetGenres();
@ -38,17 +38,13 @@ namespace Kyoo.Controllers
IEnumerable<Studio> SearchStudios(string searchQuery); IEnumerable<Studio> SearchStudios(string searchQuery);
IEnumerable<People> SearchPeople(string searchQuery); IEnumerable<People> SearchPeople(string searchQuery);
// Other get helpers
Show GetShowByPath(string path);
IEnumerable<string> GetLibrariesPath();
IEnumerable<Episode> GetEpisodes(string showSlug, long seasonNumber);
//Register values //Register values
void RegisterLibrary(Library library); void RegisterLibrary(Library library);
void RegisterCollection(Collection collection); void RegisterCollection(Collection collection);
void RegisterShow(Show show); void RegisterShow(Show show);
void RegisterSeason(Season season); void RegisterSeason(Season season);
void RegisterEpisode(Episode episode); void RegisterEpisode(Episode episode);
void RegisterTrack(Track track);
void RegisterGenre(Genre genre); void RegisterGenre(Genre genre);
void RegisterStudio(Studio studio); void RegisterStudio(Studio studio);
void RegisterPeople(People people); void RegisterPeople(People people);
@ -59,6 +55,7 @@ namespace Kyoo.Controllers
void EditShow(Show show, bool resetOld); void EditShow(Show show, bool resetOld);
void EditSeason(Season season, bool resetOld); void EditSeason(Season season, bool resetOld);
void EditEpisode(Episode episode, bool resetOld); void EditEpisode(Episode episode, bool resetOld);
void EditTrack(Track track, bool resetOld);
void EditGenre(Genre genre, bool resetOld); void EditGenre(Genre genre, bool resetOld);
void EditStudio(Studio studio, bool resetOld); void EditStudio(Studio studio, bool resetOld);
void EditPeople(People people, bool resetOld); void EditPeople(People people, bool resetOld);
@ -70,6 +67,7 @@ namespace Kyoo.Controllers
void DeleteShow(Show show); void DeleteShow(Show show);
void DeleteSeason(Season season); void DeleteSeason(Season season);
void DeleteEpisode(Episode episode); void DeleteEpisode(Episode episode);
void DeleteTrack(Track track);
void DeleteGenre(Genre genre); void DeleteGenre(Genre genre);
void DeleteStudio(Studio studio); void DeleteStudio(Studio studio);
void DeletePeople(People people); void DeletePeople(People people);

View File

@ -1,31 +1,35 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Kyoo.Models; using Kyoo.Models;
namespace Kyoo.Controllers namespace Kyoo.Controllers
{ {
public interface IRepository<T> public interface IRepository<T>
{ {
T Get(string slug); Task<T> Get(long id);
IEnumerable<T> Search(string query); Task<T> Get(string slug);
IEnumerable<T> GetAll(); Task<IEnumerable<T>> Search(string query);
T Create(T obj); Task<IEnumerable<T>> GetAll();
T CreateIfNotExists(T obj); Task<long> Create([NotNull] T obj);
void Edit(T edited, bool resetOld); Task<long> CreateIfNotExists([NotNull] T obj);
void Delete(string slug); Task Edit([NotNull] T edited, bool resetOld);
Task Delete(T obj);
} }
public interface IShowRepository : IRepository<Show> {} public interface IShowRepository : IRepository<Show> {}
public interface ISeasonRepository : IRepository<Season> public interface ISeasonRepository : IRepository<Season>
{ {
Season Get(string showSlug, int seasonNumber); Season Get(string showSlug, long seasonNumber);
} }
public interface IEpisodeRepository : IRepository<Episode> public interface IEpisodeRepository : IRepository<Episode>
{ {
Episode Get(string showSlug, int seasonNumber, int episodeNumber); Episode Get(string showSlug, long seasonNumber, long episodeNumber);
} }
public interface ITrackRepository : IRepository<Track> {}
public interface ILibraryRepository : IRepository<Library> {} public interface ILibraryRepository : IRepository<Library> {}
public interface ICollectionRepository : IRepository<Collection> {} public interface ICollectionRepository : IRepository<Collection> {}
public interface IGenreRepository : IRepository<Genre> {} public interface IGenreRepository : IRepository<Genre> {}

View File

@ -36,6 +36,7 @@ namespace Kyoo.Models
set => GenreLinks = value?.Select(x => new GenreLink(this, x)).ToList(); set => GenreLinks = value?.Select(x => new GenreLink(this, x)).ToList();
} }
[NotMergable] [JsonIgnore] public virtual IEnumerable<GenreLink> GenreLinks { get; set; } [NotMergable] [JsonIgnore] public virtual IEnumerable<GenreLink> GenreLinks { get; set; }
[JsonIgnore] public long StudioID { get; set; }
public virtual Studio Studio { get; set; } public virtual Studio Studio { get; set; }
[JsonIgnore] public virtual IEnumerable<PeopleLink> People { get; set; } [JsonIgnore] public virtual IEnumerable<PeopleLink> People { get; set; }
[JsonIgnore] public virtual IEnumerable<Season> Seasons { get; set; } [JsonIgnore] public virtual IEnumerable<Season> Seasons { get; set; }

View File

@ -1,57 +1,310 @@
using System; using System.Collections.Generic;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Kyoo.Models; using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
namespace Kyoo.Controllers namespace Kyoo.Controllers
{ {
public class LibraryManager : ILibraryManager public class LibraryManager : ILibraryManager
{ {
private readonly ILibraryRepository _library; private readonly ILibraryRepository _libraries;
private readonly ICollectionRepository _collections;
private readonly IShowRepository _shows;
private readonly ISeasonRepository _seasons;
private readonly IEpisodeRepository _episodes;
private readonly ITrackRepository _tracks;
private readonly IGenreRepository _genres;
private readonly IStudioRepository _studios;
private readonly IPeopleRepository _people;
public LibraryManager(ILibraryRepository library) public LibraryManager(ILibraryRepository libraries,
ICollectionRepository collections,
IShowRepository shows,
ISeasonRepository seasons,
IEpisodeRepository episodes,
ITrackRepository tracks,
IGenreRepository genres,
IStudioRepository studios,
IPeopleRepository people)
{ {
_library = library; _libraries = libraries;
_collections = collections;
_shows = shows;
_seasons = seasons;
_episodes = episodes;
_tracks = tracks;
_genres = genres;
_studios = studios;
_people = people;
} }
public Library Get(string slug) public Library GetLibrary(string slug)
{ {
throw new NotImplementedException(); return _libraries.Get(slug);
} }
public IEnumerable<Library> Search(string query) public Collection GetCollection(string slug)
{ {
throw new NotImplementedException(); return _collections.Get(slug);
} }
public IEnumerable<Library> GetAll() public Show GetShow(string slug)
{ {
throw new NotImplementedException(); return _shows.Get(slug);
} }
public Library Create(Library obj) public Season GetSeason(string showSlug, long seasonNumber)
{ {
throw new NotImplementedException(); return _seasons.Get(showSlug, seasonNumber);
} }
public Library CreateIfNotExists(Library obj) public Episode GetEpisode(string showSlug, long seasonNumber, long episodeNumber)
{ {
throw new NotImplementedException(); return _episodes.Get(showSlug, seasonNumber, episodeNumber);
} }
public void Edit(Library edited, bool resetOld) public Episode GetMovieEpisode(string movieSlug)
{ {
throw new NotImplementedException(); return _episodes.Get(movieSlug);
} }
public void Delete(string slug) public Track GetTrack(string slug)
{ {
throw new NotImplementedException(); return _tracks.Get(slug);
}
public Genre GetGenre(string slug)
{
return _genres.Get(slug);
}
public Studio GetStudio(string slug)
{
return _studios.Get(slug);
}
public People GetPeople(string slug)
{
return _people.Get(slug);
}
public IEnumerable<Library> GetLibraries()
{
return _libraries.GetAll();
}
public IEnumerable<Collection> GetCollections()
{
return _collections.GetAll();
}
public IEnumerable<Show> GetShows()
{
return _shows.GetAll();
}
public IEnumerable<Season> GetSeasons()
{
return _seasons.GetAll();
}
public IEnumerable<Episode> GetEpisodes()
{
return _episodes.GetAll();
}
public IEnumerable<Track> GetTracks()
{
return _tracks.GetAll();
}
public IEnumerable<Studio> GetStudios()
{
return _studios.GetAll();
}
public IEnumerable<People> GetPeoples()
{
return _people.GetAll();
}
public IEnumerable<Genre> GetGenres()
{
return _genres.GetAll();
}
public IEnumerable<Library> SearchLibraries(string searchQuery)
{
return _libraries.Search(searchQuery);
}
public IEnumerable<Collection> SearchCollections(string searchQuery)
{
return _collections.Search(searchQuery);
}
public IEnumerable<Show> SearchShows(string searchQuery)
{
return _shows.Search(searchQuery);
}
public IEnumerable<Season> SearchSeasons(string searchQuery)
{
return _seasons.Search(searchQuery);
}
public IEnumerable<Episode> SearchEpisodes(string searchQuery)
{
return _episodes.Search(searchQuery);
}
public IEnumerable<Genre> SearchGenres(string searchQuery)
{
return _genres.Search(searchQuery);
}
public IEnumerable<Studio> SearchStudios(string searchQuery)
{
return _studios.Search(searchQuery);
}
public IEnumerable<People> SearchPeople(string searchQuery)
{
return _people.Search(searchQuery);
}
public void RegisterLibrary(Library library)
{
_libraries.Create(library);
}
public void RegisterCollection(Collection collection)
{
_collections.Create(collection);
}
public void RegisterShow(Show show)
{
_shows.Create(show);
}
public void RegisterSeason(Season season)
{
_seasons.Create(season);
}
public void RegisterEpisode(Episode episode)
{
_episodes.Create(episode);
}
public void RegisterTrack(Track track)
{
_tracks.Create(track);
}
public void RegisterGenre(Genre genre)
{
_genres.Create(genre);
}
public void RegisterStudio(Studio studio)
{
_studios.Create(studio);
}
public void RegisterPeople(People people)
{
_people.Create(people);
}
public void EditLibrary(Library library, bool resetOld)
{
_libraries.Edit(library, resetOld);
}
public void EditCollection(Collection collection, bool resetOld)
{
throw new System.NotImplementedException();
}
public void EditShow(Show show, bool resetOld)
{
throw new System.NotImplementedException();
}
public void EditSeason(Season season, bool resetOld)
{
throw new System.NotImplementedException();
}
public void EditEpisode(Episode episode, bool resetOld)
{
throw new System.NotImplementedException();
}
public void EditTrack(Track track, bool resetOld)
{
throw new System.NotImplementedException();
}
public void EditGenre(Genre genre, bool resetOld)
{
throw new System.NotImplementedException();
}
public void EditStudio(Studio studio, bool resetOld)
{
throw new System.NotImplementedException();
}
public void EditPeople(People people, bool resetOld)
{
throw new System.NotImplementedException();
}
public void DelteLibrary(Library library)
{
throw new System.NotImplementedException();
}
public void DeleteCollection(Collection collection)
{
throw new System.NotImplementedException();
}
public void DeleteShow(Show show)
{
throw new System.NotImplementedException();
}
public void DeleteSeason(Season season)
{
throw new System.NotImplementedException();
}
public void DeleteEpisode(Episode episode)
{
throw new System.NotImplementedException();
}
public void DeleteTrack(Track track)
{
throw new System.NotImplementedException();
}
public void DeleteGenre(Genre genre)
{
throw new System.NotImplementedException();
}
public void DeleteStudio(Studio studio)
{
throw new System.NotImplementedException();
}
public void DeletePeople(People people)
{
throw new System.NotImplementedException();
} }
} }
} }

View File

@ -1,7 +1,9 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Kyoo.Models; using Kyoo.Models;
using Kyoo.Models.Exceptions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace Kyoo.Controllers namespace Kyoo.Controllers
@ -9,59 +11,100 @@ namespace Kyoo.Controllers
public class ShowRepository : IShowRepository public class ShowRepository : IShowRepository
{ {
private readonly DatabaseContext _database; private readonly DatabaseContext _database;
private readonly ILibraryManager _library; private readonly IGenreRepository _genres;
private readonly IPeopleRepository _people;
private readonly IStudioRepository _studio;
public ShowRepository(DatabaseContext database, ILibraryManager library) public ShowRepository(DatabaseContext database,
IGenreRepository genres,
IPeopleRepository people,
IStudioRepository studio)
{ {
_database = database; _database = database;
_library = library; _genres = genres;
_people = people;
_studio = studio;
} }
public Show Get(string slug) public Task<Show> Get(long id)
{ {
return _database.Shows.FirstOrDefault(x => x.Slug == slug); return Task.FromResult(_database.Shows.FirstOrDefault(x => x.ID == id));
}
public Task<Show> Get(string slug)
{
return Task.FromResult(_database.Shows.FirstOrDefault(x => x.Slug == slug));
} }
public IEnumerable<Show> Search(string query) public Task<IEnumerable<Show>> Search(string query)
{ {
return _database.Shows.FromSqlInterpolated($@"SELECT * FROM Shows WHERE Shows.Title LIKE {$"%{query}%"} return Task.FromResult<IEnumerable<Show>>(
OR Shows.Aliases LIKE {$"%{query}%"}").Take(20); _database.Shows.FromSqlInterpolated($@"SELECT * FROM Shows WHERE Shows.Title LIKE {$"%{query}%"}
OR Shows.Aliases LIKE {$"%{query}%"}").Take(20).ToList());
} }
public IEnumerable<Show> GetAll() public Task<IEnumerable<Show>> GetAll()
{ {
return _database.Shows.ToList(); return Task.FromResult<IEnumerable<Show>>(_database.Shows.ToList());
} }
public Show Create(Show obj) public async Task<long> Create(Show obj)
{ {
if (obj == null) if (obj == null)
throw new ArgumentNullException(nameof(obj)); throw new ArgumentNullException(nameof(obj));
obj.Genres = obj.Genres.Select(_library.CreateIfNotExists).ToList(); // TODO handle ExternalIDs.
obj.StudioID = await _studio.CreateIfNotExists(obj.Studio);
obj.GenreLinks = (await Task.WhenAll(obj.GenreLinks.Select(async x =>
{
x.GenreID = await _genres.CreateIfNotExists(x.Genre);
return x;
}))).ToList();
obj.People = (await Task.WhenAll(obj.People.Select(async x =>
{
x.PeopleID = await _people.CreateIfNotExists(x.People);
return x;
}))).ToList();
obj.Seasons = null;
obj.Episodes = null;
_database.Shows.Add(obj); await _database.Shows.AddAsync(obj);
_database.SaveChanges(); await _database.SaveChangesAsync();
return obj; return obj.ID;
} }
public Show CreateIfNotExists(Show obj) public async Task<long> CreateIfNotExists(Show obj)
{ {
if (obj == null) if (obj == null)
throw new ArgumentNullException(nameof(obj)); throw new ArgumentNullException(nameof(obj));
Show old = await Get(obj.Slug);
if (old != null)
return old.ID;
return await Create(obj);
}
public async Task Edit(Show edited, bool resetOld)
{
if (edited == null)
throw new ArgumentNullException(nameof(edited));
return Get(obj.Slug) ?? Create(obj); Show old = await Get(edited.Slug);
if (old == null)
throw new ItemNotFound($"No show found with the slug {edited.Slug}.");
if (resetOld)
Utility.Nullify(old);
Utility.Merge(old, edited);
await _database.SaveChangesAsync();
} }
public void Edit(Show edited, bool resetOld) public async Task Delete(Show show)
{ {
throw new System.NotImplementedException(); _database.Shows.Remove(show);
} await _database.SaveChangesAsync();
public void Delete(string slug)
{
throw new System.NotImplementedException();
} }
} }
} }