using Indotalent.ConfigBackEnd.Exceptions; using Indotalent.ConfigBackEnd.Extensions; using Indotalent.Infrastructure.Database; using MediatR; using Microsoft.EntityFrameworkCore; namespace Indotalent.Features.Thirdparty.PatientContact.Cqrs; public class CreatePatientContactRequest { 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 string? PatientId { get; set; } } public class CreatePatientContactResponse { public string? Id { get; set; } public string? Name { get; set; } } public record CreatePatientContactCommand(CreatePatientContactRequest Data) : IRequest; public class CreatePatientContactHandler : IRequestHandler { private readonly AppDbContext _context; public CreatePatientContactHandler(AppDbContext context) => _context = context; public async Task Handle(CreatePatientContactCommand request, CancellationToken cancellationToken) { var entityName = nameof(Data.Entities.PatientContact); var autoNo = await _context.GenerateAutoNumberAsync( entityName: entityName, prefixTemplate: $"{entityName.ToShortNameConsonant(3)}/{{Year}}/", ct: cancellationToken ); var entity = new Data.Entities.PatientContact { AutoNumber = autoNo, Name = request.Data.Name, Title = request.Data.Title, PhoneNumber = request.Data.PhoneNumber, EmailAddress = request.Data.EmailAddress, Description = request.Data.Description, PatientId = request.Data.PatientId }; _context.PatientContact.Add(entity); await _context.SaveChangesAsync(cancellationToken); return new CreatePatientContactResponse { Id = entity.Id, Name = entity.Name }; } }