New Scanner + People Pages (#3286)
Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
This commit is contained in:
parent
1ed0eae22d
commit
ba20ad4ecc
142 changed files with 17529 additions and 3038 deletions
|
|
@ -1,153 +1,120 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.Data;
|
||||
using API.DTOs.Metadata;
|
||||
using API.Entities;
|
||||
using API.Extensions;
|
||||
using API.Helpers.Builders;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace API.Helpers;
|
||||
#nullable enable
|
||||
|
||||
public static class GenreHelper
|
||||
{
|
||||
|
||||
public static void UpdateGenre(Dictionary<string, Genre> allGenres,
|
||||
IEnumerable<string> names, Action<Genre, bool> action)
|
||||
public static async Task UpdateChapterGenres(Chapter chapter, IEnumerable<string> genreNames, IUnitOfWork unitOfWork)
|
||||
{
|
||||
foreach (var name in names)
|
||||
{
|
||||
var normalizedName = name.ToNormalized();
|
||||
if (string.IsNullOrEmpty(normalizedName)) continue;
|
||||
// Normalize genre names once and store them in a hash set for quick lookups
|
||||
var normalizedGenresToAdd = new HashSet<string>(genreNames.Select(g => g.ToNormalized()));
|
||||
|
||||
if (allGenres.TryGetValue(normalizedName, out var genre))
|
||||
// Remove genres that are no longer in the new list
|
||||
var genresToRemove = chapter.Genres
|
||||
.Where(g => !normalizedGenresToAdd.Contains(g.NormalizedTitle))
|
||||
.ToList();
|
||||
|
||||
if (genresToRemove.Count > 0)
|
||||
{
|
||||
foreach (var genreToRemove in genresToRemove)
|
||||
{
|
||||
action(genre, false);
|
||||
chapter.Genres.Remove(genreToRemove);
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
// Get all normalized titles to query the database for existing genres
|
||||
var existingGenreTitles = await unitOfWork.DataContext.Genre
|
||||
.Where(g => normalizedGenresToAdd.Contains(g.NormalizedTitle))
|
||||
.ToDictionaryAsync(g => g.NormalizedTitle);
|
||||
|
||||
// Find missing genres that are not in the database
|
||||
var missingGenres = normalizedGenresToAdd
|
||||
.Where(nt => !existingGenreTitles.ContainsKey(nt))
|
||||
.Select(title => new GenreBuilder(title).Build())
|
||||
.ToList();
|
||||
|
||||
// Add missing genres to the database
|
||||
if (missingGenres.Count > 0)
|
||||
{
|
||||
unitOfWork.DataContext.Genre.AddRange(missingGenres);
|
||||
await unitOfWork.CommitAsync();
|
||||
|
||||
// Add newly inserted genres to existing genres dictionary for easier lookup
|
||||
foreach (var genre in missingGenres)
|
||||
{
|
||||
genre = new GenreBuilder(name).Build();
|
||||
allGenres.Add(normalizedName, genre);
|
||||
action(genre, true);
|
||||
existingGenreTitles[genre.NormalizedTitle] = genre;
|
||||
}
|
||||
}
|
||||
|
||||
// Add genres that are either existing or newly added to the chapter
|
||||
foreach (var normalizedTitle in normalizedGenresToAdd)
|
||||
{
|
||||
var genre = existingGenreTitles[normalizedTitle];
|
||||
|
||||
if (!chapter.Genres.Contains(genre))
|
||||
{
|
||||
chapter.Genres.Add(genre);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void KeepOnlySameGenreBetweenLists(ICollection<Genre> existingGenres, ICollection<Genre> removeAllExcept, Action<Genre>? action = null)
|
||||
{
|
||||
var existing = existingGenres.ToList();
|
||||
foreach (var genre in existing)
|
||||
{
|
||||
var existingPerson = removeAllExcept.FirstOrDefault(g => genre.NormalizedTitle != null && genre.NormalizedTitle.Equals(g.NormalizedTitle));
|
||||
if (existingPerson != null) continue;
|
||||
existingGenres.Remove(genre);
|
||||
action?.Invoke(genre);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the genre to the list if it's not already in there.
|
||||
/// </summary>
|
||||
/// <param name="metadataGenres"></param>
|
||||
/// <param name="genre"></param>
|
||||
public static void AddGenreIfNotExists(ICollection<Genre> metadataGenres, Genre genre)
|
||||
{
|
||||
var existingGenre = metadataGenres.FirstOrDefault(p =>
|
||||
p.NormalizedTitle.Equals(genre.Title?.ToNormalized()));
|
||||
if (existingGenre == null)
|
||||
{
|
||||
metadataGenres.Add(genre);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void UpdateGenreList(ICollection<GenreTagDto>? tags, Series series,
|
||||
IReadOnlyCollection<Genre> allTags, Action<Genre> handleAdd, Action onModified)
|
||||
public static void UpdateGenreList(ICollection<GenreTagDto>? existingGenres, Series series,
|
||||
IReadOnlyCollection<Genre> newGenres, Action<Genre> handleAdd, Action onModified)
|
||||
{
|
||||
// TODO: Write some unit tests
|
||||
if (tags == null) return;
|
||||
if (existingGenres == null) return;
|
||||
|
||||
var isModified = false;
|
||||
// I want a union of these 2 lists. Return only elements that are in both lists, but the list types are different
|
||||
var existingTags = series.Metadata.Genres.ToList();
|
||||
|
||||
// Convert tags and existing genres to hash sets for quick lookups by normalized title
|
||||
var tagSet = new HashSet<string>(existingGenres.Select(t => t.Title.ToNormalized()));
|
||||
var genreSet = new HashSet<string>(series.Metadata.Genres.Select(g => g.NormalizedTitle));
|
||||
|
||||
// Remove tags that are no longer present in the input tags
|
||||
var existingTags = series.Metadata.Genres.ToList(); // Copy to avoid modifying collection while iterating
|
||||
foreach (var existing in existingTags)
|
||||
{
|
||||
if (tags.SingleOrDefault(t => t.Title.ToNormalized().Equals(existing.NormalizedTitle)) == null)
|
||||
if (!tagSet.Contains(existing.NormalizedTitle)) // This correctly ensures removal of non-present tags
|
||||
{
|
||||
// Remove tag
|
||||
series.Metadata.Genres.Remove(existing);
|
||||
isModified = true;
|
||||
}
|
||||
}
|
||||
|
||||
// At this point, all tags that aren't in dto have been removed.
|
||||
foreach (var tagTitle in tags.Select(t => t.Title))
|
||||
// Prepare a dictionary for quick lookup of genres from the `newGenres` collection by normalized title
|
||||
var allTagsDict = newGenres.ToDictionary(t => t.NormalizedTitle);
|
||||
|
||||
// Add new tags from the input list
|
||||
foreach (var tagDto in existingGenres)
|
||||
{
|
||||
var normalizedTitle = tagTitle.ToNormalized();
|
||||
var existingTag = allTags.SingleOrDefault(t => t.NormalizedTitle.Equals(normalizedTitle));
|
||||
if (existingTag != null)
|
||||
var normalizedTitle = tagDto.Title.ToNormalized();
|
||||
|
||||
if (!genreSet.Contains(normalizedTitle)) // This prevents re-adding existing genres
|
||||
{
|
||||
if (series.Metadata.Genres.All(t => !t.NormalizedTitle.Equals(normalizedTitle)))
|
||||
if (allTagsDict.TryGetValue(normalizedTitle, out var existingTag))
|
||||
{
|
||||
handleAdd(existingTag);
|
||||
isModified = true;
|
||||
handleAdd(existingTag); // Add existing tag from allTagsDict
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add new tag
|
||||
handleAdd(new GenreBuilder(tagTitle).Build());
|
||||
isModified = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (isModified)
|
||||
{
|
||||
onModified();
|
||||
}
|
||||
}
|
||||
|
||||
public static void UpdateGenreList(ICollection<GenreTagDto>? tags, Chapter chapter,
|
||||
IReadOnlyCollection<Genre> allTags, Action<Genre> handleAdd, Action onModified)
|
||||
{
|
||||
// TODO: Write some unit tests
|
||||
if (tags == null) return;
|
||||
var isModified = false;
|
||||
// I want a union of these 2 lists. Return only elements that are in both lists, but the list types are different
|
||||
var existingTags = chapter.Genres.ToList();
|
||||
foreach (var existing in existingTags)
|
||||
{
|
||||
if (tags.SingleOrDefault(t => t.Title.ToNormalized().Equals(existing.NormalizedTitle)) == null)
|
||||
{
|
||||
// Remove tag
|
||||
chapter.Genres.Remove(existing);
|
||||
isModified = true;
|
||||
}
|
||||
}
|
||||
|
||||
// At this point, all tags that aren't in dto have been removed.
|
||||
foreach (var tagTitle in tags.Select(t => t.Title))
|
||||
{
|
||||
var normalizedTitle = tagTitle.ToNormalized();
|
||||
var existingTag = allTags.SingleOrDefault(t => t.NormalizedTitle.Equals(normalizedTitle));
|
||||
if (existingTag != null)
|
||||
{
|
||||
if (chapter.Genres.All(t => !t.NormalizedTitle.Equals(normalizedTitle)))
|
||||
else
|
||||
{
|
||||
handleAdd(existingTag);
|
||||
isModified = true;
|
||||
handleAdd(new GenreBuilder(tagDto.Title).Build()); // Add new genre if not found
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Add new tag
|
||||
handleAdd(new GenreBuilder(tagTitle).Build());
|
||||
isModified = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Call onModified if any changes were made
|
||||
if (isModified)
|
||||
{
|
||||
onModified();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue