60 lines
2.3 KiB
C#
60 lines
2.3 KiB
C#
using System.Data;
|
|
using AMREZ.EOP.Abstractions.Applications.Tenancy;
|
|
using AMREZ.EOP.Abstractions.Applications.UseCases.MasterData.Brand;
|
|
using AMREZ.EOP.Abstractions.Infrastructures.Common;
|
|
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
|
|
using AMREZ.EOP.Contracts.DTOs.MasterData.Brand;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace AMREZ.EOP.Application.UseCases.MasterData.Brand;
|
|
|
|
public sealed class CreateBrandUseCase : ICreateBrandUseCase
|
|
{
|
|
private readonly IBrandRepository _repo;
|
|
private readonly IUnitOfWork _uow;
|
|
private readonly ITenantResolver _tenantResolver;
|
|
private readonly IHttpContextAccessor _http;
|
|
|
|
public CreateBrandUseCase(IBrandRepository repo, IUnitOfWork uow, ITenantResolver tr, IHttpContextAccessor http)
|
|
{
|
|
_repo = repo; _uow = uow; _tenantResolver = tr; _http = http;
|
|
}
|
|
|
|
public async Task<BrandResponse> ExecuteAsync(BrandCreateRequest req, CancellationToken ct = default)
|
|
{
|
|
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
|
|
var tc = _tenantResolver.Resolve(http) ?? throw new InvalidOperationException("No tenant");
|
|
|
|
await _uow.BeginAsync(tc, IsolationLevel.ReadCommitted, ct);
|
|
try
|
|
{
|
|
var tid = Guid.Parse(tc.Id);
|
|
|
|
if (req.Scope == "tenant" && await _repo.CodeExistsAsync(tid, req.Code, ct))
|
|
throw new InvalidOperationException($"Brand code '{req.Code}' already exists.");
|
|
|
|
var entity = new Domain.Entities.MasterData.Brand
|
|
{
|
|
TenantId = tid,
|
|
Scope = string.IsNullOrWhiteSpace(req.Scope) ? "tenant" : req.Scope,
|
|
Code = req.Code.Trim(),
|
|
Name = req.Name.Trim(),
|
|
NameI18n = req.NameI18n,
|
|
Meta = req.Meta,
|
|
IsActive = req.IsActive,
|
|
IsSystem = false,
|
|
OverridesGlobalId = req.OverridesGlobalId
|
|
};
|
|
|
|
await _repo.AddAsync(entity, ct);
|
|
await _uow.CommitAsync(ct);
|
|
|
|
return new BrandResponse(entity.Id, entity.Scope, entity.Code, entity.Name, entity.NameI18n, entity.OverridesGlobalId, entity.IsActive, entity.IsSystem, entity.Meta);
|
|
}
|
|
catch
|
|
{
|
|
await _uow.RollbackAsync(ct);
|
|
throw;
|
|
}
|
|
}
|
|
} |