New Scanner + People Pages (#3286)

Co-authored-by: Robbie Davis <robbie@therobbiedavis.com>
This commit is contained in:
Joe Milazzo 2024-10-23 15:11:18 -07:00 committed by GitHub
parent 1ed0eae22d
commit ba20ad4ecc
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
142 changed files with 17529 additions and 3038 deletions

View file

@ -6,6 +6,7 @@ using API.DTOs.Metadata;
using API.Entities;
using API.Extensions;
using API.Extensions.QueryExtensions;
using API.Services.Tasks.Scanner.Parser;
using AutoMapper;
using AutoMapper.QueryableExtensions;
using Microsoft.EntityFrameworkCore;
@ -24,6 +25,7 @@ public interface IGenreRepository
Task<int> GetCountAsync();
Task<GenreTagDto> GetRandomGenre();
Task<GenreTagDto> GetGenreById(int id);
Task<List<string>> GetAllGenresNotInListAsync(ICollection<string> genreNames);
}
public class GenreRepository : IGenreRepository
@ -133,4 +135,33 @@ public class GenreRepository : IGenreRepository
.ProjectTo<GenreTagDto>(_mapper.ConfigurationProvider)
.ToListAsync();
}
/// <summary>
/// Gets all genres that are not already present in the system.
/// Normalizes genres for lookup, but returns non-normalized names for creation.
/// </summary>
/// <param name="genreNames">The list of genre names (non-normalized).</param>
/// <returns>A list of genre names that do not exist in the system.</returns>
public async Task<List<string>> GetAllGenresNotInListAsync(ICollection<string> genreNames)
{
// Group the genres by their normalized names, keeping track of the original names
var normalizedToOriginalMap = genreNames
.Distinct()
.GroupBy(Parser.Normalize)
.ToDictionary(group => group.Key, group => group.First()); // Take the first original name for each normalized name
var normalizedGenreNames = normalizedToOriginalMap.Keys.ToList();
// Query the database for existing genres using the normalized names
var existingGenres = await _context.Genre
.Where(g => normalizedGenreNames.Contains(g.NormalizedTitle)) // Assuming you have a normalized field
.Select(g => g.NormalizedTitle)
.ToListAsync();
// Find the normalized genres that do not exist in the database
var missingGenres = normalizedGenreNames.Except(existingGenres).ToList();
// Return the original non-normalized genres for the missing ones
return missingGenres.Select(normalizedName => normalizedToOriginalMap[normalizedName]).ToList();
}
}