Files
2026-07-21 14:08:10 +07:00

53 lines
2.0 KiB
C#

using Indotalent.ConfigBackEnd.Extensions;
using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Organization.Employee.Cqrs;
public class GetEmployeeListResponse
{
public string Id { get; set; } = string.Empty;
public string? AutoNumber { get; set; }
public string Code { get; set; } = string.Empty;
public string FullName { get; set; } = string.Empty;
public string DesignationName { get; set; } = string.Empty;
public string DepartmentName { get; set; } = string.Empty;
public string BranchName { get; set; } = string.Empty;
public string EmployeeStatus { get; set; } = string.Empty;
public string? GradeName { get; set; }
public decimal? BasicSalary { 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)
{
var data = await _context.Employee
.Include(x => x.Grade)
.Include(x => x.Branch)
.Include(x => x.Department)
.Include(x => x.Designation)
.AsNoTracking()
.Select(x => new GetEmployeeListResponse
{
Id = x.Id,
Code = x.Code,
FullName = $"{x.FirstName} {x.MiddleName} {x.LastName}".Replace(" ", " "),
DesignationName = x.Designation!.Name,
DepartmentName = x.Department!.Name,
BranchName = x.Branch!.Name,
GradeName = x.Grade != null ? x.Grade.Name : string.Empty,
BasicSalary = x.BasicSalary
})
.ToListAsync(cancellationToken);
return data;
}
}