using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Thirdparty.Patient.Cqrs; public class PatientContactItemResponse { public string? Id { get; set; } public string? Name { get; set; } public string? AutoNumber { get; set; } public string? Title { get; set; } public string? PhoneNumber { get; set; } public string? EmailAddress { get; set; } public string? Description { get; set; } } public class GetPatientByIdResponse { public string? Id { get; set; } public string? AutoNumber { get; set; } public string? Name { get; set; } public string? Description { get; set; } public string? Street { get; set; } public string? City { get; set; } public string? State { get; set; } public string? ZipCode { get; set; } public string? Country { get; set; } public string? PhoneNumber { get; set; } public string? FaxNumber { get; set; } public string? EmailAddress { get; set; } public string? Website { get; set; } public string? WhatsApp { get; set; } public string? LinkedIn { get; set; } public string? Facebook { get; set; } public string? Instagram { get; set; } public string? TwitterX { get; set; } public string? TikTok { get; set; } public string? PatientGroupId { get; set; } public string? PatientCategoryId { get; set; } public DateTimeOffset? CreatedAt { get; set; } public string? CreatedBy { get; set; } public DateTimeOffset? UpdatedAt { get; set; } public string? UpdatedBy { get; set; } public List Contacts { get; set; } = new(); } public record GetPatientByIdQuery(string Id) : IRequest; public class GetPatientByIdHandler : IRequestHandler { private readonly AppDbContext _context; public GetPatientByIdHandler(AppDbContext context) => _context = context; public async Task Handle(GetPatientByIdQuery request, CancellationToken cancellationToken) { return await _context.Patient .AsNoTracking() .Include(x => x.PatientContactList) .Where(x => x.Id == request.Id) .Select(x => new GetPatientByIdResponse { Id = x.Id, AutoNumber = x.AutoNumber, Name = x.Name, Description = x.Description, Street = x.Street, City = x.City, State = x.State, ZipCode = x.ZipCode, Country = x.Country, PhoneNumber = x.PhoneNumber, FaxNumber = x.FaxNumber, EmailAddress = x.EmailAddress, Website = x.Website, WhatsApp = x.WhatsApp, LinkedIn = x.LinkedIn, Facebook = x.Facebook, Instagram = x.Instagram, TwitterX = x.TwitterX, TikTok = x.TikTok, PatientGroupId = x.PatientGroupId, PatientCategoryId = x.PatientCategoryId, CreatedAt = x.CreatedAt, CreatedBy = x.CreatedBy, UpdatedAt = x.UpdatedAt, UpdatedBy = x.UpdatedBy, Contacts = x.PatientContactList .OrderBy(c => c.Name) .Select(c => new PatientContactItemResponse { Id = c.Id, Name = c.Name, AutoNumber = c.AutoNumber, Title = c.Title, PhoneNumber = c.PhoneNumber, EmailAddress = c.EmailAddress, Description = c.Description }).ToList() }) .FirstOrDefaultAsync(cancellationToken); } }