Nackup / Restore of total DB is working

This commit is contained in:
2021-03-19 00:02:59 +01:00
parent 154f8cc07a
commit f8c04d93e2
7 changed files with 86 additions and 22 deletions

View File

@ -17,7 +17,7 @@ namespace StockDAL
var entity = (from brr in context.BackupRegings
where brr.Id == backupRegister.Id
select brr).FirstOrDefault();
if(entity == null)
if (entity == null)
{
entity = new BackupRegister
{
@ -40,6 +40,17 @@ namespace StockDAL
return entity;
}
public BackupRegister GetBackupRegisterById(int brId)
{
using var context = new StockContext();
var entity = (from br in context.BackupRegings
where br.Id == brId
select br).FirstOrDefault();
return entity;
}
public IEnumerable<BackupRegister> GetAllBackupRegisters()
{
using var context = new StockContext();

View File

@ -10,6 +10,7 @@ namespace StockDAL.Interface
public interface IBackupRepository
{
IEnumerable<BackupRegister> GetAllBackupRegisters();
BackupRegister GetBackupRegisterById(int brId);
BackupRegister SaveBackupReging(BackupRegister backupRegister);
}
}

View File

@ -53,7 +53,7 @@
<ItemGroup>
<None Update="Stocks.db">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

View File

@ -36,6 +36,7 @@ namespace StockInfo
this.txtBackupFile = new System.Windows.Forms.TextBox();
this.lstBackups = new System.Windows.Forms.ListBox();
this.btnClose = new System.Windows.Forms.Button();
this.btnRestore = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
@ -83,7 +84,7 @@ namespace StockInfo
this.lstBackups.ItemHeight = 15;
this.lstBackups.Location = new System.Drawing.Point(13, 70);
this.lstBackups.Name = "lstBackups";
this.lstBackups.Size = new System.Drawing.Size(214, 184);
this.lstBackups.Size = new System.Drawing.Size(306, 184);
this.lstBackups.TabIndex = 4;
//
// btnClose
@ -94,12 +95,24 @@ namespace StockInfo
this.btnClose.TabIndex = 5;
this.btnClose.Text = "Close";
this.btnClose.UseVisualStyleBackColor = true;
this.btnClose.Click += new System.EventHandler(this.btnClose_Click);
//
// btnRestore
//
this.btnRestore.Location = new System.Drawing.Point(350, 70);
this.btnRestore.Name = "btnRestore";
this.btnRestore.Size = new System.Drawing.Size(75, 23);
this.btnRestore.TabIndex = 6;
this.btnRestore.Text = "Restore";
this.btnRestore.UseVisualStyleBackColor = true;
this.btnRestore.Click += new System.EventHandler(this.btnRestore_Click);
//
// frmBackup
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(438, 294);
this.Controls.Add(this.btnRestore);
this.Controls.Add(this.btnClose);
this.Controls.Add(this.lstBackups);
this.Controls.Add(this.txtBackupFile);
@ -109,6 +122,7 @@ namespace StockInfo
this.Name = "frmBackup";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "frmBackup";
this.Shown += new System.EventHandler(this.frmBackup_Shown);
this.ResumeLayout(false);
this.PerformLayout();
@ -123,5 +137,6 @@ namespace StockInfo
private System.Windows.Forms.TextBox txtBackupFile;
private System.Windows.Forms.ListBox lstBackups;
private System.Windows.Forms.Button btnClose;
private System.Windows.Forms.Button btnRestore;
}
}

View File

@ -20,7 +20,7 @@ namespace StockInfo
{
private readonly IBackupRepository _backupRepository;
private readonly IBackupRoutines _backupRoutines;
public List<BackupRegister> CurrentBackups { get; set; } = new List<BackupRegister>();
public frmBackup(IBackupRepository backupRepository,IBackupRoutines backupRoutines)
{
InitializeComponent();
@ -43,12 +43,64 @@ namespace StockInfo
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(wholeFile, reg.Id);
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
});
}
}
}
}

View File

@ -32,7 +32,6 @@ namespace StockInfo
this.dataGridView = new System.Windows.Forms.DataGridView();
this.lblTotalRecords = new System.Windows.Forms.Label();
this.gB1 = new System.Windows.Forms.GroupBox();
this.btnRestoreAll = new System.Windows.Forms.Button();
this.btnBackupAll = new System.Windows.Forms.Button();
this.chkEnableBackRes = new System.Windows.Forms.CheckBox();
this.btnRestoreShares = new System.Windows.Forms.Button();
@ -83,7 +82,6 @@ namespace StockInfo
// gB1
//
this.gB1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.gB1.Controls.Add(this.btnRestoreAll);
this.gB1.Controls.Add(this.btnBackupAll);
this.gB1.Controls.Add(this.chkEnableBackRes);
this.gB1.Controls.Add(this.btnRestoreShares);
@ -96,22 +94,13 @@ namespace StockInfo
this.gB1.TabStop = false;
this.gB1.Text = "Sharelist";
//
// btnRestoreAll
//
this.btnRestoreAll.Location = new System.Drawing.Point(130, 111);
this.btnRestoreAll.Name = "btnRestoreAll";
this.btnRestoreAll.Size = new System.Drawing.Size(105, 23);
this.btnRestoreAll.TabIndex = 7;
this.btnRestoreAll.Text = "Restore All";
this.btnRestoreAll.UseVisualStyleBackColor = true;
//
// btnBackupAll
//
this.btnBackupAll.Location = new System.Drawing.Point(130, 82);
this.btnBackupAll.Name = "btnBackupAll";
this.btnBackupAll.Size = new System.Drawing.Size(105, 23);
this.btnBackupAll.TabIndex = 6;
this.btnBackupAll.Text = "Backup All";
this.btnBackupAll.Text = "Backup / Restore";
this.btnBackupAll.UseVisualStyleBackColor = true;
this.btnBackupAll.Click += new System.EventHandler(this.btnBackupAll_Click);
//
@ -347,7 +336,6 @@ namespace StockInfo
private System.Windows.Forms.Button btnConnShares;
private System.Windows.Forms.Button btnEditPerson;
private System.Windows.Forms.ComboBox cmbOwners;
private System.Windows.Forms.Button btnRestoreAll;
private System.Windows.Forms.Button btnBackupAll;
}
}

View File

@ -63,7 +63,6 @@ namespace StockInfo
btnRestoreShares.Enabled = false;
btnBackupShares.Enabled = false;
btnReloadShares.Enabled = false;
btnRestoreAll.Enabled = false;
btnBackupAll.Enabled = false;
}
@ -146,14 +145,12 @@ namespace StockInfo
btnRestoreShares.Enabled = true;
btnBackupShares.Enabled = true;
btnBackupAll.Enabled = true;
btnRestoreAll.Enabled = true;
}
else
{
btnRestoreShares.Enabled = false;
btnBackupShares.Enabled = false;
btnBackupAll.Enabled = false;
btnRestoreAll.Enabled = false;
}
}