/// <summary> /// Deletes the currently selected event. /// </summary> private void DeleteEvent() { if (DetailsDataContext.SelectedEvent == null) { return; } dialogBox = new DialogBox(DialogBoxType.WARNING, "DELETE!", "Would you like to delete this event?"); dialogBox.ShowDialog(); if (dialogBox.Answer == DialogAnswer.NO) { return; } using (var context = new EventDatabaseEntities()) { var deleteItem = context.Events.FirstOrDefault(x => x.ID == SelectedEvent.CurrentEvent.ID); context.Events.Remove(deleteItem); context.SaveChanges(); // Remove the deleted event from the UI. var delete = TodayEventsList.FirstOrDefault(r => r.CurrentEvent.ID == SelectedEvent.CurrentEvent.ID); TodayEventsList.Remove(delete); DetailsDataContext.TodayTasksList = null; DetailsDataContext.SelectedEvent = null; DetailsDataContext.ProgressbarValue = 0; dialogBox = new DialogBox("Event deleted successfully!"); dialogBox.Show(); } }
/// <summary> /// Completing the selected event and add it to the completed table. /// </summary> private void CompleteEvent() { if (SelectedEvent == null || SelectedEvent.IsCompleted) { return; } using (var context = new EventDatabaseEntities()) { Completed completed = new Completed() { EventID = SelectedEvent.CurrentEvent.ID }; context.Completeds.Add(completed); // Mark every task as completed. (from item in context.Tasks where item.EventID == SelectedEvent.CurrentEvent.ID select item).ToList().ForEach(x => x.Completed = true); TodayTasksList.ToList().ForEach(x => x.Completed = true); SelectedEvent.IsCompleted = true; ProgressbarValue = ProgressbarSize; context.SaveChanges(); MessageBox.Show("Event completed successfully!"); } }
/// <summary> /// Directly deletes an event from the today's list. /// </summary> /// <param name="sender"></param> private void DirectDelete() { dialogBox = new DialogBox(DialogBoxType.WARNING, "DELETE!", "Would you like to delete this event?"); dialogBox.ShowDialog(); if (dialogBox.Answer == DialogAnswer.NO) { return; } // GOT A BIG ERROR HERE AFTER EDITING... using (var context = new EventDatabaseEntities()) { // Create a new object to avoid DbUpdateConcurrencyException. var deleteItem = context.Events.FirstOrDefault(x => x.ID == SelectedEvent.CurrentEvent.ID); context.Events.Remove(deleteItem); context.SaveChanges(); // Remove deleted event from the UI. DetailsDataContext.TodayTasksList = new ObservableCollection <Task>(); TodayEventsList.Remove(SelectedEvent); DetailsDataContext.SelectedEvent = null; DetailsDataContext.ProgressbarValue = 0; } }
/// <summary> /// Directly deletes an event from the today's list. /// </summary> /// <param name="sender"></param> private void DirectDelete(EventDataModel sender) { dialogBox = new DialogBox(DialogBoxType.WARNING, "DELETE!", "Would you like to delete this event?"); dialogBox.ShowDialog(); if (dialogBox.Answer == DialogAnswer.NO) { return; } using (var context = new EventDatabaseEntities()) { context.Events.Attach(sender.CurrentEvent); context.Events.Remove(sender.CurrentEvent); context.SaveChanges(); var delete = UpcomingEventsList.Where(p => p.EventModelList.Any(x => x.CurrentEvent.ID == sender.CurrentEvent.ID)); delete.ElementAt(0).EventModelList.Remove(sender); DetailsDataContext.SelectedEvent = null; DetailsDataContext.ProgressbarValue = 0; DetailsDataContext.TodayTasksList = new ObservableCollection <Task>(); // Remove the empy date object. var item = UpcomingEventsList.FirstOrDefault(x => x.EventModelList.Count == 0); if (item != null) { UpcomingEventsList.Remove(item); } } }
/// <summary> /// Updates the selected event. /// </summary> private void UpdateEvent() { if (SelectedEvent.IsCompleted) { return; } if (SelectedEvent.CurrentEvent.Date == null || string.IsNullOrWhiteSpace(SelectedEvent.CurrentEvent.Title)) { MessageBox.Show("Wrong title or date!"); return; } using (var context = new EventDatabaseEntities()) { // Update the event. var update = context.Events.Where(x => x.ID == SelectedEvent.CurrentEvent.ID).Single(); context.Entry(update).CurrentValues.SetValues(SelectedEvent); context.Tasks.RemoveRange(context.Tasks.Where(x => x.EventID == SelectedEvent.CurrentEvent.ID)); context.SaveChanges(); foreach (var item in TodayTasksList) { if (!string.IsNullOrWhiteSpace(item.Description)) { context.Tasks.Add(new Task { EventID = SelectedEvent.CurrentEvent.ID, Description = item.Description }); } } context.SaveChanges(); //Remove every empty task item from the list if there any. TodayTasksList.Where(x => string.IsNullOrWhiteSpace(x.Description)).ToList().All(i => TodayTasksList.Remove(i)); TaskNumberSelected = TodayTasksList.Count().ToString(); ProgressbarSize = TodayTasksList.Count(); ProgressbarValue = TodayTasksList.Where(r => r.Completed == true).Count(); IsEditable = false; } }
/// <summary> /// Deletes the selected event. /// </summary> private void DeleteEvent(Event sender) { dialogBox = new DialogBox(DialogBoxType.WARNING, "DELETE!", "Would you like to delete this event?"); dialogBox.ShowDialog(); if (dialogBox.Answer == DialogAnswer.NO) { return; } using (var context = new EventDatabaseEntities()) { context.Events.Attach(sender); context.Events.Remove(sender); context.SaveChanges(); dialogBox = new DialogBox("Event deleted successfully!"); dialogBox.ShowDialog(); if (sender.ID == AutoSearchItem.ID) { CloseEvent(); } } foreach (var item in HistoryList) { foreach (var del in item.EventList) { if (del.CurrentEvent.ID == sender.ID) { item.EventList.Remove(del); if (item.EventList.Count == 0) { HistoryList.Remove(item); } return; } } } }
/// <summary> /// Deletes the currently selected event. /// </summary> private void DeleteEvent() { if (DetailsDataContext.SelectedEvent == null) { return; } dialogBox = new DialogBox(DialogBoxType.WARNING, "DELETE!", "Would you like to delete this event?"); dialogBox.ShowDialog(); if (dialogBox.Answer == DialogAnswer.NO) { return; } using (var context = new EventDatabaseEntities()) { context.Events.Attach(DetailsDataContext.SelectedEvent.CurrentEvent); context.Events.Remove(DetailsDataContext.SelectedEvent.CurrentEvent); context.SaveChanges(); var delete = UpcomingEventsList.SelectMany(x => x.EventModelList.Where(r => r.CurrentEvent.ID == SelectedEvent.CurrentEvent.ID)).Single(); UpcomingEventsList.ToList().RemoveAll(x => x.EventModelList.Remove(delete)); DetailsDataContext.TodayTasksList = null; DetailsDataContext.SelectedEvent = null; DetailsDataContext.ProgressbarValue = 0; // Remove the empy date object. var item = UpcomingEventsList.FirstOrDefault(x => x.EventModelList.Count == 0); if (item != null) { UpcomingEventsList.Remove(item); } dialogBox = new DialogBox("Event deleted successfully!"); dialogBox.Show(); } }
/// <summary> /// Check or uncheck a task. /// </summary> /// <param name="sender"></param> private void CheckTaskEvent(Task sender) { if (SelectedEvent.IsCompleted) { sender.Completed = true; return; } if (sender.Completed) { ProgressbarValue++; } else { ProgressbarValue--; } using (var context = new EventDatabaseEntities()) { var item = context.Tasks.Where(r => r.ID == sender.ID).Single(); item.Completed = sender.Completed; context.SaveChanges(); } }
/// <summary> /// Creates a new event. /// </summary> private void AddEvent() { if (string.IsNullOrWhiteSpace(EventModel.EventTitle) || EventModel.EventDate == null) { dialogBox = new DialogBox("ERROR!", "Invalid inputs!"); dialogBox.Show(); return; } dialogBox = new DialogBox(DialogBoxType.WARNING, "CONFIRM!", "Would you like to add this event?"); dialogBox.ShowDialog(); if (dialogBox.Answer == DialogAnswer.NO) { return; } if (!string.IsNullOrWhiteSpace(EventModel.EventTime)) { EventModel.EventDate += " " + EventModel.EventTime; } using (var context = new EventDatabaseEntities()) { try { var item = new Event() { Title = EventModel.EventTitle, Priority = EventModel.Priority.ToString(), Date = DateTime.Parse(EventModel.EventDate), Description = EventModel.EventDescription, }; context.Events.Add(item); context.SaveChanges(); if (item.Date.Date == DateTime.Today.Date) { TodayEventsList.Add(new EventDataModel { CurrentEvent = item, IsCompleted = false }); } if (TasksList.Count != 0) { foreach (var task in TasksList) { if (!string.IsNullOrWhiteSpace(task.Description)) { context.Tasks.Add(new Task { EventID = item.ID, Description = task.Description }); } } context.SaveChanges(); } dialogBox = new DialogBox("Event added successfully!"); dialogBox.Show(); } catch { dialogBox = new DialogBox("ERROR!", "There was an error!"); dialogBox.Show(); } } }