public Task <bool> Handle(RegisterNewPatientCommand message, CancellationToken cancellationToken) { if (!message.IsValid()) { NotifyValidationErrors(message); return(Task.FromResult(false)); } var patient = new Patient.Builder(Guid.NewGuid()).Named(message.FullName).BornIn(message.BirthDate) .WithCpf(message.Cpf) .WithGender(message.Gender).WithPhone(message.Phone).WhichIsActive().Build(); if (_patientRepository.GetByCpf(patient.Cpf) != null) { _bus.RaiseEvent(new DomainNotification(message.MessageType, $"There is already a patient registered with this CPF ({patient.Cpf})")); return(Task.FromResult(false)); } if (!patient.IsValid()) { NotifyValidationErrors(patient.ValidationResult); return(Task.FromResult(false)); } _patientRepository.Add(patient); if (Commit()) { _bus.RaiseEvent(new PatientRegisteredEvent(patient)); } return(Task.FromResult(true)); }
public Task <bool> Handle(UpdatePatientAddressCommand message, CancellationToken cancellationToken) { if (!message.IsValid()) { NotifyValidationErrors(message); return(Task.FromResult(false)); } var existingPatient = _patientRepository.GetById(message.Id); if (existingPatient == null) { _bus.RaiseEvent(new DomainNotification(message.MessageType, "Patient not found.")); return(Task.FromResult(false)); } if (existingPatient.Id != message.Id) { _bus.RaiseEvent(new DomainNotification(message.MessageType, "Patient identification does not match, please verify.")); return(Task.FromResult(false)); } var address = new Address.Builder().InCityOf(message.City).InTheDistrict(message.District) .InTheState(message.State).WithNumber(message.Number).WithObservation(message.Observation) .WithPostalCode(message.PostalCode).WithStreet(message.Street).Build(); var patient = new Patient.Builder(existingPatient.Id).BornIn(existingPatient.BirthDate) .Named(existingPatient.FullName).ThatLivesIn(address).WithCpf(existingPatient.Cpf) .WithEmail(existingPatient.Email).WithGender(existingPatient.Gender).WithPhone(existingPatient.Phone) .WithPhoto(existingPatient.Photo).WhichIsActive().WithHeartRate(existingPatient.HeartRate).Build(); if (!patient.IsValid()) { NotifyValidationErrors(patient.ValidationResult); return(Task.FromResult(false)); } _patientRepository.Update(patient); if (Commit()) { _bus.RaiseEvent(new PatientAddressUpdatedEvent(patient)); } return(Task.FromResult(true)); }
public Task <bool> Handle(DeactivatePatientCommand message, CancellationToken cancellationToken) { if (!message.IsValid()) { NotifyValidationErrors(message); return(Task.FromResult(false)); } var existingPatient = _patientRepository.GetByCpf(message.Cpf); if (existingPatient == null) { _bus.RaiseEvent(new DomainNotification(message.MessageType, "Patient not found.")); return(Task.FromResult(false)); } if (existingPatient.Id != message.Id) { _bus.RaiseEvent(new DomainNotification(message.MessageType, "Patient identification does not match, please verify.")); return(Task.FromResult(false)); } var patient = new Patient.Builder(existingPatient.Id).BornIn(existingPatient.BirthDate) .Named(existingPatient.FullName).ThatLivesIn(existingPatient.Address).WithCpf(existingPatient.Cpf) .WithEmail(existingPatient.Email).WithGender(existingPatient.Gender).WithPhone(message.Phone) .WithPhoto(existingPatient.Photo).WhichIsInactive().WithHeartRate(existingPatient.HeartRate).Build(); if (!patient.IsValid()) { NotifyValidationErrors(patient.ValidationResult); return(Task.FromResult(false)); } _patientRepository.Update(patient); if (Commit()) { _bus.RaiseEvent(new PatientDeactivatedEvent(patient)); } return(Task.FromResult(true)); }
public void MustCreateValidActivePatientWithAllAttributes() { // Arrange var address = new Address.Builder() .InCityOf("Porto Alegre") .InTheState("Rio Grande do Sul") .WithPostalCode("91900420") .WithStreet("Some Place") .WithNumber("123") .InTheDistrict("Some District") .WithObservation("The Blue House") .Build(); // Act var patient = new Patient.Builder(Guid.NewGuid()) .Named("Johnny Bravo") .WithCpf("58554143027") .BornIn(new DateTime(1988, 09, 09)) .WithGender(Gender.Male) .WithEmail("*****@*****.**") .WhichIsActive() .ThatLivesIn(address) .WithPhoto(new byte[] { 0x00, 0x01, 0x02, 0x03 }) .WithPhone("555-5555") .Build(); // Assert Assert.Equal("Johnny Bravo", patient.FullName); Assert.Equal("58554143027", patient.Cpf); Assert.Equal(new DateTime(1988, 09, 09), patient.BirthDate); Assert.Equal(Gender.Male, patient.Gender); Assert.Equal("*****@*****.**", patient.Email); Assert.NotNull(patient.Address); Assert.True(patient.IsActive); Assert.NotNull(patient.Photo); Assert.Equal("555-5555", patient.Phone); }