Files
amrez-nova-eop-services-api/AMREZ.EOP.Application/UseCases/HumanResources/SetPrimaryAddressUseCase.cs
Thanakarn Klangkasame 92e614674c Init Git
2025-09-30 11:01:02 +07:00

46 lines
1.5 KiB
C#

using System.Data;
using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.HumanResources;
using AMREZ.EOP.Abstractions.Infrastructures.Common;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.HumanResources.SetPrimaryAddress;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.HumanResources;
public sealed class SetPrimaryAddressUseCase : ISetPrimaryAddressUseCase
{
private readonly ITenantResolver _resolver;
private readonly IUnitOfWork _uow;
private readonly IUserProfileRepository _hr;
private readonly IHttpContextAccessor _http;
public SetPrimaryAddressUseCase(ITenantResolver r, IUnitOfWork uow, IUserProfileRepository hr,
IHttpContextAccessor http)
{
_resolver = r;
_uow = uow;
_hr = hr;
_http = http;
}
public async Task<bool> ExecuteAsync(SetPrimaryAddressRequest request, CancellationToken ct = default)
{
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
var tenant = _resolver.Resolve(http, request);
if (tenant is null) return false;
await _uow.BeginAsync(tenant, IsolationLevel.ReadCommitted, ct);
try
{
await _hr.SetPrimaryAddressAsync(request.UserProfileId, request.AddressId, ct);
await _uow.CommitAsync(ct);
return true;
}
catch
{
await _uow.RollbackAsync(ct);
throw;
}
}
}