using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Leave.LeaveRequest.Cqrs; public class GetLeaveRequestByIdResponse { public string? Id { get; set; } public string? AutoNumber { get; set; } public string? EmployeeId { get; set; } public string? EmployeeName { get; set; } public string? LeaveCategoryId { get; set; } public string? LeaveType { get; set; } public DateTime StartDate { get; set; } public DateTime EndDate { get; set; } public double Days { get; set; } public string? Reason { get; set; } public string? Status { get; set; } public string? AttachmentPath { get; set; } public string? ApproverId { get; set; } public DateTime? ActionDate { get; set; } public string? RejectReason { get; set; } public DateTimeOffset? CreatedAt { get; set; } public string? CreatedBy { get; set; } public DateTimeOffset? UpdatedAt { get; set; } public string? UpdatedBy { get; set; } } public record GetLeaveRequestByIdQuery(string Id) : IRequest; public class GetLeaveRequestByIdHandler : IRequestHandler { private readonly AppDbContext _context; public GetLeaveRequestByIdHandler(AppDbContext context) => _context = context; public async Task Handle(GetLeaveRequestByIdQuery request, CancellationToken cancellationToken) { return await _context.LeaveRequest .AsNoTracking() .Include(x => x.Employee) .Include(x => x.LeaveCategory) .Where(x => x.Id == request.Id) .Select(x => new GetLeaveRequestByIdResponse { Id = x.Id, AutoNumber = x.AutoNumber, EmployeeId = x.EmployeeId, EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty, LeaveCategoryId = x.LeaveCategoryId, LeaveType = x.LeaveCategory != null ? x.LeaveCategory.Name : string.Empty, StartDate = x.StartDate, EndDate = x.EndDate, Days = x.Days, Reason = x.Reason, Status = x.Status, AttachmentPath = x.AttachmentPath, ApproverId = x.ApproverId, ActionDate = x.ActionDate, RejectReason = x.RejectReason, CreatedAt = x.CreatedAt, CreatedBy = x.CreatedBy, UpdatedAt = x.UpdatedAt, UpdatedBy = x.UpdatedBy }) .FirstOrDefaultAsync(cancellationToken); } }