Colorscape Love (#3326)

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
This commit is contained in:
Joe Milazzo 2024-10-31 18:44:03 -05:00 committed by GitHub
parent b44f89d1e8
commit a847468a6c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
42 changed files with 1009 additions and 429 deletions

View file

@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using API.DTOs.CoverDb;
using API.Entities;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace API.Data.Repositories;
#nullable enable
/// <summary>
/// This is a manual repository, not a DB repo
/// </summary>
public class CoverDbRepository
{
private readonly List<CoverDbAuthor> _authors;
public CoverDbRepository(string filePath)
{
var deserializer = new DeserializerBuilder()
.WithNamingConvention(CamelCaseNamingConvention.Instance)
.Build();
// Read and deserialize YAML file
var yamlContent = File.ReadAllText(filePath);
var peopleData = deserializer.Deserialize<CoverDbPeople>(yamlContent);
_authors = peopleData.People;
}
public CoverDbAuthor? FindAuthorByNameOrAlias(string name)
{
return _authors.Find(author =>
author.Name.Equals(name, StringComparison.OrdinalIgnoreCase) ||
author.Aliases.Contains(name, StringComparer.OrdinalIgnoreCase));
}
public CoverDbAuthor? FindBestAuthorMatch(Person person)
{
var aniListId = person.AniListId > 0 ? $"{person.AniListId}" : string.Empty;
var highestScore = 0;
CoverDbAuthor? bestMatch = null;
foreach (var author in _authors)
{
var score = 0;
// Check metadata IDs and add points if they match
if (!string.IsNullOrEmpty(author.Ids.AmazonId) && author.Ids.AmazonId == person.Asin)
{
score += 10;
}
if (!string.IsNullOrEmpty(author.Ids.AnilistId) && author.Ids.AnilistId == aniListId)
{
score += 10;
}
if (!string.IsNullOrEmpty(author.Ids.HardcoverId) && author.Ids.HardcoverId == person.HardcoverId)
{
score += 10;
}
// Check for exact name match
if (author.Name.Equals(person.Name, StringComparison.OrdinalIgnoreCase))
{
score += 7;
}
// Check for alias match
if (author.Aliases.Contains(person.Name, StringComparer.OrdinalIgnoreCase))
{
score += 5;
}
// Update the best match if current score is higher
if (score <= highestScore) continue;
highestScore = score;
bestMatch = author;
}
return bestMatch;
}
}

View file

@ -38,6 +38,7 @@ public interface IPersonRepository
Task<Person?> GetPersonById(int personId);
Task<PersonDto?> GetPersonDtoByName(string name, int userId);
Task<Person> GetPersonByName(string name);
Task<bool> IsNameUnique(string name);
Task<IEnumerable<SeriesDto>> GetSeriesKnownFor(int personId);
Task<IEnumerable<StandaloneChapterDto>> GetChaptersForPersonByRole(int personId, int userId, PersonRole role);
@ -211,6 +212,11 @@ public class PersonRepository : IPersonRepository
return await _context.Person.FirstOrDefaultAsync(p => p.NormalizedName == name.ToNormalized());
}
public async Task<bool> IsNameUnique(string name)
{
return !(await _context.Person.AnyAsync(p => p.Name == name));
}
public async Task<IEnumerable<SeriesDto>> GetSeriesKnownFor(int personId)
{
return await _context.Person

View file

@ -248,6 +248,7 @@ public class ReadingListRepository : IReadingListRepository
ChapterTitleName = chapter.TitleName,
FileSize = chapter.Files.Sum(f => f.Bytes),
chapter.Summary,
chapter.IsSpecial
})
.Join(_context.Volume, s => s.ReadingListItem.VolumeId, volume => volume.Id, (data, volume) => new
@ -259,6 +260,7 @@ public class ReadingListRepository : IReadingListRepository
data.ChapterTitleName,
data.FileSize,
data.Summary,
data.IsSpecial,
VolumeId = volume.Id,
VolumeNumber = volume.Name,
})
@ -277,6 +279,7 @@ public class ReadingListRepository : IReadingListRepository
data.ChapterTitleName,
data.FileSize,
data.Summary,
data.IsSpecial,
LibraryName = _context.Library.Where(l => l.Id == s.LibraryId).Select(l => l.Name).Single(),
LibraryType = _context.Library.Where(l => l.Id == s.LibraryId).Select(l => l.Type).Single()
})
@ -299,7 +302,8 @@ public class ReadingListRepository : IReadingListRepository
ChapterTitleName = data.ChapterTitleName,
LibraryName = data.LibraryName,
FileSize = data.FileSize,
Summary = data.Summary
Summary = data.Summary,
IsSpecial = data.IsSpecial
})
.Where(o => userLibraries.Contains(o.LibraryId))
.OrderBy(rli => rli.Order)