public async Task <bool> Handle(PatientForPatchCommand patchCommand, CancellationToken cancellationToken) { if (patchCommand.PatchDoc == null) { // log error throw new ApiException("Invalid patch document."); } var patientToUpdate = await _db.Patients .FirstOrDefaultAsync(p => p.PatientId == patchCommand.PatientId, cancellationToken); 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 patchCommand.PatchDoc.ApplyTo(patientToPatch); // apply patchdoc updates to the updatable patient var validationResults = new CustomPatchPatientValidation().Validate(patientToPatch); if (!validationResults.IsValid) { throw new Application.Exceptions.ValidationException(validationResults.Errors); } _mapper.Map(patientToPatch, patientToUpdate); // apply updates from the updatable patient to the db entity so we can apply the updates to the database return(await _db.SaveChangesAsync(cancellationToken) > 0); }
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); }