public async Task <PatientDto> Handle(AddPatientCommand request, CancellationToken cancellationToken)
            {
                var patient = _mapper.Map <Patient> (request.PatientToAdd);

                _db.Patients.Add(patient);
                var saveSuccessful = await _db.SaveChangesAsync() > 0;

                if (saveSuccessful)
                {
                    // include marker -- to accomodate adding includes with craftsman commands, the next line must stay as `var result = await _db.Patients`. -- do not delete this comment
                    return(await _db.Patients
                           .ProjectTo <PatientDto>(_mapper.ConfigurationProvider)
                           .FirstOrDefaultAsync(p => p.PatientId == patient.PatientId));
                }
                else
                {
                    // add log
                    throw new Exception("Unable to save the new record. Please check the logs for more information.");
                }
            }
            public async Task <bool> Handle(PatchPatientCommand request, CancellationToken cancellationToken)
            {
                // add logger (and a try catch with logger so i can cap the unexpected info)........ unless this happens in my logger decorator that i am going to add?
                if (request.PatchDoc == null)
                {
                    // log error
                    throw new ApiException("Invalid patch document.");
                }

                var patientToUpdate = await _db.Patients
                                      .FirstOrDefaultAsync(p => p.PatientId == request.PatientId);

                if (patientToUpdate == null)
                {
                    // log error
                    throw new KeyNotFoundException();
                }

                var patientToPatch = _mapper.Map <PatientForUpdateDto>(patientToUpdate); // map the patient we got from the database to an updatable patient model

                request.PatchDoc.ApplyTo(patientToPatch);                                // apply patchdoc updates to the updatable patient

                var validationResults = new CustomPatchPatientValidation().Validate(patientToPatch);

                if (!validationResults.IsValid)
                {
                    throw new ValidationException(validationResults.Errors);
                }

                _mapper.Map(patientToPatch, patientToUpdate);
                var saveSuccessful = await _db.SaveChangesAsync() > 0;

                if (!saveSuccessful)
                {
                    // add log
                    throw new Exception("Unable to save the requested changes. Please check the logs for more information.");
                }

                return(true);
            }
Esempio n. 3
0
            public async Task <bool> Handle(DeleteSampleCommand request, CancellationToken cancellationToken)
            {
                // add logger (and a try catch with logger so i can cap the unexpected info)........ unless this happens in my logger decorator that i am going to add?

                var recordToDelete = await _db.Samples
                                     .FirstOrDefaultAsync(s => s.SampleId == request.SampleId);

                if (recordToDelete == null)
                {
                    // log error
                    throw new KeyNotFoundException();
                }

                _db.Samples.Remove(recordToDelete);
                var saveSuccessful = await _db.SaveChangesAsync() > 0;

                if (!saveSuccessful)
                {
                    // add log
                    throw new Exception("Unable to save the new record. Please check the logs for more information.");
                }

                return(true);
            }
Esempio n. 4
0
            public async Task <bool> Handle(UpdatePatientCommand request, CancellationToken cancellationToken)
            {
                // add logger (and a try catch with logger so i can cap the unexpected info)........ unless this happens in my logger decorator that i am going to add?

                var recordToUpdate = await _db.Patients
                                     .FirstOrDefaultAsync(p => p.PatientId == request.PatientId);

                if (recordToUpdate == null)
                {
                    // log error
                    throw new KeyNotFoundException();
                }

                _mapper.Map(request.PatientToUpdate, recordToUpdate);
                var saveSuccessful = await _db.SaveChangesAsync() > 0;

                if (!saveSuccessful)
                {
                    // add log
                    throw new Exception("Unable to save the requested changes. Please check the logs for more information.");
                }

                return(true);
            }