using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Thirdparty.Patient.Cqrs; public class UpdatePatientContactRequest { public string? Id { get; set; } public string? Name { get; set; } public string? Title { get; set; } public string? PhoneNumber { get; set; } public string? EmailAddress { get; set; } public string? Description { get; set; } } public class UpdatePatientContactResponse { public string? Id { get; set; } public bool Success { get; set; } } public record UpdatePatientContactCommand(UpdatePatientContactRequest Data) : IRequest; public class UpdatePatientContactHandler : IRequestHandler { private readonly AppDbContext _context; public UpdatePatientContactHandler(AppDbContext context) => _context = context; public async Task Handle(UpdatePatientContactCommand request, CancellationToken cancellationToken) { var entity = await _context.PatientContact .FirstOrDefaultAsync(x => x.Id == request.Data.Id, cancellationToken); if (entity == null) { return new UpdatePatientContactResponse { Id = request.Data.Id, Success = false }; } entity.Name = request.Data.Name; entity.Title = request.Data.Title; entity.PhoneNumber = request.Data.PhoneNumber; entity.EmailAddress = request.Data.EmailAddress; entity.Description = request.Data.Description; await _context.SaveChangesAsync(cancellationToken); return new UpdatePatientContactResponse { Id = entity.Id, Success = true }; } }