Files
2026-07-21 14:22:06 +07:00

58 lines
2.2 KiB
C#

using Indotalent.Infrastructure.Database;
using MediatR;
using Microsoft.EntityFrameworkCore;
namespace Indotalent.Features.Performance.Promotion.Cqrs;
public class GetPromotionByIdResponse
{
public string? Id { get; set; }
public string? AutoNumber { get; set; }
public string? EmployeeId { get; set; }
public string? EmployeeCode { get; set; }
public string? EmployeeName { get; set; }
public string? FromGrade { get; set; }
public string? ToGrade { get; set; }
public DateTime? EffectiveDate { get; set; }
public string? PromotionNote { get; set; }
public string? Status { get; set; }
public DateTimeOffset? CreatedAt { get; set; }
public string? CreatedBy { get; set; }
public DateTimeOffset? UpdatedAt { get; set; }
public string? UpdatedBy { get; set; }
}
public record GetPromotionByIdQuery(string Id) : IRequest<GetPromotionByIdResponse?>;
public class GetPromotionByIdHandler : IRequestHandler<GetPromotionByIdQuery, GetPromotionByIdResponse?>
{
private readonly AppDbContext _context;
public GetPromotionByIdHandler(AppDbContext context) => _context = context;
public async Task<GetPromotionByIdResponse?> Handle(GetPromotionByIdQuery request, CancellationToken cancellationToken)
{
return await _context.Promotion
.AsNoTracking()
.Include(x => x.Employee)
.Where(x => x.Id == request.Id)
.Select(x => new GetPromotionByIdResponse
{
Id = x.Id,
AutoNumber = x.AutoNumber,
EmployeeId = x.EmployeeId,
EmployeeCode = x.Employee != null ? x.Employee.Code : string.Empty,
EmployeeName = x.Employee != null ? $"{x.Employee.FirstName} {x.Employee.LastName}" : string.Empty,
FromGrade = x.FromGrade,
ToGrade = x.ToGrade,
EffectiveDate = x.EffectiveDate,
PromotionNote = x.PromotionNote,
Status = x.Status,
CreatedAt = x.CreatedAt,
CreatedBy = x.CreatedBy,
UpdatedAt = x.UpdatedAt,
UpdatedBy = x.UpdatedBy
})
.FirstOrDefaultAsync(cancellationToken);
}
}