using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Performance.Evaluation.Cqrs; public class GetEvaluationByIdResponse { 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? Period { get; set; } public string? FinalScore { get; set; } public string? Rating { get; set; } public string? EvaluatorId { get; set; } public string? EvaluatorCode { get; set; } public string? EvaluatorName { get; set; } public DateTime EvaluationDate { get; set; } public string? EvaluationNote { 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 GetEvaluationByIdQuery(string Id) : IRequest; public class GetEvaluationByIdHandler : IRequestHandler { private readonly AppDbContext _context; public GetEvaluationByIdHandler(AppDbContext context) => _context = context; public async Task Handle(GetEvaluationByIdQuery request, CancellationToken cancellationToken) { return await _context.Evaluation .AsNoTracking() .Include(x => x.Employee) .Include(x => x.Evaluator) .Where(x => x.Id == request.Id) .Select(x => new GetEvaluationByIdResponse { 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, Period = x.Period, FinalScore = x.FinalScore, Rating = x.Rating, EvaluatorId = x.EvaluatorId, EvaluatorCode = x.Evaluator != null ? x.Evaluator.Code : string.Empty, EvaluatorName = x.Evaluator != null ? $"{x.Evaluator.FirstName} {x.Evaluator.LastName}" : string.Empty, EvaluationDate = x.EvaluationDate, EvaluationNote = x.EvaluationNote, Status = x.Status, CreatedAt = x.CreatedAt, CreatedBy = x.CreatedBy, UpdatedAt = x.UpdatedAt, UpdatedBy = x.UpdatedBy }) .FirstOrDefaultAsync(cancellationToken); } }