Fix Genre/Tag with spaces getting normalized (#3731)

This commit is contained in:
Joe Milazzo 2025-04-13 08:19:42 -06:00 committed by GitHub
parent 55966f0b2a
commit 33221fee2b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 104 additions and 51 deletions

View file

@ -16,14 +16,14 @@ public class GenreBuilder : IEntityBuilder<Genre>
{
Title = name.Trim().SentenceCase(),
NormalizedTitle = name.ToNormalized(),
Chapters = new List<Chapter>(),
SeriesMetadatas = new List<SeriesMetadata>()
Chapters = [],
SeriesMetadatas = []
};
}
public GenreBuilder WithSeriesMetadata(SeriesMetadata seriesMetadata)
{
_genre.SeriesMetadatas ??= new List<SeriesMetadata>();
_genre.SeriesMetadatas ??= [];
_genre.SeriesMetadatas.Add(seriesMetadata);
return this;
}

View file

@ -16,8 +16,8 @@ public class TagBuilder : IEntityBuilder<Tag>
{
Title = name.Trim().SentenceCase(),
NormalizedTitle = name.ToNormalized(),
Chapters = new List<Chapter>(),
SeriesMetadatas = new List<SeriesMetadata>()
Chapters = [],
SeriesMetadatas = []
};
}

View file

@ -19,7 +19,12 @@ public static class GenreHelper
public static async Task UpdateChapterGenres(Chapter chapter, IEnumerable<string> genreNames, IUnitOfWork unitOfWork)
{
// Normalize genre names once and store them in a hash set for quick lookups
var normalizedGenresToAdd = new HashSet<string>(genreNames.Select(g => g.ToNormalized()));
var normalizedToOriginal = genreNames
.Select(g => new { Original = g, Normalized = g.ToNormalized() })
.GroupBy(x => x.Normalized)
.ToDictionary(g => g.Key, g => g.First().Original);
var normalizedGenresToAdd = new HashSet<string>(normalizedToOriginal.Keys);
// Remove genres that are no longer in the new list
var genresToRemove = chapter.Genres
@ -42,7 +47,7 @@ public static class GenreHelper
// Find missing genres that are not in the database
var missingGenres = normalizedGenresToAdd
.Where(nt => !existingGenreTitles.ContainsKey(nt))
.Select(title => new GenreBuilder(title).Build())
.Select(nt => new GenreBuilder(normalizedToOriginal[nt]).Build())
.ToList();
// Add missing genres to the database

View file

@ -20,7 +20,13 @@ public static class TagHelper
public static async Task UpdateChapterTags(Chapter chapter, IEnumerable<string> tagNames, IUnitOfWork unitOfWork)
{
// Normalize tag names once and store them in a hash set for quick lookups
var normalizedTagsToAdd = new HashSet<string>(tagNames.Select(t => t.ToNormalized()));
// Create a dictionary: normalized => original
var normalizedToOriginal = tagNames
.Select(t => new { Original = t, Normalized = t.ToNormalized() })
.GroupBy(x => x.Normalized) // in case of duplicates
.ToDictionary(g => g.Key, g => g.First().Original);
var normalizedTagsToAdd = new HashSet<string>(normalizedToOriginal.Keys);
var existingTagsSet = new HashSet<string>(chapter.Tags.Select(t => t.NormalizedTitle));
var isModified = false;
@ -30,7 +36,7 @@ public static class TagHelper
.Where(t => !normalizedTagsToAdd.Contains(t.NormalizedTitle))
.ToList();
if (tagsToRemove.Any())
if (tagsToRemove.Count != 0)
{
foreach (var tagToRemove in tagsToRemove)
{
@ -47,7 +53,7 @@ public static class TagHelper
// Find missing tags that are not already in the database
var missingTags = normalizedTagsToAdd
.Where(nt => !existingTagTitles.ContainsKey(nt))
.Select(title => new TagBuilder(title).Build())
.Select(nt => new TagBuilder(normalizedToOriginal[nt]).Build())
.ToList();
// Add missing tags to the database if any
@ -67,13 +73,11 @@ public static class TagHelper
// Add the new or existing tags to the chapter
foreach (var normalizedTitle in normalizedTagsToAdd)
{
var tag = existingTagTitles[normalizedTitle];
if (existingTagsSet.Contains(normalizedTitle)) continue;
if (!existingTagsSet.Contains(normalizedTitle))
{
chapter.Tags.Add(tag);
isModified = true;
}
var tag = existingTagTitles[normalizedTitle];
chapter.Tags.Add(tag);
isModified = true;
}
// Commit changes if modifications were made to the chapter's tags