Add ConfigureAwait(false) to all await expressions in Application and Infrastructure projects (111 files). This follows .NET best practices for library code to avoid potential deadlocks when consumed from synchronization contexts (legacy, unit test runners). Also fix 4 test assertions that check SaveChangesAsync — update to SaveChangesWithRetryAsync to match the handler changes from Fix 4.
120 lines
5.3 KiB
C#
120 lines
5.3 KiB
C#
using Heimdall.Application.Common;
|
|
using Heimdall.Application.DTOs;
|
|
using Heimdall.Application.Services;
|
|
using Heimdall.Domain.Entities;
|
|
using Heimdall.Infrastructure.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Heimdall.Infrastructure.Services;
|
|
|
|
public sealed class PersonService : IPersonService
|
|
{
|
|
private readonly IDbContextFactory<AppDbContext> _dbFactory;
|
|
private readonly ILogger<PersonService> _logger;
|
|
|
|
public PersonService(IDbContextFactory<AppDbContext> dbFactory, ILogger<PersonService> logger)
|
|
{
|
|
_dbFactory = dbFactory;
|
|
_logger = logger;
|
|
}
|
|
|
|
public async Task<ServiceResult<PersonResponse>> CreateAsync(CreatePersonRequest request, CancellationToken ct)
|
|
{
|
|
var person = new Person(request.DisplayName, request.Category);
|
|
person.Update(request.DisplayName, request.Notes ?? "", request.Category, request.IsWatchlisted);
|
|
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
db.Persons.Add(person);
|
|
await db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
|
|
|
_logger.LogInformation("Person created: {PersonId} ({Name})", person.Id, person.DisplayName);
|
|
return ServiceResult<PersonResponse>.Success(Map(person));
|
|
}
|
|
|
|
public async Task<ServiceResult<PersonResponse>> UpdateAsync(Guid id, UpdatePersonRequest request, CancellationToken ct)
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
var person = await db.Persons.FindAsync([id], ct).ConfigureAwait(false);
|
|
if (person is null) return ServiceResult<PersonResponse>.NotFound("Person not found");
|
|
|
|
person.Update(request.DisplayName, request.Notes, request.Category, request.IsWatchlisted);
|
|
await db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
|
|
|
return ServiceResult<PersonResponse>.Success(Map(person));
|
|
}
|
|
|
|
public async Task<ServiceResult<PersonResponse>> GetByIdAsync(Guid id, CancellationToken ct)
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
var person = await db.Persons.Include(p => p.Faces).FirstOrDefaultAsync(p => p.Id == id, ct).ConfigureAwait(false);
|
|
return person is null
|
|
? ServiceResult<PersonResponse>.NotFound("Person not found")
|
|
: ServiceResult<PersonResponse>.Success(Map(person));
|
|
}
|
|
|
|
public async Task<ServiceResult<List<PersonResponse>>> GetAllAsync(string? category, CancellationToken ct)
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
var query = db.Persons.Include(p => p.Faces).AsNoTracking();
|
|
if (!string.IsNullOrEmpty(category))
|
|
query = query.Where(p => p.Category == category);
|
|
|
|
var persons = await query.OrderBy(p => p.DisplayName).ToListAsync(ct).ConfigureAwait(false);
|
|
return ServiceResult<List<PersonResponse>>.Success(persons.Select(Map).ToList());
|
|
}
|
|
|
|
public async Task<ServiceResult> DeleteAsync(Guid id, CancellationToken ct)
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
var person = await db.Persons.FindAsync([id], ct).ConfigureAwait(false);
|
|
if (person is null) return ServiceResult.NotFound("Person not found");
|
|
|
|
db.Persons.Remove(person);
|
|
await db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
|
return ServiceResult.Success();
|
|
}
|
|
|
|
public async Task<ServiceResult<KnownFaceResponse>> RegisterFaceAsync(RegisterFaceRequest request, CancellationToken ct)
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
var person = await db.Persons.Include(p => p.Faces).FirstOrDefaultAsync(p => p.Id == request.PersonId, ct).ConfigureAwait(false);
|
|
if (person is null) return ServiceResult<KnownFaceResponse>.NotFound("Person not found");
|
|
|
|
var face = new KnownFace(request.PersonId, request.Embedding, request.ThumbnailUrl, request.SourceEventId);
|
|
person.AddFace(face);
|
|
await db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
|
|
|
return ServiceResult<KnownFaceResponse>.Success(MapFace(face));
|
|
}
|
|
|
|
public async Task<ServiceResult<List<KnownFaceResponse>>> GetFacesAsync(Guid personId, CancellationToken ct)
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
var faces = await db.KnownFaces
|
|
.AsNoTracking()
|
|
.Where(f => f.PersonId == personId)
|
|
.ToListAsync(ct);
|
|
|
|
return ServiceResult<List<KnownFaceResponse>>.Success(faces.Select(MapFace).ToList());
|
|
}
|
|
|
|
public async Task<ServiceResult> DeleteFaceAsync(Guid faceId, CancellationToken ct)
|
|
{
|
|
await using var db = await _dbFactory.CreateDbContextAsync(ct);
|
|
var face = await db.KnownFaces.FindAsync([faceId], ct).ConfigureAwait(false);
|
|
if (face is null) return ServiceResult.NotFound("Face not found");
|
|
|
|
db.KnownFaces.Remove(face);
|
|
await db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
|
return ServiceResult.Success();
|
|
}
|
|
|
|
private static PersonResponse Map(Person p) => new(
|
|
p.Id, p.DisplayName, p.Notes, p.PhotoUrl,
|
|
p.Category, p.IsWatchlisted, p.Faces.Count, p.CreatedAt);
|
|
|
|
private static KnownFaceResponse MapFace(KnownFace f) => new(
|
|
f.Id, f.PersonId, f.ThumbnailUrl, f.SourceEventId);
|
|
}
|