107 lines
4.2 KiB
C#
107 lines
4.2 KiB
C#
using DataDomain;
|
|
using Helpers;
|
|
using SqliteBackups.Interfaces;
|
|
using StockDAL.Interface;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Diagnostics;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace StockInfo
|
|
{
|
|
public partial class frmBackup : Form
|
|
{
|
|
private readonly IBackupRepository _backupRepository;
|
|
private readonly IBackupRoutines _backupRoutines;
|
|
public List<BackupRegister> CurrentBackups { get; set; } = new List<BackupRegister>();
|
|
public frmBackup(IBackupRepository backupRepository,IBackupRoutines backupRoutines)
|
|
{
|
|
InitializeComponent();
|
|
_backupRepository = backupRepository;
|
|
_backupRoutines = backupRoutines;
|
|
}
|
|
|
|
private void btnChooseBackupDest_Click(object sender, EventArgs e)
|
|
{
|
|
sfdChoosePlaceAndFile.Filter = "Database files (*.db)|*.db|All files (*.*)|*.*";
|
|
if (sfdChoosePlaceAndFile.ShowDialog() == DialogResult.OK)
|
|
{
|
|
var wholeFile = sfdChoosePlaceAndFile.FileName;
|
|
var lastBackslash = wholeFile.LastIndexOf('\\') + 1;
|
|
var dir = wholeFile.Substring(0, lastBackslash);
|
|
var file = wholeFile.Substring(lastBackslash, wholeFile.Length - lastBackslash);
|
|
//Debug.WriteLine($"Chosen file:{wholeFile}");
|
|
//Debug.WriteLine($"Path: {dir} , File {file}");
|
|
BackupRegister reg = new();
|
|
reg.BackupDbName = file;
|
|
reg.BackupPath = dir;
|
|
reg = _backupRepository.SaveBackupReging(reg);
|
|
txtBackupPath.Text = dir;
|
|
txtBackupFile.Text = file;
|
|
_backupRoutines.BackupSqliteDb(reg.DbName, Path.Combine(dir, file));
|
|
var cmbItem = new ComboboxItem($"{reg.BackedUp.ToLocalTime()} {wholeFile}", reg.Id);
|
|
lstBackups.Items.Add(cmbItem);
|
|
CurrentBackups = _backupRepository.GetAllBackupRegisters().ToList();
|
|
};
|
|
|
|
}
|
|
|
|
private void btnClose_Click(object sender, EventArgs e)
|
|
{
|
|
this.Close();
|
|
}
|
|
|
|
private void frmBackup_Shown(object sender, EventArgs e)
|
|
{
|
|
CurrentBackups =_backupRepository.GetAllBackupRegisters().ToList();
|
|
lstBackups.Items.Clear();
|
|
foreach(var bckrg in CurrentBackups)
|
|
{
|
|
var cmbItem = new ComboboxItem($"{bckrg.BackedUp.ToLocalTime()} {Path.Combine(bckrg.BackupPath, bckrg.BackupDbName)}", bckrg.Id);
|
|
lstBackups.Items.Add(cmbItem);
|
|
}
|
|
}
|
|
|
|
private void btnRestore_Click(object sender, EventArgs e)
|
|
{
|
|
if (lstBackups.SelectedIndex != -1)
|
|
{
|
|
BackupRegister br = new();
|
|
br.Id = ((ComboboxItem)lstBackups.SelectedItem).HiddenValue;
|
|
var backupReg = _backupRepository.GetBackupRegisterById(br.Id);
|
|
if (File.Exists(backupReg.DbName))
|
|
{
|
|
File.Move(backupReg.DbName, "tempFile.bak",true);
|
|
}
|
|
_backupRoutines.BackupSqliteDb(Path.Combine(backupReg.BackupPath, backupReg.BackupDbName), backupReg.DbName);
|
|
UpdateBackupRegs(CurrentBackups);
|
|
MessageBox.Show($"Backup {Path.Combine(backupReg.BackupPath, backupReg.BackupDbName)} loaded !");
|
|
}
|
|
}
|
|
|
|
private void UpdateBackupRegs(List<BackupRegister> currentBackups)
|
|
{
|
|
var backupRegs = _backupRepository.GetAllBackupRegisters();
|
|
var result = currentBackups.Where(cb => backupRegs.All(br => br.BackupDbName != cb.BackupDbName || br.BackupPath != cb.BackupPath));
|
|
foreach(var br in result)
|
|
{
|
|
_backupRepository.SaveBackupReging(new BackupRegister
|
|
{
|
|
BackedUp = br.BackedUp,
|
|
BackupDbName = br.BackupDbName,
|
|
BackupPath = br.BackupPath,
|
|
DbName = br.DbName,
|
|
Id = 0
|
|
});
|
|
}
|
|
}
|
|
}
|
|
}
|