Fixes all lowercase issue (#3302)

This commit is contained in:
Joe Milazzo 2024-10-24 05:36:34 -07:00 committed by GitHub
parent 184c766fa7
commit b65b78a736
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 377 additions and 65 deletions

View file

@ -72,7 +72,6 @@ public static class GenreHelper
public static void UpdateGenreList(ICollection<GenreTagDto>? existingGenres, Series series,
IReadOnlyCollection<Genre> newGenres, Action<Genre> handleAdd, Action onModified)
{
// TODO: Write some unit tests
if (existingGenres == null) return;
var isModified = false;
@ -100,18 +99,17 @@ public static class GenreHelper
{
var normalizedTitle = tagDto.Title.ToNormalized();
if (!genreSet.Contains(normalizedTitle)) // This prevents re-adding existing genres
if (genreSet.Contains(normalizedTitle)) continue; // This prevents re-adding existing genres
if (allTagsDict.TryGetValue(normalizedTitle, out var existingTag))
{
if (allTagsDict.TryGetValue(normalizedTitle, out var existingTag))
{
handleAdd(existingTag); // Add existing tag from allTagsDict
}
else
{
handleAdd(new GenreBuilder(tagDto.Title).Build()); // Add new genre if not found
}
isModified = true;
handleAdd(existingTag); // Add existing tag from allTagsDict
}
else
{
handleAdd(new GenreBuilder(tagDto.Title).Build()); // Add new genre if not found
}
isModified = true;
}
// Call onModified if any changes were made

View file

@ -22,14 +22,14 @@ public static class PersonHelper
{
var modification = false;
// Get all normalized names of people with the specified role from chapterPeople
// Get all people with the specified role from chapterPeople
var peopleToAdd = chapterPeople
.Where(cp => cp.Role == role)
.Select(cp => cp.Person.NormalizedName)
.Select(cp => new { cp.Person.Name, cp.Person.NormalizedName }) // Store both real and normalized names
.ToList();
// Prepare a HashSet for quick lookup of people to add
var peopleToAddSet = new HashSet<string>(peopleToAdd);
// Prepare a HashSet for quick lookup of normalized names of people to add
var peopleToAddSet = new HashSet<string>(peopleToAdd.Select(p => p.NormalizedName));
// Get all existing people from metadataPeople with the specified role
var existingMetadataPeople = metadataPeople
@ -48,9 +48,9 @@ public static class PersonHelper
modification = true;
}
// Bulk fetch existing people from the repository
// Bulk fetch existing people from the repository based on normalized names
var existingPeopleInDb = await unitOfWork.PersonRepository
.GetPeopleByNames(peopleToAdd);
.GetPeopleByNames(peopleToAdd.Select(p => p.NormalizedName).ToList());
// Prepare a dictionary for quick lookup of existing people by normalized name
var existingPeopleDict = new Dictionary<string, Person>();
@ -63,18 +63,21 @@ public static class PersonHelper
var peopleToAttach = new List<Person>();
// Identify new people (not already in metadataPeople) to add
foreach (var personName in peopleToAdd)
foreach (var personData in peopleToAdd)
{
var personName = personData.Name;
var normalizedPersonName = personData.NormalizedName;
// Check if the person already exists in metadataPeople with the specific role
var personAlreadyInMetadata = metadataPeople
.Any(mp => mp.Person.NormalizedName == personName && mp.Role == role);
.Any(mp => mp.Person.NormalizedName == normalizedPersonName && mp.Role == role);
if (!personAlreadyInMetadata)
{
// Check if the person exists in the database
if (!existingPeopleDict.TryGetValue(personName, out var dbPerson))
if (!existingPeopleDict.TryGetValue(normalizedPersonName, out var dbPerson))
{
// If not, create a new Person entity
// If not, create a new Person entity using the real name
dbPerson = new PersonBuilder(personName).Build();
peopleToAttach.Add(dbPerson); // Add new person to the list to be attached
modification = true;
@ -107,6 +110,7 @@ public static class PersonHelper
}
public static async Task UpdateChapterPeopleAsync(Chapter chapter, IList<string> people, PersonRole role, IUnitOfWork unitOfWork)
{
var modification = false;
@ -119,10 +123,10 @@ public static class PersonHelper
.Where(cp => cp.Role == role)
.ToList();
// Prepare a hash set for quick lookup of existing people by name
// Prepare a hash set for quick lookup of existing people by normalized name
var existingPeopleNames = new HashSet<string>(existingChapterPeople.Select(cp => cp.Person.NormalizedName));
// Bulk select all people from the repository whose names are in the provided list
// Bulk select all people from the repository whose normalized names are in the provided list
var existingPeople = await unitOfWork.PersonRepository.GetPeopleByNames(normalizedPeople);
// Prepare a dictionary for quick lookup by normalized name
@ -151,7 +155,11 @@ public static class PersonHelper
// Bulk insert new people (if they don't already exist in the database)
var newPeople = newPeopleNames
.Where(name => !existingPeopleDict.ContainsKey(name)) // Avoid adding duplicates
.Select(name => new PersonBuilder(name).Build())
.Select(name =>
{
var realName = people.First(p => p.ToNormalized() == name); // Get the original name
return new PersonBuilder(realName).Build(); // Use the real name for the Person entity
})
.ToList();
foreach (var newPerson in newPeople)
@ -188,6 +196,7 @@ public static class PersonHelper
}
}
public static bool HasAnyPeople(SeriesMetadataDto? dto)
{
if (dto == null) return false;

View file

@ -51,7 +51,7 @@ public static class TagHelper
.ToList();
// Add missing tags to the database if any
if (missingTags.Any())
if (missingTags.Count != 0)
{
unitOfWork.DataContext.Tag.AddRange(missingTags);
await unitOfWork.CommitAsync(); // Commit once after adding missing tags to avoid multiple DB calls
@ -102,42 +102,49 @@ public static class TagHelper
}
public static void UpdateTagList(ICollection<TagDto>? tags, Series series, IReadOnlyCollection<Tag> allTags, Action<Tag> handleAdd, Action onModified)
public static void UpdateTagList(ICollection<TagDto>? existingDbTags, Series series, IReadOnlyCollection<Tag> newTags, Action<Tag> handleAdd, Action onModified)
{
if (tags == null) return;
if (existingDbTags == null) return;
var isModified = false;
var existingTags = series.Metadata.Tags;
// Create a HashSet for quick lookup of tag IDs
var tagIds = new HashSet<int>(tags.Select(t => t.Id));
// Convert tags and existing genres to hash sets for quick lookups by normalized title
var existingTagSet = new HashSet<string>(existingDbTags.Select(t => t.Title.ToNormalized()));
var dbTagSet = new HashSet<string>(series.Metadata.Tags.Select(g => g.NormalizedTitle));
// Remove tags that no longer exist in the provided tag list
var tagsToRemove = existingTags.Where(existing => !tagIds.Contains(existing.Id)).ToList();
if (tagsToRemove.Count > 0)
// Remove tags that are no longer present in the input tags
var existingTagsCopy = series.Metadata.Tags.ToList(); // Copy to avoid modifying collection while iterating
foreach (var existing in existingTagsCopy)
{
foreach (var tagToRemove in tagsToRemove)
if (!existingTagSet.Contains(existing.NormalizedTitle)) // This correctly ensures removal of non-present tags
{
existingTags.Remove(tagToRemove);
series.Metadata.Tags.Remove(existing);
isModified = true;
}
}
// Prepare a dictionary for quick lookup of genres from the `newTags` collection by normalized title
var allTagsDict = newTags.ToDictionary(t => t.NormalizedTitle);
// Add new tags from the input list
foreach (var tagDto in existingDbTags)
{
var normalizedTitle = tagDto.Title.ToNormalized();
if (dbTagSet.Contains(normalizedTitle)) continue; // This prevents re-adding existing genres
if (allTagsDict.TryGetValue(normalizedTitle, out var existingTag))
{
handleAdd(existingTag); // Add existing tag from allTagsDict
}
else
{
handleAdd(new TagBuilder(tagDto.Title).Build()); // Add new genre if not found
}
isModified = true;
}
// Create a HashSet of normalized titles for quick lookups
var normalizedTitlesToAdd = new HashSet<string>(tags.Select(t => t.Title.ToNormalized()));
var existingNormalizedTitles = new HashSet<string>(existingTags.Select(t => t.NormalizedTitle));
// Add missing tags based on normalized title comparison
foreach (var normalizedTitle in normalizedTitlesToAdd)
{
if (existingNormalizedTitles.Contains(normalizedTitle)) continue;
var existingTag = allTags.FirstOrDefault(t => t.NormalizedTitle == normalizedTitle);
handleAdd(existingTag ?? new TagBuilder(normalizedTitle).Build());
isModified = true;
}
// Call the modification handler if any changes were made
// Call onModified if any changes were made
if (isModified)
{
onModified();