/
WorkerTime
/
WorkerTimeDiplom
Обзор
Документация
Войти
/
WorkerTime
/
WorkerTimeDiplom
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
ViewModels/SettingsViewModel.cs
119 строк
5 KB
nikit
Логирование, автосохранение, восстановление пароля
05 июн 2026, 19:24
05 июн 2026, 19:24
715eb3d
Код
Авторство
О чём код?
using System; using System.Windows; using System.Windows.Input; using WorkerTime.Services; namespace WorkerTime.ViewModels { public class SettingsViewModel : BaseViewModel { private readonly SettingsService _settingsService; private readonly BackupService _backupService; public string OrganizationName { get; set; } public string Inn { get; set; } public string Okpo { get; set; } public string BackupFolder { get; set; } public ICommand SaveOrgCommand { get; set; } public ICommand ChangePasswordCommand { get; set; } public ICommand BrowseFolderCommand { get; set; } public ICommand CreateBackupCommand { get; set; } public ICommand BrowseBackupCommand { get; set; } public ICommand RestoreCommand { get; set; } public SettingsViewModel() { _settingsService = new SettingsService(App.DbContext); _backupService = new BackupService(); AppSettings settings = _settingsService.GetSettings(); OrganizationName = settings.OrganizationName; Inn = settings.Inn; Okpo = settings.Okpo; BackupFolder = @"C:\Backups\WorkerTime"; SaveOrgCommand = new RelayCommand(_ => SaveOrganization()); ChangePasswordCommand = new RelayCommand(_ => { }); BrowseFolderCommand = new RelayCommand(_ => BrowseFolder()); CreateBackupCommand = new RelayCommand(_ => CreateBackup()); BrowseBackupCommand = new RelayCommand(_ => BrowseBackup()); RestoreCommand = new RelayCommand(_ => RestoreBackup()); } private void SaveOrganization() { AppSettings settings = new AppSettings(); settings.OrganizationName = OrganizationName; settings.Inn = Inn; settings.Okpo = Okpo; _settingsService.SaveSettings(settings); MessageBox.Show("Настройки сохранены.", "Готово", MessageBoxButton.OK, MessageBoxImage.Information); } private void BrowseFolder() { Microsoft.Win32.OpenFolderDialog folderDialog = new Microsoft.Win32.OpenFolderDialog(); folderDialog.Title = "Выберите папку для резервных копий"; if (folderDialog.ShowDialog() == true) { BackupFolder = folderDialog.FolderName; OnPropertyChanged(nameof(BackupFolder)); } } private void CreateBackup() { try { _backupService.CreateBackup(BackupFolder); MessageBox.Show("Резервная копия создана в папке:\n" + BackupFolder, "Готово", MessageBoxButton.OK, MessageBoxImage.Information); } catch (Exception ex) { MessageBox.Show("Ошибка: " + ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } private void BrowseBackup() { Microsoft.Win32.OpenFileDialog dialog = new Microsoft.Win32.OpenFileDialog(); dialog.Title = "Выберите файл резервной копии"; dialog.Filter = "Файлы базы данных (*.db)|*.db"; dialog.DefaultExt = ".db"; if (dialog.ShowDialog() == true) { try { MessageBoxResult result = MessageBox.Show( "Восстановление заменит все текущие данные. Продолжить?", "Подтверждение", MessageBoxButton.YesNo, MessageBoxImage.Warning); if (result == MessageBoxResult.Yes) { _backupService.RestoreFromBackup(dialog.FileName); MessageBox.Show("База данных восстановлена. Приложение будет закрыто.", "Готово", MessageBoxButton.OK, MessageBoxImage.Information); Application.Current.Shutdown(); } } catch (Exception ex) { MessageBox.Show("Ошибка: " + ex.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error); } } } private void RestoreBackup() { BrowseBackup(); } } }