/// <summary> /// Deletes message. Gets id to remove from input field. /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void DeleteMessage(object sender, RoutedEventArgs e) { MessageBoxResult result = MessageBox.Show("Are you sure you want to delete?", "Deleting", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result == MessageBoxResult.Yes && idToDelete.Text.Length > 0) { FirebaseHandler firebaseHandler = new FirebaseHandler(); CollectionHandler collection = new CollectionHandler(); string locale = ""; int index = 0; foreach (dataModel item in this.dataCollection) { if (item.id.Equals(idToDelete.Text)) { locale = item.locale; index = this.dataCollection.IndexOf(item); } } if (locale.Equals("fi") || locale.Equals("en")) { string[] response = await firebaseHandler.DeleteFromDb(idToDelete.Text, collection.GetNewsCollection(locale)); if (response[0].Equals("200")) { this.dataCollection.RemoveAt(index); _ = MessageBox.Show("Deleted succesfully", "Deleted", MessageBoxButton.OK, MessageBoxImage.Information); } else { _ = MessageBox.Show(response[1], "Error happened", MessageBoxButton.OK, MessageBoxImage.Error); } } else { _ = MessageBox.Show("Error cannot get locale from news delete. Are you sure id is valid?", "Error happened", MessageBoxButton.OK, MessageBoxImage.Error); } } else if (idToDelete.Text.Length == 0) { _ = MessageBox.Show("Id to delete cannot be empty!", "Id is empty", MessageBoxButton.OK, MessageBoxImage.Information); } else { _ = MessageBox.Show("Nothing deleted!", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information); } }
// remove button private async void RemoveMessage(object sender, RoutedEventArgs e) { MessageBoxResult wannaRemove = MessageBox.Show("Are you sure you want to remove?", "Confirmation", MessageBoxButton.YesNo, MessageBoxImage.Question); if (wannaRemove == MessageBoxResult.Yes) { // collection does get removed if empty, prevent that if (this.data.Count > 1) { FirebaseHandler firebaseHandler = new FirebaseHandler(); CollectionHandler collectionH = new CollectionHandler(); string[] response = await firebaseHandler.DeleteFromDb(this.currentMessage, collectionH.GetMessageCollection()); if (response[0].Equals("200")) { int index = this.FindIndex(this.currentMessage); try { this.data.RemoveAt(index); this.InsertToDom(this.data[0]); _ = MessageBox.Show("Document deleted!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ed) { _ = MessageBox.Show(ed.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } //error from firebase else { _ = MessageBox.Show(response[1], "Error", MessageBoxButton.OK, MessageBoxImage.Error); } } // cannot remove only document else { _ = MessageBox.Show("Nothing removed! Collection length must be at least one.", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } } // cancelled remove else { _ = MessageBox.Show("Nothing removed!", "Information", MessageBoxButton.OK, MessageBoxImage.Information); } }