Trying to prevent duplicated of related entities

This commit is contained in:
Zoe Roux 2020-04-16 02:34:30 +02:00
parent e890c05660
commit 9b42680b31
6 changed files with 27 additions and 11 deletions

View File

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

View File

@ -347,8 +347,25 @@ namespace Kyoo.Controllers
{
if (show == null)
return 0;
if (!_database.Entry(show).IsKeySet)
_database.Shows.Add(show);
if (show.Studio != null) // Do not query it if the studio is already attached. // Do not set this value if the database does not found the studio.
show.Studio = _database.Studios.FirstOrDefault(x => x.Slug == show.Studio.Slug);
// show.GenreLinks = show.GenreLinks?.Select(x =>
// {
// x.Genre = _database.Genres.FirstOrDefault(y => y.Slug == x.Genre.Slug) ?? x.Genre;
// return x;
// });
// show.People = show.People?.Select(x =>
// {
// x.People = _database.Peoples.FirstOrDefault(y => y.Slug == x.Slug) ?? x.People;
// return x;
// });
// show.Seasons = show.Seasons?.Select(x => _database.Seasons.FirstOrDefault(y => y.SeasonNumber == x.SeasonNumber) ?? x);
// show.Episodes = show.Episodes?.Select(x => _database.Episodes.FirstOrDefault(y => y.EpisodeNumber == x.EpisodeNumber && y.SeasonNumber == x.SeasonNumber) ?? x);
//
_database.SaveChanges();
return show.ID;
}

View File

@ -65,7 +65,7 @@ namespace Kyoo
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) {}
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { }
public DbSet<Library> Libraries { get; set; }
public DbSet<Collection> Collections { get; set; }

View File

@ -52,8 +52,8 @@ namespace Kyoo
services.AddDbContext<DatabaseContext>(options =>
{
options.UseLazyLoadingProxies()
.UseSqlite(_configuration.GetConnectionString("Database"));
//.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()));
.UseSqlite(_configuration.GetConnectionString("Database"))
.UseLoggerFactory(LoggerFactory.Create(builder => builder.AddConsole()));
});
services.AddDbContext<IdentityDatabase>(options =>

View File

@ -144,9 +144,10 @@ namespace Kyoo.Controllers
if (seasonNumber == -1)
return null;
Season season = _libraryManager.GetSeason(show.Slug, seasonNumber);
if (season != null)
return await Task.FromResult(season);
return await _metadataProvider.GetSeason(show, seasonNumber, library);
if (season == null)
return await _metadataProvider.GetSeason(show, seasonNumber, library);
season.Show = show;
return await Task.FromResult(season);
}
private async Task<Episode> GetEpisode(Show show, Season season, long episodeNumber, long absoluteNumber, string episodePath, Library library)

View File

@ -47,13 +47,11 @@ namespace Kyoo.Api
{
if (!ModelState.IsValid)
return BadRequest(show);
show.ID = 0;
Show old = _libraryManager.GetShowBySlug(slug);
if (old == null)
return NotFound();
show.ID = 0;
//Should prevent duplicates (If the user put another studio, it is always created even if there is already a studio with the same slug.
old = Utility.Complete(old, show);
//old = Utility.Complete(old, show);
_libraryManager.RegisterShow(old);
return Ok();
}