public async Task <EmergencyContactResponse> UpdateAsync(int id, EmergencyContact emergencyContact) { var existingEmergencyContact = await _emergencyContactRepository.FindByIdAsync(id); if (existingEmergencyContact == null) { return(new EmergencyContactResponse("EmergencyContact not found")); } var bookingExists = await _emergencyContactRepository.BookingExists(emergencyContact.BookingID); if (!bookingExists) { return(new EmergencyContactResponse($"Booking ID:{ emergencyContact.BookingID } does not exist")); } existingEmergencyContact.BookingID = emergencyContact.BookingID; existingEmergencyContact.Name = emergencyContact.Name; existingEmergencyContact.LastName = emergencyContact.LastName; existingEmergencyContact.PhoneNumber = emergencyContact.PhoneNumber; try { _emergencyContactRepository.Update(existingEmergencyContact); await _unitOfWork.CompleteAsync(); return(new EmergencyContactResponse(existingEmergencyContact)); } catch (Exception e) { //TODO - log the exception return(new EmergencyContactResponse($"An error ocurred while updating the EmergencyContact: " + $"{ e.Message } { e.InnerException?.Message }")); } }
public ActionResult Update(EmergencyContactModel vm) { ApiResult <EmergencyContact> apiResult; if (ModelState.IsValid) { if (vm.Id > 0) { apiResult = TryExecute(() => { var emergencyContact = new EmergencyContact { Name = vm.Name, Relationship = vm.Relationship, Phone = vm.Phone, EmployeeId = vm.EmployeeId, Mobile = vm.Mobile, WorkPhone = vm.WorkPhone, Address = vm.Address, Email = vm.Email, Id = vm.Id, }; _emergencyContactRepository.Update(emergencyContact); _unitOfWork.Commit(); return(emergencyContact); }, "Emergency Contact updated sucessfully"); } else { apiResult = TryExecute(() => { var emergencyContact = new EmergencyContact { Name = vm.Name, Relationship = vm.Relationship, Phone = vm.Phone, EmployeeId = vm.EmployeeId, Mobile = vm.Mobile, WorkPhone = vm.WorkPhone, Address = vm.Address, Email = vm.Email, Id = vm.Id }; _emergencyContactRepository.Create(emergencyContact); _unitOfWork.Commit(); return(emergencyContact); }, "Emergency Contact created sucessfully"); } } else { apiResult = ApiResultFromModelErrors <EmergencyContact>(); } return(Json(apiResult, JsonRequestBehavior.AllowGet)); }
public IHttpActionResult UpdateEmergencyContact(string ClientId, EmergencyContact emergencyContact) { EmergencyContact eContact = _emergencyContactRepository.Find(x => x.clientId == ClientId).FirstOrDefault(); eContact.FirstName = emergencyContact.FirstName; eContact.LastName = emergencyContact.LastName; eContact.Gender = emergencyContact.Gender; eContact.Relationship = emergencyContact.Relationship; eContact.Email = emergencyContact.Email; eContact.Mobile = emergencyContact.Mobile; eContact.AlternateNumber = emergencyContact.AlternateNumber; eContact.WorkNumber = emergencyContact.WorkNumber; eContact.Address = emergencyContact.Address; return(Ok(_emergencyContactRepository.Update(eContact))); }