[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.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using AMREZ.EOP.Abstractions.Applications.Tenancy;
|
||||
using AMREZ.EOP.Abstractions.Applications.UseCases.MasterData.Brand;
|
||||
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AMREZ.EOP.Application.UseCases.MasterData.Brand;
|
||||
|
||||
public sealed class DeleteBrandUseCase : IDeleteBrandUseCase
|
||||
{
|
||||
private readonly IBrandRepository _repo;
|
||||
private readonly ITenantResolver _tenantResolver;
|
||||
private readonly IHttpContextAccessor _http;
|
||||
|
||||
public DeleteBrandUseCase(IBrandRepository repo, ITenantResolver tr, IHttpContextAccessor http)
|
||||
{
|
||||
_repo = repo;
|
||||
_tenantResolver = tr;
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public async Task<bool> ExecuteAsync(Guid id, CancellationToken ct = default)
|
||||
{
|
||||
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
|
||||
var tc = _tenantResolver.Resolve(http) ?? throw new InvalidOperationException("No tenant");
|
||||
var tid = Guid.Parse(tc.Id);
|
||||
|
||||
var n = await _repo.SoftDeleteAsync(id, tid, ct);
|
||||
return n > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using AMREZ.EOP.Abstractions.Applications.UseCases.MasterData.Brand;
|
||||
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
|
||||
using AMREZ.EOP.Contracts.DTOs.MasterData.Brand;
|
||||
|
||||
namespace AMREZ.EOP.Application.UseCases.MasterData.Brand;
|
||||
|
||||
public sealed class GetBrandUseCase : IGetBrandUseCase
|
||||
{
|
||||
private readonly IBrandRepository _repo;
|
||||
public GetBrandUseCase(IBrandRepository repo) => _repo = repo;
|
||||
|
||||
public async Task<BrandResponse?> ExecuteAsync(Guid id, CancellationToken ct = default)
|
||||
{
|
||||
var e = await _repo.GetAsync(id, ct);
|
||||
return e is null ? null : new BrandResponse(e.Id, e.Scope, e.Code, e.Name, e.NameI18n, e.OverridesGlobalId, e.IsActive, e.IsSystem, e.Meta);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using AMREZ.EOP.Abstractions.Applications.Tenancy;
|
||||
using AMREZ.EOP.Abstractions.Applications.UseCases.MasterData.Brand;
|
||||
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
|
||||
using AMREZ.EOP.Contracts.DTOs.Common;
|
||||
using AMREZ.EOP.Contracts.DTOs.MasterData.Brand;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace AMREZ.EOP.Application.UseCases.MasterData.Brand;
|
||||
|
||||
public sealed class ListBrandsUseCase : IListBrandsUseCase
|
||||
{
|
||||
private readonly IBrandRepository _repo;
|
||||
private readonly ITenantResolver _tenantResolver;
|
||||
private readonly IHttpContextAccessor _http;
|
||||
|
||||
public ListBrandsUseCase(IBrandRepository repo, ITenantResolver tenantResolver, IHttpContextAccessor http)
|
||||
{
|
||||
_repo = repo;
|
||||
_tenantResolver = tenantResolver;
|
||||
_http = http;
|
||||
}
|
||||
|
||||
public async Task<PagedResponse<BrandResponse>> ExecuteAsync(BrandListRequest req, CancellationToken ct = default)
|
||||
{
|
||||
var http = _http.HttpContext ?? throw new InvalidOperationException("No HttpContext");
|
||||
var tc = _tenantResolver.Resolve(http) ?? throw new InvalidOperationException("No tenant");
|
||||
var tid = Guid.Parse(tc.Id);
|
||||
|
||||
var page = await _repo.SearchEffectiveAsync(tid, req, ct);
|
||||
var items = page.Items.Select(Map).ToList();
|
||||
return new PagedResponse<BrandResponse>(page.Page, page.PageSize, page.Total, items);
|
||||
}
|
||||
|
||||
private static BrandResponse Map(Domain.Entities.MasterData.Brand e) =>
|
||||
new(e.Id, e.Scope, e.Code, e.Name, e.NameI18n, e.OverridesGlobalId, e.IsActive, e.IsSystem, e.Meta);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using System.Data;
|
||||
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;
|
||||
|
||||
namespace AMREZ.EOP.Application.UseCases.MasterData.Brand;
|
||||
|
||||
public sealed class UpdateBrandUseCase : IUpdateBrandUseCase
|
||||
{
|
||||
private readonly IBrandRepository _repo;
|
||||
private readonly IUnitOfWork _uow;
|
||||
|
||||
public UpdateBrandUseCase(IBrandRepository repo, IUnitOfWork uow) { _repo = repo; _uow = uow; }
|
||||
|
||||
public async Task<BrandResponse?> ExecuteAsync(Guid id, BrandUpdateRequest req, CancellationToken ct = default)
|
||||
{
|
||||
await _uow.BeginAsync(null, IsolationLevel.ReadCommitted, ct);
|
||||
try
|
||||
{
|
||||
var e = await _repo.GetAsync(id, ct);
|
||||
if (e is null) { await _uow.RollbackAsync(ct); return null; }
|
||||
|
||||
e.Name = req.Name.Trim();
|
||||
e.NameI18n = req.NameI18n;
|
||||
e.Meta = req.Meta;
|
||||
e.IsActive = req.IsActive;
|
||||
|
||||
await _repo.UpdateAsync(e, ct);
|
||||
await _uow.CommitAsync(ct);
|
||||
|
||||
return new BrandResponse(e.Id, e.Scope, e.Code, e.Name, e.NameI18n, e.OverridesGlobalId, e.IsActive, e.IsSystem, e.Meta);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _uow.RollbackAsync(ct);
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user