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>; public class GetEmployeeListHandler : IRequestHandler> { private readonly AppDbContext _context; public GetEmployeeListHandler(AppDbContext context) => _context = context; public async Task> 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); } }