public async Task <CohortGroupDetailDto> Handle(AddCohortGroupCommand message, CancellationToken cancellationToken) { var conditionFromRepo = await _conditionRepository.GetAsync(c => c.Description == message.ConditionName); if (conditionFromRepo == null) { throw new KeyNotFoundException("Unable to locate condition"); } if (_cohortGroupRepository.Exists(cg => cg.CohortName == message.CohortName || cg.CohortCode == message.CohortCode)) { throw new DomainException("Cohort group with same name already exists"); } var newCohortGroup = new CohortGroup(message.CohortName, message.CohortCode, conditionFromRepo, message.StartDate, message.FinishDate); await _cohortGroupRepository.SaveAsync(newCohortGroup); _logger.LogInformation($"----- Cohort group {message.CohortName} created"); var mappedCohortGroup = _mapper.Map <CohortGroupDetailDto>(newCohortGroup); return(CreateLinks(mappedCohortGroup)); }
public async Task <bool> Handle(ChangeProductDetailsCommand message, CancellationToken cancellationToken) { var conceptFromRepo = await _conceptRepository.GetAsync(c => c.ConceptName + "; " + c.Strength + " (" + c.MedicationForm.Description + ")" == message.ConceptName, new string[] { "Products" }); if (conceptFromRepo == null) { throw new KeyNotFoundException($"Unable to locate concept {message.ConceptName}"); } if (_productRepository.Exists(p => p.ConceptId == conceptFromRepo.Id && p.ProductName == message.ProductName && p.Id != message.ProductId)) { throw new DomainException("Product with same name annd concept already exists"); } conceptFromRepo.ChangeProductDetails(message.ProductId, message.ProductName, message.Manufacturer, message.Description); if (message.Active) { conceptFromRepo.MarkProductAsActive(message.ProductId); } else { conceptFromRepo.MarkProductAsInActive(message.ProductId); } _conceptRepository.Update(conceptFromRepo); _logger.LogInformation($"----- Product {message.ProductId} details updated"); return(await _unitOfWork.CompleteAsync()); }
public async Task <ConceptIdentifierDto> Handle(AddConceptCommand message, CancellationToken cancellationToken) { var medicationFormFromRepo = await _medicationFormRepository.GetAsync(mf => mf.Description == message.MedicationForm); if (medicationFormFromRepo == null) { throw new KeyNotFoundException("Unable to locate medication form"); } if (_conceptRepository.Exists(c => c.ConceptName == message.ConceptName && c.MedicationForm.Id == medicationFormFromRepo.Id && c.Strength == message.Strength)) { throw new DomainException("Concept with same name, strength and form already exists"); } var newConcept = new Concept(message.ConceptName, message.Strength, medicationFormFromRepo); await _conceptRepository.SaveAsync(newConcept); _logger.LogInformation($"----- Concept {message.ConceptName} created"); var mappedConcept = _mapper.Map <ConceptIdentifierDto>(newConcept); CreateLinks(mappedConcept); return(mappedConcept); }
public async Task <bool> Handle(DeleteUserCommand message, CancellationToken cancellationToken) { var userFromRepo = await _userRepository.GetAsync(u => u.Id == message.UserId, new string[] { "Facilities" }); if (userFromRepo == null) { throw new KeyNotFoundException("Unable to locate user"); } if (_auditLogRepository.Exists(a => a.User.Id == message.UserId)) { throw new DomainException("Unable to delete as item is in use"); } var userFacilities = await _userFacilityRepository.ListAsync(c => c.User.Id == message.UserId); userFacilities.ToList().ForEach(userFacility => _userFacilityRepository.Delete(userFacility)); _userRepository.Delete(userFromRepo); await _unitOfWork.CompleteAsync(); _logger.LogInformation($"----- User {userFromRepo.Id} deleted"); return(true); }
public async Task <bool> Handle(ChangeCohortGroupDetailsCommand message, CancellationToken cancellationToken) { var cohortGroupFromRepo = await _cohortGroupRepository.GetAsync(cg => cg.Id == message.Id); if (cohortGroupFromRepo == null) { throw new KeyNotFoundException("Unable to locate cohort group"); } var conditionFromRepo = await _conditionRepository.GetAsync(c => c.Description == message.ConditionName); if (conditionFromRepo == null) { throw new KeyNotFoundException("Unable to locate condition"); } if (_cohortGroupRepository.Exists(l => (l.CohortName == message.CohortName || l.CohortCode == message.CohortCode) && l.Id != message.Id)) { throw new DomainException("Item with same name already exists"); } cohortGroupFromRepo.ChangeDetails(message.CohortName, message.CohortCode, conditionFromRepo, message.StartDate, message.FinishDate); _cohortGroupRepository.Update(cohortGroupFromRepo); _logger.LogInformation($"----- Cohort group {message.CohortName} details updated"); return(await _unitOfWork.CompleteAsync()); }
public async Task <ProductIdentifierDto> Handle(AddProductCommand message, CancellationToken cancellationToken) { var conceptFromRepo = await _conceptRepository.GetAsync(c => c.ConceptName + "; " + c.Strength + " (" + c.MedicationForm.Description + ")" == message.ConceptName); if (conceptFromRepo == null) { throw new KeyNotFoundException($"Unable to locate concept {message.ConceptName}"); } if (_productRepository.Exists(p => p.ConceptId == conceptFromRepo.Id && p.ProductName == message.ProductName)) { throw new DomainException("Product with same name annd concept already exists"); } var newProduct = conceptFromRepo.AddProduct(message.ProductName, message.Manufacturer, message.Description); _conceptRepository.Update(conceptFromRepo); _logger.LogInformation($"----- Product {message.ProductName} created"); await _unitOfWork.CompleteAsync(); var mappedProduct = _mapper.Map <ProductIdentifierDto>(newProduct); CreateLinks(mappedProduct); return(mappedProduct); }
public async Task <bool> Handle(ChangeFacilityDetailsCommand message, CancellationToken cancellationToken) { var facilityFromRepo = await _facilityRepository.GetAsync(f => f.Id == message.Id); if (facilityFromRepo == null) { throw new KeyNotFoundException("Unable to locate facility"); } var facilityTypeFromRepo = await _facilityTypeRepository.GetAsync(c => c.Description == message.FacilityType); if (facilityTypeFromRepo == null) { throw new KeyNotFoundException("Unable to locate facility type"); } OrgUnit orgUnitFromRepo = null; if (message.OrgUnitId.HasValue) { if (message.OrgUnitId > 0) { orgUnitFromRepo = await _orgUnitRepository.GetAsync(message.OrgUnitId); if (orgUnitFromRepo == null) { throw new KeyNotFoundException($"Unable to locate organisation unit {message.OrgUnitId}"); } } } if (_facilityRepository.Exists(l => (l.FacilityName == message.FacilityName || l.FacilityCode == message.FacilityCode) && l.Id != message.Id)) { throw new DomainException("Item with same name already exists"); } facilityFromRepo.ChangeDetails(message.FacilityName, message.FacilityCode, facilityTypeFromRepo, message.TelNumber, message.MobileNumber, message.FaxNumber, orgUnitFromRepo); _facilityRepository.Update(facilityFromRepo); _logger.LogInformation($"----- Facility {message.FacilityName} details updated"); return(await _unitOfWork.CompleteAsync()); }
public async Task <bool> Handle(DeleteCohortGroupCommand message, CancellationToken cancellationToken) { var cohortGroupFromRepo = await _cohortGroupRepository.GetAsync(cg => cg.Id == message.Id); if (cohortGroupFromRepo == null) { throw new KeyNotFoundException("Unable to locate cohort group"); } if (_cohortGroupEnrolmentRepository.Exists(cge => cge.CohortGroup.Id == message.Id)) { throw new DomainException("Unable to delete the Cohort Group as it is currently in use"); } _cohortGroupRepository.Delete(cohortGroupFromRepo); _logger.LogInformation($"----- Cohort group {message.Id} deleted"); return(await _unitOfWork.CompleteAsync()); }
public async Task <bool> Handle(ChangeAppointmentDetailsCommand message, CancellationToken cancellationToken) { var appointmentFromRepo = await _appointmentRepository.GetAsync(a => a.PatientId == message.PatientId && a.Id == message.AppointmentId); if (appointmentFromRepo == null) { throw new KeyNotFoundException("Unable to locate appointment"); } if (_appointmentRepository.Exists(a => a.PatientId == message.PatientId && a.AppointmentDate == message.AppointmentDate && a.Id != message.AppointmentId && !a.Archived)) { throw new DomainException("Patient already has an appointment for this date"); } appointmentFromRepo.ChangeDetails(message.AppointmentDate, message.Reason, message.Cancelled, message.CancellationReason); _appointmentRepository.Update(appointmentFromRepo); _logger.LogInformation($"----- Appointment {appointmentFromRepo.Id} details updated"); return(await _unitOfWork.CompleteAsync()); }
public async Task <FacilityDetailDto> Handle(AddFacilityCommand message, CancellationToken cancellationToken) { var facilityTypeFromRepo = await _facilityTypeRepository.GetAsync(c => c.Description == message.FacilityType); if (facilityTypeFromRepo == null) { throw new KeyNotFoundException("Unable to locate facility type"); } if (_facilityRepository.Exists(f => f.FacilityName == message.FacilityName || f.FacilityCode == message.FacilityCode)) { throw new DomainException("Facility with same name or code already exists"); } OrgUnit orgUnitFromRepo = null; if (message.OrgUnitId.HasValue) { if (message.OrgUnitId > 0) { orgUnitFromRepo = await _orgUnitRepository.GetAsync(message.OrgUnitId); if (orgUnitFromRepo == null) { throw new KeyNotFoundException($"Unable to locate organisation unit {message.OrgUnitId}"); } } } var newFacility = new Facility(message.FacilityName, message.FacilityCode, facilityTypeFromRepo, message.TelNumber, message.MobileNumber, message.FaxNumber, orgUnitFromRepo); await _facilityRepository.SaveAsync(newFacility); _logger.LogInformation($"----- Facility {message.FacilityName} created"); var mappedFacility = _mapper.Map <FacilityDetailDto>(newFacility); return(CreateLinks(mappedFacility)); }
public async Task <UserIdentifierDto> Handle(AddUserCommand message, CancellationToken cancellationToken) { if (_userRepository.Exists(u => u.UserName == message.UserName)) { throw new DomainException("User with same user name already exists"); } var identityId = await CreateIdentity(message); var facilities = await PrepareFacilityAccess(message.Facilities); var newUser = new User(message.FirstName, message.LastName, message.UserName, message.Email, identityId, facilities); await _userRepository.SaveAsync(newUser); _logger.LogInformation($"----- User {message.UserName} created"); var mappedUser = _mapper.Map <UserIdentifierDto>(newUser); CreateLinks(mappedUser); return(mappedUser); }
public async Task <bool> Handle(ChangeConceptDetailsCommand message, CancellationToken cancellationToken) { var conceptFromRepo = await _conceptRepository.GetAsync(c => c.Id == message.Id); if (conceptFromRepo == null) { throw new KeyNotFoundException("Unable to locate concept"); } var medicationFormFromRepo = await _medicationFormRepository.GetAsync(mf => mf.Description == message.MedicationForm); if (medicationFormFromRepo == null) { throw new KeyNotFoundException("Unable to locate medication form"); } if (_conceptRepository.Exists(c => c.ConceptName == message.ConceptName && c.Strength == message.Strength && c.MedicationForm.Id == medicationFormFromRepo.Id && c.Id != message.Id)) { throw new DomainException("Item with same name already exists"); } conceptFromRepo.ChangeDetails(message.ConceptName, message.Strength, medicationFormFromRepo); if (message.Active) { conceptFromRepo.MarkAsActive(); } else { conceptFromRepo.MarkAsInActive(); } _conceptRepository.Update(conceptFromRepo); _logger.LogInformation($"----- Concept {message.ConceptName} details updated"); return(await _unitOfWork.CompleteAsync()); }
public async Task <bool> Handle(ChangeUserDetailCommand message, CancellationToken cancellationToken) { var userFromRepo = await _userRepository.GetAsync(u => u.Id == message.UserId, new string[] { "" }); if (userFromRepo == null) { throw new KeyNotFoundException("Unable to locate user"); } if (_userRepository.Exists(u => u.UserName == message.UserName && u.Id != message.UserId)) { throw new DomainException("User with same user name already exists"); } userFromRepo.ChangeUserDetails(message.FirstName, message.LastName, message.UserName, message.Email, message.Active, message.AllowDatasetDownload); _userRepository.Update(userFromRepo); await _unitOfWork.CompleteAsync(); _logger.LogInformation($"----- User {userFromRepo.Id} details updated"); return(true); }