From f8c04d93e2b71ceaf1d403f58b43e25d742d8e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Fri, 19 Mar 2021 00:02:59 +0100 Subject: [PATCH] Nackup / Restore of total DB is working --- StockDAL/BackupRepository.cs | 13 +++++- StockDal.Interface/IBackupRepository.cs | 1 + StockInfo/StockInfo.csproj | 2 +- StockInfo/frmBackup.Designer.cs | 17 +++++++- StockInfo/frmBackup.cs | 58 +++++++++++++++++++++++-- StockInfo/frmInitial.Designer.cs | 14 +----- StockInfo/frmInitial.cs | 3 -- 7 files changed, 86 insertions(+), 22 deletions(-) diff --git a/StockDAL/BackupRepository.cs b/StockDAL/BackupRepository.cs index 30fcf90..50e44c7 100644 --- a/StockDAL/BackupRepository.cs +++ b/StockDAL/BackupRepository.cs @@ -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 GetAllBackupRegisters() { using var context = new StockContext(); diff --git a/StockDal.Interface/IBackupRepository.cs b/StockDal.Interface/IBackupRepository.cs index 370b281..90060bb 100644 --- a/StockDal.Interface/IBackupRepository.cs +++ b/StockDal.Interface/IBackupRepository.cs @@ -10,6 +10,7 @@ namespace StockDAL.Interface public interface IBackupRepository { IEnumerable GetAllBackupRegisters(); + BackupRegister GetBackupRegisterById(int brId); BackupRegister SaveBackupReging(BackupRegister backupRegister); } } diff --git a/StockInfo/StockInfo.csproj b/StockInfo/StockInfo.csproj index ef3d9f9..f30572c 100644 --- a/StockInfo/StockInfo.csproj +++ b/StockInfo/StockInfo.csproj @@ -53,7 +53,7 @@ - Never + PreserveNewest diff --git a/StockInfo/frmBackup.Designer.cs b/StockInfo/frmBackup.Designer.cs index dba5b60..184cf21 100644 --- a/StockInfo/frmBackup.Designer.cs +++ b/StockInfo/frmBackup.Designer.cs @@ -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; } } \ No newline at end of file diff --git a/StockInfo/frmBackup.cs b/StockInfo/frmBackup.cs index 7b337aa..9179268 100644 --- a/StockInfo/frmBackup.cs +++ b/StockInfo/frmBackup.cs @@ -20,7 +20,7 @@ namespace StockInfo { private readonly IBackupRepository _backupRepository; private readonly IBackupRoutines _backupRoutines; - + public List CurrentBackups { get; set; } = new List(); 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 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 + }); + } + } } } diff --git a/StockInfo/frmInitial.Designer.cs b/StockInfo/frmInitial.Designer.cs index bf50f65..44543b3 100644 --- a/StockInfo/frmInitial.Designer.cs +++ b/StockInfo/frmInitial.Designer.cs @@ -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; } } diff --git a/StockInfo/frmInitial.cs b/StockInfo/frmInitial.cs index a5aed95..a02c4c9 100644 --- a/StockInfo/frmInitial.cs +++ b/StockInfo/frmInitial.cs @@ -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; } }