Skip to content

Commit

Permalink
Series metadata api is now implemented (had to write new code unfortu…
Browse files Browse the repository at this point in the history
…nately)
  • Loading branch information
majora2007 committed Oct 13, 2024
1 parent e55b0bd commit 3bbe4f2
Show file tree
Hide file tree
Showing 6 changed files with 329 additions and 137 deletions.
15 changes: 15 additions & 0 deletions API/Data/Repositories/PersonRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace API.Data.Repositories;
public interface IPersonRepository
{
void Attach(Person person);
void Attach(IEnumerable<Person> person);
void Remove(Person person);
void Remove(ChapterPeople person);
void Remove(SeriesMetadataPeople person);
Expand All @@ -41,6 +42,7 @@ public interface IPersonRepository

Task<IEnumerable<SeriesDto>> GetSeriesKnownFor(int personId);
Task<IEnumerable<ChapterDto>> GetChaptersForPersonByRole(int personId, int userId, PersonRole role);
Task<IList<Person>> GetPeopleByNames(List<string> normalizedNames);
}

public class PersonRepository : IPersonRepository
Expand All @@ -59,6 +61,11 @@ public void Attach(Person person)
_context.Person.Attach(person);
}

public void Attach(IEnumerable<Person> person)
{
_context.Person.AttachRange(person);
}

