Init Git
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
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 Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AMREZ.EOP.Application.UseCases.Tenancy;
|
||||
|
||||
public sealed class DeleteTenantUseCase : IDeleteTenantUseCase
|
||||
{
|
||||
private readonly ITenantResolver _resolver;
|
||||
private readonly IHttpContextAccessor _http;
|
||||
private readonly IUnitOfWork _uow;
|
||||
private readonly ITenantRepository _repo;
|
||||
|
||||
public DeleteTenantUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
|
||||
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
|
||||
|
||||
public async Task<bool> ExecuteAsync(string tenantKey, CancellationToken ct = default)
|
||||
{
|
||||
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
|
||||
var key = (tenantKey ?? string.Empty).Trim().ToLowerInvariant();
|
||||
if (string.IsNullOrWhiteSpace(key)) return false;
|
||||
|
||||
var ctx = _resolver.Resolve(http, hint: null);
|
||||
if (ctx is null) return false;
|
||||
|
||||
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
|
||||
try
|
||||
{
|
||||
var ok = await _repo.DeleteAsync(key, ct);
|
||||
if (!ok) { await _uow.RollbackAsync(ct); return false; }
|
||||
await _uow.CommitAsync(ct);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _uow.RollbackAsync(ct);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user