Refactor movie matching logic to improve clarity and accuracy
ci / build-backend (push) Successful in 1m32s
ci / build-frontend (push) Successful in 56s
ci / tests (push) Successful in 1m40s
ci / sonar (push) Successful in 6m20s

Updated the MovieMatcher class to streamline the status determination for movie matches. Introduced a new method, StatusOf, to encapsulate the logic for assessing whether a movie is already in the library or if a confident match exists. This refactor enhances code readability and maintains the integrity of the movie import process. Adjusted related logic to ensure consistent handling of movie matching statuses.
This commit is contained in:
Leonid Pershin
2026-07-28 03:17:16 +03:00
parent d0bc0465e7
commit f15b2fefcd
2 changed files with 14 additions and 7 deletions
@@ -20,7 +20,7 @@ public sealed class AutoAttachMovieCommandHandler(
)
{
// Источник берём первый настроенный: выбирать его в фоне некому, а настроен обычно один.
if (resolver.AvailableKeys.FirstOrDefault() is not { } provider)
if (resolver.AvailableKeys is not [var provider, ..])
return Result.Success(false);
var asset = await dbContext.MediaAssets.FirstOrDefaultAsync(
@@ -176,16 +176,11 @@ public sealed class MovieMatcher(IAppDbContext dbContext, IMetadataProviderResol
var picked = Pick(candidates, parsed);
var existing = picked is null ? null : taken.GetValueOrDefault(picked.ExternalId);
var status =
existing is not null ? MovieMatchStatus.AlreadyInLibrary
: picked is not null ? MovieMatchStatus.Confident
: MovieMatchStatus.Uncertain;
return new MovieMatchDto(
name,
parsed.Title,
parsed.Year,
status,
StatusOf(existing, picked),
candidates,
picked?.ExternalId,
existing?.Id,
@@ -196,6 +191,18 @@ public sealed class MovieMatcher(IAppDbContext dbContext, IMetadataProviderResol
private static MovieMatchDto NothingFound(string name, string title, int? year) =>
new(name, title, year, MovieMatchStatus.NotFound, [], null, null, null);
/// <summary>
/// Что делать со строкой. Занятый кандидат важнее уверенности: второй файл того же фильма —
/// это не второй фильм, и заводить его нельзя, каким бы точным ни было совпадение.
/// </summary>
private static MovieMatchStatus StatusOf(TakenShow? existing, MetadataCandidate? picked)
{
if (existing is not null)
return MovieMatchStatus.AlreadyInLibrary;
return picked is not null ? MovieMatchStatus.Confident : MovieMatchStatus.Uncertain;
}
/// <summary>
/// Единственный кандидат, у которого название совпало дословно, а год сошёлся. Несколько таких —
/// значит выбирать не из чего: два «Мстителя» одного года различит только человек.