[Add] MasterData Services.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
using System.Data;
|
||||
using AMREZ.EOP.Abstractions.Applications.Tenancy;
|
||||
using AMREZ.EOP.Abstractions.Applications.UseCases.MasterData.Allergen;
|
||||
using AMREZ.EOP.Abstractions.Infrastructures.Common;
|
||||
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
|
||||
using AMREZ.EOP.Contracts.DTOs.MasterData.Allergen;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AMREZ.EOP.Application.UseCases.MasterData.Allergen;
|
||||
|
||||
public sealed class CreateAllergenUseCase : ICreateAllergenUseCase
|
||||
{
|
||||
private readonly IAllergenRepository _repo;
|
||||
private readonly IUnitOfWork _uow;
|
||||
private readonly ITenantResolver _tenantResolver;
|
||||
private readonly IHttpContextAccessor _http;
|
||||
|
||||
public CreateAllergenUseCase(IAllergenRepository repo, IUnitOfWork uow, ITenantResolver tr, IHttpContextAccessor http)
|
||||
{
|
||||
_repo = repo; _uow = uow; _tenantResolver = tr; _http = http;
|
||||
}
|
||||
|
||||
public async Task<AllergenResponse> ExecuteAsync(AllergenCreateRequest 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.Allergen()
|
||||
{
|
||||
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 AllergenResponse(entity.Id, entity.Scope, entity.Code, entity.Name, entity.NameI18n, entity.OverridesGlobalId, entity.IsActive, entity.IsSystem, entity.Meta);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _uow.RollbackAsync(ct);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user