Finishing the Lazy load migration and solving some bugs with the API

This commit is contained in:
Zoe Roux 2020-02-10 22:17:49 +01:00
parent cbc10acf54
commit d28f976d2b
7 changed files with 41 additions and 16 deletions

View File

@ -1,4 +1,5 @@
using Newtonsoft.Json;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Kyoo.Models
{
@ -8,6 +9,8 @@ namespace Kyoo.Models
public string Slug { get; set; }
public string Name { get; set; }
// public IEnumerable<Show> Shows { get; set; }
public Genre(string slug, string name)
{
Slug = slug;

View File

@ -2,12 +2,17 @@ namespace Kyoo.Models
{
public class GenreLink
{
// TODO Make json serializer ignore this class and only serialize the Genre child.
// TODO include this class in the EF workflows.
public long ShowID { get; set; }
public virtual Show Show { get; set; }
public long GenreID { get; set; }
public virtual Genre Genre { get; set; }
public GenreLink() {}
public GenreLink(Show show, Genre genre)
{
Show = show;
Genre = genre;
}
}
}

View File

@ -6,7 +6,12 @@ namespace Kyoo.Models
{
[JsonIgnore] public long ID { get; set; }
[JsonIgnore] public long PeopleID { get; set; }
public virtual People People { get; set; }
[JsonIgnore] public virtual People People { get; set; }
public string Slug => People.Slug;
public string Name => People.Name;
public string ExternalIDs => People.ExternalIDs;
[JsonIgnore] public long ShowID { get; set; }
[JsonIgnore] public virtual Show Show { get; set; }
public string Role { get; set; }

View File

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

View File

@ -56,7 +56,7 @@ namespace Kyoo.Models
public class Track : Stream
{
public int ID { get; set; }
[JsonIgnore] public long ID { get; set; }
[JsonIgnore] public long EpisodeID { get; set; }
public bool IsDefault
{
@ -72,7 +72,7 @@ namespace Kyoo.Models
public string Link;
[JsonIgnore] public bool IsExternal { get; set; }
public virtual Episode Episode { get; set; }
[JsonIgnore] public virtual Episode Episode { get; set; }
public Track() { }

View File

@ -76,14 +76,7 @@ namespace Kyoo.Controllers
public Show GetShowBySlug(string slug)
{
Show ret = (from show in _database.Shows where show.Slug == slug select show).FirstOrDefault();
// if (ret == null)
// return null;
//_database.Entry(ret).Collection(x => x.Genres).Load();
// _database.Entry(ret).Reference(x => x.Studio).Load();
// _database.Entry(ret).Collection(x => x.People).Load();
// _database.Entry(ret).Collection(x => x.Seasons).Load();
return ret;
return (from show in _database.Shows where show.Slug == slug select show).FirstOrDefault();
}
public Show GetShow(string path)

View File

@ -25,6 +25,10 @@ namespace Kyoo
public DbSet<CollectionLink> CollectionLinks { get; set; }
public DbSet<PeopleLink> PeopleLinks { get; set; }
// This is used because EF doesn't support Many-To-Many relationships so for now we need to override the getter/setters to store this.
public DbSet<GenreLink> GenreLinks { get; set; }
private ValueConverter<string[], string> stringArrayConverter = new ValueConverter<string[], string>(
arr => string.Join("|", arr),
str => str.Split("|", StringSplitOptions.None));
@ -48,6 +52,16 @@ namespace Kyoo
modelBuilder.Entity<Track>()
.Property(t => t.IsForced)
.ValueGeneratedNever();
modelBuilder.Entity<GenreLink>()
.HasKey(x => new {x.ShowID, x.GenreID});
modelBuilder.Entity<Show>()
.Ignore(x => x.Genres);
// modelBuilder.Entity<Genre>()
// .Ignore(x => x.Shows);
}
}
}