46 lines
1.8 KiB
C#
46 lines
1.8 KiB
C#
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.AddBaseDomain;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace AMREZ.EOP.Application.UseCases.Tenancy;
|
|
|
|
public sealed class AddBaseDomainUseCase : IAddBaseDomainUseCase
|
|
{
|
|
private readonly ITenantResolver _resolver;
|
|
private readonly IHttpContextAccessor _http;
|
|
private readonly IUnitOfWork _uow;
|
|
private readonly ITenantRepository _repo;
|
|
|
|
public AddBaseDomainUseCase(ITenantResolver resolver, IHttpContextAccessor http, IUnitOfWork uow, ITenantRepository repo)
|
|
{ _resolver = resolver; _http = http; _uow = uow; _repo = repo; }
|
|
|
|
public async Task<AddBaseDomainResponse?> ExecuteAsync(AddBaseDomainRequest 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;
|
|
|
|
var baseDomain = (request.BaseDomain ?? string.Empty).Trim().TrimEnd('.').ToLowerInvariant();
|
|
if (string.IsNullOrWhiteSpace(baseDomain)) return null;
|
|
|
|
await _uow.BeginAsync(ctx, IsolationLevel.ReadCommitted, ct);
|
|
try
|
|
{
|
|
var ok = await _repo.AddBaseDomainAsync(baseDomain, ct); // repo ดึง target tenant จาก X-Tenant/context
|
|
if (!ok) { await _uow.RollbackAsync(ct); return null; }
|
|
|
|
await _uow.CommitAsync(ct);
|
|
return new AddBaseDomainResponse(baseDomain);
|
|
}
|
|
catch
|
|
{
|
|
await _uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
}
|
|
} |