Files
biblioteka-standartnyh-izdeliy/dotnet/src/StandardProducts.App/MainWindow.xaml.cs
T

168 lines
4.6 KiB
C#

using System.IO;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Win32;
using StandardProducts.Core.Data;
using StandardProducts.Core.Gost;
using StandardProducts.Core.IO;
using StandardProducts.Core.Models;
namespace StandardProducts.App;
public partial class MainWindow : Window
{
private readonly CatalogDatabase _database;
private readonly ProductRepository _repository;
public MainWindow()
{
InitializeComponent();
var databasePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"StandardProducts",
"catalog.db");
_database = new CatalogDatabase(databasePath);
_repository = new ProductRepository(_database);
SearchBox.TextChanged += SearchBox_TextChanged;
RefreshList();
}
private void RefreshList()
{
try
{
var products = _repository.Search(SearchBox.Text, limit: 1000);
ProductsGrid.ItemsSource = products;
StatusText.Text = $"Найдено: {products.Count} · База: {_database.DatabasePath}";
}
catch (Exception exception)
{
ShowError(exception);
}
}
private void SearchBox_TextChanged(object sender, TextChangedEventArgs e) => RefreshList();
private void SeedGost_Click(object sender, RoutedEventArgs e)
{
try
{
var count = Gost7805.Seed(_repository);
RefreshList();
StatusText.Text = $"Загружено изделий ГОСТ 7805-70: {count}";
}
catch (Exception exception)
{
ShowError(exception);
}
}
private void Import_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog
{
Title = "Импорт каталога",
Filter = "JSON (*.json)|*.json|Все файлы (*.*)|*.*",
};
if (dialog.ShowDialog(this) != true)
{
return;
}
try
{
var count = JsonCatalog.Import(_repository, dialog.FileName);
RefreshList();
StatusText.Text = $"Импортировано записей: {count}";
}
catch (Exception exception)
{
ShowError(exception);
}
}
private void Export_Click(object sender, RoutedEventArgs e)
{
var dialog = new SaveFileDialog
{
Title = "Экспорт каталога",
Filter = "JSON (*.json)|*.json",
FileName = "catalog.json",
};
if (dialog.ShowDialog(this) != true)
{
return;
}
try
{
var count = JsonCatalog.Export(_repository.List(), dialog.FileName);
StatusText.Text = $"Экспортировано записей: {count} → {dialog.FileName}";
}
catch (Exception exception)
{
ShowError(exception);
}
}
private void Delete_Click(object sender, RoutedEventArgs e)
{
if (ProductsGrid.SelectedItem is not Product product)
{
MessageBox.Show(
this,
"Выберите изделие в таблице.",
"Удаление",
MessageBoxButton.OK,
MessageBoxImage.Information);
return;
}
var answer = MessageBox.Show(
this,
$"Удалить «{product.Designation}»?",
"Подтверждение",
MessageBoxButton.YesNo,
MessageBoxImage.Question);
if (answer != MessageBoxResult.Yes)
{
return;
}
try
{
_repository.Delete(product.Designation);
RefreshList();
StatusText.Text = $"Удалено: {product.Designation}";
}
catch (Exception exception)
{
ShowError(exception);
}
}
private void ProductsGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
AttributesText.Text = ProductsGrid.SelectedItem is Product product
? string.Join(
Environment.NewLine,
product.Attributes.Select(pair => $"{pair.Key}: {pair.Value}"))
: string.Empty;
}
private void ShowError(Exception exception) =>
MessageBox.Show(this, exception.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
protected override void OnClosed(EventArgs e)
{
_database.Dispose();
base.OnClosed(e);
}
}