Files
Thanakarn Klangkasame 1e636aa3d5 [Add] MasterData Services.
2025-11-26 10:29:56 +07:00

58 lines
2.0 KiB
C#

using AMREZ.EOP.Abstractions.Applications.Tenancy;
using AMREZ.EOP.Abstractions.Applications.UseCases.MasterData.Subdistrict;
using AMREZ.EOP.Abstractions.Infrastructures.Repositories;
using AMREZ.EOP.Contracts.DTOs.Common;
using AMREZ.EOP.Contracts.DTOs.MasterData.Subdistrict;
using Microsoft.AspNetCore.Http;
namespace AMREZ.EOP.Application.UseCases.MasterData.Subdistricts;
public sealed class ListSubdistrictsUseCase : IListSubdistrictsUseCase
{
private readonly ISubdistrictRepository _repo;
private readonly ITenantResolver _tenantResolver;
private readonly IHttpContextAccessor _http;
public ListSubdistrictsUseCase(
ISubdistrictRepository repo,
ITenantResolver tenantResolver,
IHttpContextAccessor http)
{
_repo = repo;
_tenantResolver = tenantResolver;
_http = http;
}
public async Task<PagedResponse<SubdistrictResponse>> ExecuteAsync(SubdistrictListRequest 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<SubdistrictResponse>(page.Page, page.PageSize, page.Total, items);
}
private static SubdistrictResponse Map(Domain.Entities.MasterData.Subdistrict e) =>
new(
e.Id,
e.Scope,
e.Code,
e.Name,
e.NameI18n,
e.OverridesGlobalId,
e.IsActive,
e.IsSystem,
e.Meta,
e.Code,
e.Postcode,
e.DistrictId,
e.District.Name,
e.District.Code,
e.District.ProvinceId,
e.District.Province.Name,
e.District.Province.Code
);
}