/** * * Internal helper methods * */ private async Task <Patient> GetPatientByPersonalDetailsAsync(AppointmentMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (string.IsNullOrWhiteSpace(message.FirstName)) { throw new ArgumentNullException(nameof(message.FirstName)); } if (string.IsNullOrWhiteSpace(message.LastName)) { throw new ArgumentNullException(nameof(message.LastName)); } var result = await _unitOfWork .PatientRepository .GetByConditionAsync(p => p.LastName.Contains(message.LastName) && p.FirstName.Contains(message.FirstName)); var patient = result.FirstOrDefault(p => p.DateOfBirth.Date == message.DateOfBirth.Date); return(patient); }
/// <summary> /// Generate a <see cref="AppointmentMessage"/>. /// </summary> /// <param name="model"></param> /// <param name="entity"></param> /// <returns></returns> private static AppointmentMessage GenerateMessage(AppointmentDTO model, Appointment entity) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (entity == null) { throw new ArgumentNullException(nameof(entity)); } var message = new AppointmentMessage { AppointmentId = entity.Id, ConsultantId = entity.ConsultantId, TimeSlotId = entity.TimeSlotId, Date = entity.Date, FirstName = model.Patient.FirstName, LastName = model.Patient.LastName, DateOfBirth = model.Patient.DateOfBirth }; return(message); }
protected override async Task <bool> Process(AppointmentMessage message) { if (message == null) { return(false); } try { var dto = new AppointmentDTO { AppointmentId = message.AppointmentId, ConsultantId = message.ConsultantId, TimeSlotId = message.TimeSlotId, Date = message.Date }; await _hubContext.Clients.All.SendAsync("Appointment", dto); } catch (Exception e) { Log.Error("An error has occurred: {@error}", e); } return(true); }
/// <summary> /// Handles an incoming <see cref="AppointmentMessage"/> by querying for the <seealso cref="Patient"/> /// entity matching the supplied personal details. On success, emits a <seealso cref="PatientMessage"/> containing the /// received AppointmentId as well as the PatientId of the found entity. /// </summary> /// <param name="message"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> /// <exception cref="Exception"></exception> public async Task HandleIncomingPatientData(AppointmentMessage message) { if (message == null || string.IsNullOrWhiteSpace(message.FirstName) || string.IsNullOrWhiteSpace(message.LastName)) { throw new ArgumentNullException(); } try { var patient = await GetPatientByPersonalDetailsAsync(message); if (patient == null) { throw new Exception($"No {typeof(Patient)} entity matching the specified criteria exists."); } _patientPublisher.PushMessageToQueue(new PatientMessage { AppointmentId = message.AppointmentId, PatientId = patient.Id }); } catch (Exception e) { Log.Error("An exception was raised: {@e}", e); throw; } }
public HttpResponseMessage Post(AppointmentMessage msg) { if (msg == null || msg.Appointment == null || String.IsNullOrEmpty(msg.Appointment.Identifier)) { return Request.CreateResponse(HttpStatusCode.BadRequest, "Require appointment or appointment properties missing"); } var cur = _repository.GetItem(msg.Appointment.Identifier); if (cur == null) { return Request.CreateResponse(HttpStatusCode.NotFound, "Response to unknown appointment"); } if (msg.State == cur.State) return Request.CreateResponse(HttpStatusCode.NotModified); try { switch (msg.State) { case State.Accepted: _repository.Accept(msg.Appointment.Identifier); break; case State.Conflict: _repository.Conflict(msg.Appointment.Identifier); break; case State.Rejected: _repository.Reject(msg.Appointment.Identifier); break; default: return Request.CreateResponse(HttpStatusCode.BadRequest, "Message state is invalid"); } } catch (ArgumentException) { return Request.CreateResponse(HttpStatusCode.InternalServerError); } return Request.CreateResponse(HttpStatusCode.OK); }
public async Task TestHandleIncomingPatientDataInvalidArgument() { // Arrange AppointmentMessage testMessage = null; var service = new Services.PatientService(null, null, null); // Act async Task TestAction() => await service.HandleIncomingPatientData(testMessage); // Assert await Assert.ThrowsAsync <ArgumentNullException>(TestAction); }
// GET: AppointmentMessages/Details/5 public ActionResult Details(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AppointmentMessage appointmentMessage = _appointmentService.GetById(id.Value); if (appointmentMessage == null) { return(HttpNotFound()); } return(View(appointmentMessage)); }
public async Task TestHandleIncomingPatientData() { // Arrange var testMessage = new AppointmentMessage { FirstName = "Test", LastName = "McTest", DateOfBirth = new DateTime(2000, 1, 1) }; var testPatient = new Patient { FirstName = testMessage.FirstName, LastName = testMessage.LastName, DateOfBirth = testMessage.DateOfBirth }; var mockUnitOfWork = new Mock <IUnitOfWork>(); mockUnitOfWork .Setup(x => x.PatientRepository.GetByConditionAsync( It.IsAny <Expression <Func <Patient, bool> > >(), It.IsAny <bool>())) .ReturnsAsync(new List <Patient> { testPatient }) .Verifiable(); var mockPublisher = new Mock <IPatientPublisher>(); mockPublisher .Setup(x => x.PushMessageToQueue(It.IsAny <PatientMessage>())) .Verifiable(); var service = new Services.PatientService(mockUnitOfWork.Object, mockPublisher.Object, null); // Act await service.HandleIncomingPatientData(testMessage); // Assert mockUnitOfWork .Verify(x => x.PatientRepository.GetByConditionAsync( It.IsAny <Expression <Func <Patient, bool> > >(), It.IsAny <bool>()), Times.Once); mockPublisher .Verify(x => x.PushMessageToQueue(It.IsAny <PatientMessage>()), Times.Once); }
public async Task TestHandleIncomingPatientDataLastNameNull() { // Arrange var testMessage = new AppointmentMessage { FirstName = "Test", LastName = null }; var service = new Services.PatientService(null, null, null); // Act async Task TestAction() => await service.HandleIncomingPatientData(testMessage); // Assert await Assert.ThrowsAsync <ArgumentNullException>(TestAction); }
public async Task TestHandleIncomingDataPatientNull() { // Arrange var testMessage = new AppointmentMessage { FirstName = "Test", LastName = "McTest", DateOfBirth = new DateTime(2000, 1, 1) }; var mockUnitOfWork = new Mock <IUnitOfWork>(); mockUnitOfWork .Setup(x => x.PatientRepository.GetByConditionAsync( It.IsAny <Expression <Func <Patient, bool> > >(), It.IsAny <bool>())) .ReturnsAsync(new List <Patient>()) .Verifiable(); var mockPublisher = new Mock <IPatientPublisher>(); mockPublisher .Setup(x => x.PushMessageToQueue(It.IsAny <PatientMessage>())) .Verifiable(); var service = new Services.PatientService(mockUnitOfWork.Object, mockPublisher.Object, null); // Act async Task TestAction() => await service.HandleIncomingPatientData(testMessage); // Assert var ex = await Assert.ThrowsAnyAsync <Exception>(TestAction); Assert.Equal($"No {typeof(Patient)} entity matching the specified criteria exists.", ex.Message); mockUnitOfWork .Verify(x => x.PatientRepository.GetByConditionAsync( It.IsAny <Expression <Func <Patient, bool> > >(), It.IsAny <bool>()), Times.Once); mockPublisher .Verify(x => x.PushMessageToQueue(It.IsAny <PatientMessage>()), Times.Never); }
public bool PushMessageToQueue(AppointmentMessage message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } try { _bus.Publish <AppointmentMessage>(message); } catch (Exception e) { Console.WriteLine("An error occurred while attempting to emit an event: {@ex}", e); return(false); } return(true); }
// GET: AppointmentMessages/Edit/5 public ActionResult Edit(Guid?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } AppointmentMessage appointmentMessage = _appointmentService.GetById(id.Value); PatientService patientService = new PatientService(); ViewBag.IdPatient = new SelectList(patientService.GetList(), "Id", "FirstName", appointmentMessage?.IdPatient); ViewBag.IdAppointmentType = new SelectList(AppointmentTypeService.GetList(), "Id", "Name", appointmentMessage?.IdAppointmentType); if (appointmentMessage == null) { return(HttpNotFound()); } return(View(appointmentMessage)); }
public ActionResult Edit([Bind(Include = "Id,IdPatient,IdAppointmentType,Date,State")] AppointmentMessage appointmentMessage) { if (ModelState.IsValid) { AppointmentDuplicatesService appointmentDuplicatesService = new AppointmentDuplicatesService(); appointmentDuplicatesService.AppointmentDate = appointmentMessage.Date; appointmentDuplicatesService.IdPatient = appointmentMessage.IdPatient; appointmentDuplicatesService.IdAppointment = appointmentMessage.Id; if (!appointmentDuplicatesService.Exist()) { _appointmentService.Save(appointmentMessage); return(RedirectToAction("Index")); } else { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Ya existe una cita para la fecha seleccionada.")); } } return(View(appointmentMessage)); }
public void SaveAppointmentTest() { PatientFilterService patientFilterService = new PatientFilterService(); patientFilterService.Identification = "INT123"; List <PatientMessage> listPatient = patientFilterService.GetList(); if (listPatient?.Count > 0) { AppointmentMessage appointmentMessage = new AppointmentMessage { Date = DateTime.Now, Id = Guid.Empty, IdAppointmentType = (int)AppointmentType.General, IdPatient = listPatient[0].Id }; AppointmentService appointmentService = new AppointmentService(); appointmentService.Save(appointmentMessage); } }
protected override async Task <bool> Process(AppointmentMessage message) { if (message == null) { return(false); } try { using (var scope = _services.CreateScope()) using (var service = scope.ServiceProvider.GetRequiredService <IPatientService>()) { await service.HandleIncomingPatientData(message); } } catch (Exception e) { Log.Error("An error has occurred: {@error}", e); } return(true); }
public HttpResponseMessage Post(AppointmentMessage msg) { return Request.CreateResponse(HttpStatusCode.OK); }
protected abstract Task <bool> Process(AppointmentMessage message);