using System.Data; using AMREZ.EOP.Abstractions.Applications.Tenancy; using AMREZ.EOP.Abstractions.Applications.UseCases.Tenancy; using AMREZ.EOP.Abstractions.Infrastructures.Common; using AMREZ.EOP.Abstractions.Infrastructures.Repositories; using AMREZ.EOP.Contracts.DTOs.Tenancy.ListTenants; using Microsoft.AspNetCore.Http; namespace AMREZ.EOP.Application.UseCases.Tenancy; public sealed class ListTenantsUseCase : IListTenantsUseCase { private readonly ITenantResolver _resolver; private readonly IHttpContextAccessor _http; private readonly IUnitOfWork _uow; private readonly ITenantRepository _repo; public ListTenantsUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo) { _resolver = resolver; _http = http; _uow = uow; _repo = repo; } public async Task ExecuteAsync(ListTenantsRequest request, CancellationToken ct = default) { var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext"); var ctx = _resolver.Resolve(http, hint: null); if (ctx is null) return null; await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct); try { var items = (await _repo.ListTenantsAsync(ct)) .Select(x => new TenantDto(x.TenantKey, x.Schema, x.ConnectionString, x.Mode, x.IsActive, x.UpdatedAtUtc)) .ToList(); await _uow.CommitAsync(ct); return new ListTenantsResponse(items); } catch { await _uow.RollbackAsync(ct); throw; } } }