Пример #1
0
        /// <summary>
        /// category context menu delete click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CategoryContextMenuDelete_Click(object sender, RoutedEventArgs e)
        {
            if (!((this._categoryMenu.Tag as ListViewItem)?.DataContext is CategoryModel model))
            {
                return;
            }
            var dialog = new CategoryDeleteConfirm(this, model, this._categoryList);

            if (true != dialog.ShowDialog())
            {
                return;
            }

            try {
                this._profileDatabase.Open();
                this._profileDatabase.BeginTrans();

                using (var categoriesTable = new CategoriesTable(this._profileDatabase))
                    using (var itemsTable = new ItemsTable(this._profileDatabase)) {
                        if (0 == categoriesTable.DeleteById(model))
                        {
                            AppCommon.ShowErrorMsg(string.Format(ErrorMsg.FailToDelete, "Category"));
                        }
                        if (-1 == dialog.MoveCategory)
                        {
                            itemsTable.DeleteByCateogyId(model.Id);
                        }
                        else
                        {
                            itemsTable.UpdateCategoryIdByCategoryId(model.Id, dialog.MoveCategory);
                        }
                        this._profileDatabase.CommitTrans();
                    }
                this._categoryList.Remove(model);
            } finally {
                this._profileDatabase.RollbackTrans();
                this._profileDatabase.Close();
            }
        }