private void ModifyCommandExecute(Object param) { SelectedEvent.BeginEdit(); if (AddNewEventFunc != null && AddNewEventFunc(SelectedEvent)) { using (var context = new DataContext()) { context.UpdateEventParticipators(SelectedEvent.Id, SelectedEvent.Participators); context.UpdateEvent(SelectedEvent.ConvertBackToDalModel()); context.UpdateEventDocuments(SelectedEvent.Documents.Select(d => d.ConvertBackToDalModel()), SelectedEvent.Id); SelectedEvent.EndEdit(); RefreshEvents(); return; } } SelectedEvent.CancelEdit(); }
private async void OnSave() { if (AddMode) { var _event = Converter.ConvertFromEventModel(SelectedEvent); int id = 0; bool success = true; string error = ""; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <Event> repo = new Repository <Event>()) { var result = repo.InsertWithResult(_event); if (result.Status.Success) { id = result.Data.ID; } else { success = false; error = result.Status.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured. Message: {ex.Message}"; } }); if (success) { SelectedEvent.Id = id; OnInformationRequested("Data added successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { EventCollection.Remove(SelectedEvent); SelectedEvent = null; OnErrorOccured(error); } AddMode = false; } else if (EditMode) { var _event = Converter.ConvertFromEventModel(SelectedEvent); bool success = true; string error = ""; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <Event> repo = new Repository <Event>()) { var result = repo.Update(_event); if (!result.Success) { success = false; error = result.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured. Message: {ex.Message}"; } }); if (success) { SelectedEvent.EndEdit(); OnInformationRequested("Data updated successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { SelectedEvent.CancelEdit(); OnErrorOccured(error); } EditMode = false; } else if (RemoveMode) { var temp = from item in EventCollection where item.IsChecked select item; var eventsToRemove = temp.Select(x => Converter.ConvertFromEventModel(x)); bool success = true; string error = ""; OnInformationRequested("Loading..."); await Task.Run(() => { try { using (Repository <Event> repo = new Repository <Event>()) { var result = repo.Delete(eventsToRemove); if (!result.Success) { success = false; error = result.ErrorMessage; } } } catch (Exception ex) { success = false; error = $"An error occured. Message: {ex.Message}"; } }); if (success) { //to avoid Invalid Operation Exception for (int i = 0; i < EventCollection.Count; i++) { if (EventCollection[i].IsChecked) { EventCollection.RemoveAt(i); i--; } } OnInformationRequested("Data removed successfully, refreshing service in the background..."); await Task.Run(() => { var result = Controller.RefreshService(); if (!result.Success) { success = false; error = result.ErrorMessage; } }); if (success) { OnInformationRequested("Service refreshed successfully"); } else { OnErrorOccured(error); } } else { OnErrorOccured(error); } RemoveMode = false; } CanUseDatagrid = true; CanEditFields = false; }