#region ========================================================================= USING =====================================================================================
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Presentation.Api.IntegrationTests.Common.Setup;
///
/// Database transaction interceptor that reports the rolled back transactions to a .
///
[ExcludeFromCodeCoverage]
public sealed class TransactionProbeInterceptor : DbTransactionInterceptor
{
private readonly TransactionProbe _probe;
private readonly IHttpContextAccessor _httpContextAccessor;
///
/// Initializes a new instance of the class.
///
/// The probe that records the rolled back transactions.
/// Injected accessor used to identify the request whose transaction was rolled back.
public TransactionProbeInterceptor(TransactionProbe probe, IHttpContextAccessor httpContextAccessor)
{
_probe = probe;
_httpContextAccessor = httpContextAccessor;
}
///
/// Records that a transaction was rolled back.
///
/// The rolled back transaction.
/// The data of the transaction event.
/// Cancellation token that can be used to stop the execution.
/// A completed task.
public override Task TransactionRolledBackAsync(DbTransaction transaction, TransactionEndEventData eventData, CancellationToken cancellationToken = default)
{
RecordRolledBack();
return Task.CompletedTask;
}
///
/// Records that a transaction was rolled back synchronously.
///
/// The rolled back transaction.
/// The data of the transaction event.
public override void TransactionRolledBack(DbTransaction transaction, TransactionEndEventData eventData)
{
RecordRolledBack();
}
///
/// Reports the rolled back transaction of the current request to the probe.
///
private void RecordRolledBack()
{
string path = _httpContextAccessor.HttpContext?.Request.Path.Value ?? string.Empty;
_probe.RecordRolledBack(path);
}
}