Files
blazor-hrm/Features/Performance/Transfer/Cqrs/GetTransferByIdHandler.cs
T
2026-07-21 14:08:10 +07:00

45 lines
1.7 KiB
C#

using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Performance.Transfer.Cqrs;
public class GetTransferByIdResponse : UpdateTransferRequest { }
public record GetTransferByIdQuery(string Id) : IRequest<GetTransferByIdResponse?>;
public class GetTransferByIdHandler : IRequestHandler<GetTransferByIdQuery, GetTransferByIdResponse?>
{
private readonly AppDbContext _context;
public GetTransferByIdHandler(AppDbContext context) => _context = context;
public async Task<GetTransferByIdResponse?> Handle(GetTransferByIdQuery request, CancellationToken cancellationToken)
{
return await _context.Transfer
.AsNoTracking()
.Include(x => x.Employee)
.Where(x => x.Id == request.Id)
.Select(x => new GetTransferByIdResponse
{
Id = x.Id,
AutoNumber = x.AutoNumber,
EmployeeId = x.EmployeeId,
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
FromBranchId = x.FromBranchId,
FromDepartmentId = x.FromDepartmentId,
FromDesignationId = x.FromDesignationId,
ToBranchId = x.ToBranchId,
ToDepartmentId = x.ToDepartmentId,
ToDesignationId = x.ToDesignationId,
TransferDate = x.TransferDate,
TransferNote = x.TransferNote,
Status = x.Status,
CreatedAt = x.CreatedAt,
CreatedBy = x.CreatedBy,
UpdatedAt = x.UpdatedAt,
UpdatedBy = x.UpdatedBy
})
.FirstOrDefaultAsync(cancellationToken);
}
}