public void Remove(Person person)
{
_context.Person.Remove(person);
Expand Down Expand Up @@ -259,6 +266,14 @@ public async Task<IEnumerable<ChapterDto>> GetChaptersForPersonByRole(int person
.ToListAsync();
}

public async Task<IList<Person>> GetPeopleByNames(List<string> normalizedNames)
{
return await _context.Person
.Where(p => normalizedNames.Contains(p.NormalizedName))
.OrderBy(p => p.Name)
.ToListAsync();
}


public async Task<IList<Person>> GetAllPeople()
{
Expand Down
14 changes: 4 additions & 10 deletions API/Data/Repositories/SeriesRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ public enum SeriesIncludes
{
None = 1,
Volumes = 2,
/// <summary>
/// This will include all necessary includes
/// </summary>
Metadata = 4,
Related = 8,
Library = 16,
Chapters = 32,
ExternalReviews = 64,
ExternalRatings = 128,
ExternalRecommendations = 256,
ExternalMetadata = 512

ExternalMetadata = 512,
}

/// <summary>
Expand Down Expand Up @@ -533,14 +535,6 @@ public async Task<SearchResultGroupDto> SearchSeries(int userId, bool isAdmin, I
.SingleOrDefaultAsync();
}

public async Task<Series?> GetSeriesByIdForUserAsync(int seriesId, int userId, SeriesIncludes includes = SeriesIncludes.Volumes | SeriesIncludes.Metadata)
{
return await _context.Series
.Where(s => s.Id == seriesId)
.Includes(includes)
.SingleOrDefaultAsync();
}

/// <summary>
/// Returns Full Series including all external links
/// </summary>
Expand Down
5 changes: 2 additions & 3 deletions API/Extensions/QueryExtensions/IncludesExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,17 +162,16 @@ public static IQueryable<Series> Includes(this IQueryable<Series> query,

if (includeFlags.HasFlag(SeriesIncludes.Metadata))
{
query = query.Include(s => s.Metadata)
.ThenInclude(m => m.CollectionTags.OrderBy(g => g.NormalizedTitle))
query = query
.Include(s => s.Metadata)
.ThenInclude(m => m.Genres.OrderBy(g => g.NormalizedTitle))
.Include(s => s.Metadata)
.ThenInclude(m => m.People)
.ThenInclude(smp => smp.Person)
.Include(s => s.Metadata)
.ThenInclude(m => m.Tags.OrderBy(g => g.NormalizedTitle));
}


return query.AsSplitQuery();
}

Expand Down
129 changes: 129 additions & 0 deletions API/Helpers/PersonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using API.DTOs;
using API.Entities;
using API.Entities.Enums;
using API.Entities.Metadata;
using API.Extensions;
using API.Helpers.Builders;

Expand All @@ -16,6 +17,134 @@ namespace API.Helpers;
public static class PersonHelper
{

public static async Task UpdateSeriesMetadataPeopleAsync(SeriesMetadata metadata, ICollection<SeriesMetadataPeople> metadataPeople,
IEnumerable<ChapterPeople> chapterPeople, PersonRole role, IUnitOfWork unitOfWork)
{
// Normalize and group by person
var peopleToAdd = chapterPeople
.Where(cp => cp.Role == role)
.Select(cp => cp.Person)
.ToList();

var modification = false;

// Remove any people who are not part of the new list
var peopleToRemove = metadataPeople
.Where(mp => mp.Role == role && peopleToAdd.TrueForAll(p => p.NormalizedName != mp.Person.NormalizedName))
.ToList();

foreach (var personToRemove in peopleToRemove)
{
metadataPeople.Remove(personToRemove);
modification = true;
}

// Add new people if they do not already exist
foreach (var person in peopleToAdd)
{
var existingPerson = metadataPeople
.FirstOrDefault(mp => mp.Person.NormalizedName == person.NormalizedName && mp.Role == role);

if (existingPerson == null)
{
// Check if the person already exists in the database
var dbPerson = await unitOfWork.PersonRepository.GetPersonByName(person.Name);

if (dbPerson == null)
{
// Create a new Person entity if not found in the database
dbPerson = new PersonBuilder(person.Name).Build();

// Attach and save the new Person entity
unitOfWork.DataContext.Person.Attach(dbPerson);
await unitOfWork.CommitAsync();
}

// Add the person to the SeriesMetadataPeople collection
metadataPeople.Add(new SeriesMetadataPeople
{
PersonId = dbPerson.Id,
Person = dbPerson,
SeriesMetadataId = metadata.Id,
SeriesMetadata = metadata,
Role = role
});
modification = true;
}
}

// Commit the changes if any modifications were made
if (modification)
{
await unitOfWork.CommitAsync();
}
}


public static async Task UpdateSeriesMetadataPeopleAsync(SeriesMetadata metadata, ICollection<SeriesMetadataPeople> metadataPeople,
IEnumerable<SeriesMetadataPeople> seriesPeople, PersonRole role, IUnitOfWork unitOfWork)
{
// Normalize and group by person
var peopleToAdd = seriesPeople
.Where(cp => cp.Role == role)
.Select(cp => cp.Person)
.ToList();

var modification = false;

// Remove any people who are not part of the new list
var peopleToRemove = metadataPeople
.Where(mp => mp.Role == role && peopleToAdd.TrueForAll(p => p.NormalizedName != mp.Person.NormalizedName))
.ToList();

foreach (var personToRemove in peopleToRemove)
{
metadataPeople.Remove(personToRemove);
modification = true;
}

// Add new people if they do not already exist
foreach (var person in peopleToAdd)
{
var existingPerson = metadataPeople
.FirstOrDefault(mp => mp.Person.NormalizedName == person.NormalizedName && mp.Role == role);

if (existingPerson == null)
{
// Check if the person already exists in the database
var dbPerson = await unitOfWork.PersonRepository.GetPersonByName(person.Name);

if (dbPerson == null)
{
// Create a new Person entity if not found in the database
dbPerson = new PersonBuilder(person.Name).Build();

// Attach and save the new Person entity
unitOfWork.DataContext.Person.Attach(dbPerson);
await unitOfWork.CommitAsync();
}

// Add the person to the SeriesMetadataPeople collection
metadataPeople.Add(new SeriesMetadataPeople
{
PersonId = dbPerson.Id,
Person = dbPerson,
SeriesMetadataId = metadata.Id,
SeriesMetadata = metadata,
Role = role
});
modification = true;
}
}

// Commit the changes if any modifications were made
if (modification)
{
await unitOfWork.CommitAsync();
}
}



public static async Task UpdateChapterPeopleAsync(Chapter chapter, IList<string> people, PersonRole role, IUnitOfWork unitOfWork)
{
Expand Down
Loading

0 comments on commit 3bbe4f2

Please sign in to comment.