Files
blazor-cms/Features/Organization/Employee/Cqrs/GetEmployeeListHandler.cs
T
2026-07-21 13:52:43 +07:00

45 lines
1.6 KiB
C#

using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Organization.Employee.Cqrs;
public class GetEmployeeListResponse
{
public string? Id { get; set; }
public string? AutoNumber { get; set; }
public string? Name { get; set; }
public string? PhoneNumber { get; set; }
public string? EmailAddress { get; set; }
public string? EmployeeGroupName { get; set; }
public string? EmployeeCategoryName { get; set; }
}
public record GetEmployeeListQuery() : IRequest<List<GetEmployeeListResponse>>;
public class GetEmployeeListHandler : IRequestHandler<GetEmployeeListQuery, List<GetEmployeeListResponse>>
{
private readonly AppDbContext _context;
public GetEmployeeListHandler(AppDbContext context) => _context = context;
public async Task<List<GetEmployeeListResponse>> Handle(GetEmployeeListQuery request, CancellationToken cancellationToken)
{
return await _context.Employee
.AsNoTracking()
.Include(x => x.EmployeeGroup)
.Include(x => x.EmployeeCategory)
.OrderBy(x => x.Name)
.Select(x => new GetEmployeeListResponse
{
Id = x.Id,
AutoNumber = x.AutoNumber,
Name = x.Name,
PhoneNumber = x.PhoneNumber,
EmailAddress = x.EmailAddress,
EmployeeGroupName = x.EmployeeGroup != null ? x.EmployeeGroup.Name : string.Empty,
EmployeeCategoryName = x.EmployeeCategory != null ? x.EmployeeCategory.Name : string.Empty
})
.ToListAsync(cancellationToken);
}
}