Backup / Restore funktioner infört för kommande uppdateringar

This commit is contained in:
2021-03-07 15:57:33 +01:00
parent c2d8e76643
commit 03878ae8af
7 changed files with 167 additions and 33 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -64,6 +64,13 @@ namespace StockDAL
return output;
}
public void RemoveAllStocks()
{
using var context = new StockContext();
context.Stocks.RemoveRange(GetAllStocks());
context.SaveChanges();
}
public void InsertMany(List<StockMember> stockMembers)
{
using var context = new StockContext();
@ -71,5 +78,38 @@ namespace StockDAL
context.SaveChanges();
}
public void RestoreStockMembers(List<StockMember> stockMembers)
{
using var context = new StockContext();
using var transaction = context.Database.BeginTransaction();
try
{
var stocksToRemove = context.Stocks;
context.Stocks.RemoveRange(stocksToRemove);
var insertStocks = stockMembers.Select(o => new StockMember
{
Id = 0,
StockId = o.StockId,
StockExtId = o.StockExtId,
BuyDate = o.BuyDate,
BuyValue = o.BuyValue,
ActAmount = o.ActAmount,
ActValue = o.ActValue,
ActDate = o.ActDate,
SoldValue = o.SoldValue,
SoldDate = o.SoldDate,
Comment = o.Comment,
PostAmount = o.PostAmount
}).ToList();
context.Stocks.AddRange(insertStocks);
context.SaveChanges();
transaction.Commit();
}
catch (Exception ex)
{
throw new InvalidOperationException(ex.Message);
}
}
}
}

View File

@ -12,6 +12,8 @@ namespace StockDAL.Interface
IEnumerable<StockMember> GetAllRemainingStocks();
IEnumerable<StockMember> GetAllStocks();
void InsertMany(List<StockMember> stockMembers);
void RemoveAllStocks();
void RestoreStockMembers(List<StockMember> stockMembers);
void SaveStockMember(StockMember stockMember);
void UpdateActualForSell(int id, int sellAmount, decimal sellPrice, DateTime sellDate);
void UpdateActualPrice(int id, decimal price);

View File

@ -32,6 +32,9 @@ namespace StockInfo
this.dataGridView = new System.Windows.Forms.DataGridView();
this.lblTotalRecords = new System.Windows.Forms.Label();
this.gB1 = new System.Windows.Forms.GroupBox();
this.chkEnableBackRes = new System.Windows.Forms.CheckBox();
this.btnRestoreShares = new System.Windows.Forms.Button();
this.btnBackupShares = new System.Windows.Forms.Button();
this.btnReload = new System.Windows.Forms.Button();
this.btnTestScrapFunction = new System.Windows.Forms.Button();
this.lblStockRows = new System.Windows.Forms.Label();
@ -41,6 +44,8 @@ namespace StockInfo
this.btnStockSale = new System.Windows.Forms.Button();
this.btnValueView = new System.Windows.Forms.Button();
this.btnStockReg = new System.Windows.Forms.Button();
this.sfdSaver = new System.Windows.Forms.SaveFileDialog();
this.ofdOpener = new System.Windows.Forms.OpenFileDialog();
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).BeginInit();
this.gB1.SuspendLayout();
this.gbStockMgmnt.SuspendLayout();
@ -55,13 +60,12 @@ namespace StockInfo
this.dataGridView.Location = new System.Drawing.Point(12, 16);
this.dataGridView.Name = "dataGridView";
this.dataGridView.RowTemplate.Height = 25;
this.dataGridView.Size = new System.Drawing.Size(776, 363);
this.dataGridView.Size = new System.Drawing.Size(829, 363);
this.dataGridView.TabIndex = 0;
//
// lblTotalRecords
//
this.lblTotalRecords.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.lblTotalRecords.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.lblTotalRecords.AutoSize = true;
this.lblTotalRecords.Location = new System.Drawing.Point(12, 385);
this.lblTotalRecords.Name = "lblTotalRecords";
@ -71,20 +75,53 @@ namespace StockInfo
//
// gB1
//
this.gB1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.gB1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.gB1.Controls.Add(this.chkEnableBackRes);
this.gB1.Controls.Add(this.btnRestoreShares);
this.gB1.Controls.Add(this.btnBackupShares);
this.gB1.Controls.Add(this.btnReload);
this.gB1.Location = new System.Drawing.Point(157, 388);
this.gB1.Name = "gB1";
this.gB1.Size = new System.Drawing.Size(248, 35);
this.gB1.Size = new System.Drawing.Size(264, 123);
this.gB1.TabIndex = 3;
this.gB1.TabStop = false;
this.gB1.Text = "Sharelist";
//
// chkEnableBackRes
//
this.chkEnableBackRes.AutoSize = true;
this.chkEnableBackRes.Location = new System.Drawing.Point(7, 54);
this.chkEnableBackRes.Name = "chkEnableBackRes";
this.chkEnableBackRes.Size = new System.Drawing.Size(144, 19);
this.chkEnableBackRes.TabIndex = 5;
this.chkEnableBackRes.Text = "Enable backup/restore";
this.chkEnableBackRes.UseVisualStyleBackColor = true;
this.chkEnableBackRes.CheckedChanged += new System.EventHandler(this.chkEnableBackRes_CheckedChanged);
//
// btnRestoreShares
//
this.btnRestoreShares.Location = new System.Drawing.Point(131, 84);
this.btnRestoreShares.Name = "btnRestoreShares";
this.btnRestoreShares.Size = new System.Drawing.Size(118, 23);
this.btnRestoreShares.TabIndex = 4;
this.btnRestoreShares.Text = "Restore Share data";
this.btnRestoreShares.UseVisualStyleBackColor = true;
this.btnRestoreShares.Click += new System.EventHandler(this.btnRestoreShares_Click);
//
// btnBackupShares
//
this.btnBackupShares.Location = new System.Drawing.Point(7, 84);
this.btnBackupShares.Name = "btnBackupShares";
this.btnBackupShares.Size = new System.Drawing.Size(118, 23);
this.btnBackupShares.TabIndex = 3;
this.btnBackupShares.Text = "Backup Share data";
this.btnBackupShares.UseVisualStyleBackColor = true;
this.btnBackupShares.Click += new System.EventHandler(this.btnBackupShares_Click);
//
// btnReload
//
this.btnReload.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnReload.Location = new System.Drawing.Point(0, 9);
this.btnReload.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnReload.Location = new System.Drawing.Point(6, 22);
this.btnReload.Name = "btnReload";
this.btnReload.Size = new System.Drawing.Size(112, 23);
this.btnReload.TabIndex = 2;
@ -94,9 +131,8 @@ namespace StockInfo
//
// btnTestScrapFunction
//
this.btnTestScrapFunction.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnTestScrapFunction.Location = new System.Drawing.Point(12, 429);
this.btnTestScrapFunction.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnTestScrapFunction.Location = new System.Drawing.Point(12, 439);
this.btnTestScrapFunction.Name = "btnTestScrapFunction";
this.btnTestScrapFunction.Size = new System.Drawing.Size(75, 23);
this.btnTestScrapFunction.TabIndex = 4;
@ -115,11 +151,10 @@ namespace StockInfo
//
// button1
//
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.button1.Location = new System.Drawing.Point(157, 429);
this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.button1.Location = new System.Drawing.Point(12, 410);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 23);
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 6;
this.button1.Text = "ReLoad";
this.button1.UseVisualStyleBackColor = true;
@ -127,10 +162,9 @@ namespace StockInfo
//
// chbShowBrowser
//
this.chbShowBrowser.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.chbShowBrowser.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.chbShowBrowser.AutoSize = true;
this.chbShowBrowser.Location = new System.Drawing.Point(12, 455);
this.chbShowBrowser.Location = new System.Drawing.Point(12, 474);
this.chbShowBrowser.Name = "chbShowBrowser";
this.chbShowBrowser.Size = new System.Drawing.Size(100, 22);
this.chbShowBrowser.TabIndex = 7;
@ -140,22 +174,21 @@ namespace StockInfo
//
// gbStockMgmnt
//
this.gbStockMgmnt.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.gbStockMgmnt.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.gbStockMgmnt.Controls.Add(this.btnStockSale);
this.gbStockMgmnt.Controls.Add(this.btnValueView);
this.gbStockMgmnt.Controls.Add(this.btnStockReg);
this.gbStockMgmnt.Location = new System.Drawing.Point(411, 388);
this.gbStockMgmnt.Location = new System.Drawing.Point(427, 388);
this.gbStockMgmnt.Name = "gbStockMgmnt";
this.gbStockMgmnt.Size = new System.Drawing.Size(257, 123);
this.gbStockMgmnt.TabIndex = 8;
this.gbStockMgmnt.TabStop = false;
this.gbStockMgmnt.Text = "Working";
//
// btnStockSale
//
this.btnStockSale.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnStockSale.Location = new System.Drawing.Point(108, 9);
this.btnStockSale.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnStockSale.Location = new System.Drawing.Point(108, 22);
this.btnStockSale.Name = "btnStockSale";
this.btnStockSale.Size = new System.Drawing.Size(96, 23);
this.btnStockSale.TabIndex = 2;
@ -165,9 +198,8 @@ namespace StockInfo
//
// btnValueView
//
this.btnValueView.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnValueView.Location = new System.Drawing.Point(7, 39);
this.btnValueView.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnValueView.Location = new System.Drawing.Point(7, 51);
this.btnValueView.Name = "btnValueView";
this.btnValueView.Size = new System.Drawing.Size(95, 23);
this.btnValueView.TabIndex = 1;
@ -177,9 +209,8 @@ namespace StockInfo
//
// btnStockReg
//
this.btnStockReg.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.btnStockReg.Location = new System.Drawing.Point(6, 9);
this.btnStockReg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.btnStockReg.Location = new System.Drawing.Point(6, 22);
this.btnStockReg.Name = "btnStockReg";
this.btnStockReg.Size = new System.Drawing.Size(96, 23);
this.btnStockReg.TabIndex = 0;
@ -187,11 +218,15 @@ namespace StockInfo
this.btnStockReg.UseVisualStyleBackColor = true;
this.btnStockReg.Click += new System.EventHandler(this.btnStockReg_Click);
//
// ofdOpener
//
this.ofdOpener.FileName = "openFileDialog1";
//
// frmInitial
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 520);
this.ClientSize = new System.Drawing.Size(853, 520);
this.Controls.Add(this.gbStockMgmnt);
this.Controls.Add(this.chbShowBrowser);
this.Controls.Add(this.button1);
@ -208,6 +243,7 @@ namespace StockInfo
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit();
this.gB1.ResumeLayout(false);
this.gB1.PerformLayout();
this.gbStockMgmnt.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
@ -228,6 +264,11 @@ namespace StockInfo
private System.Windows.Forms.Button btnStockReg;
private System.Windows.Forms.Button btnValueView;
private System.Windows.Forms.Button btnStockSale;
private System.Windows.Forms.CheckBox chkEnableBackRes;
private System.Windows.Forms.Button btnRestoreShares;
private System.Windows.Forms.Button btnBackupShares;
private System.Windows.Forms.SaveFileDialog sfdSaver;
private System.Windows.Forms.OpenFileDialog ofdOpener;
}
}

View File

@ -10,6 +10,8 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.Json;
using System.IO;
namespace StockInfo
{
@ -32,6 +34,8 @@ namespace StockInfo
private void Form1_Load(object sender, EventArgs e)
{
ReloadData();
btnRestoreShares.Enabled = false;
btnBackupShares.Enabled = false;
}
private void ReloadData()
@ -101,5 +105,47 @@ namespace StockInfo
ReloadData();
}
private void chkEnableBackRes_CheckedChanged(object sender, EventArgs e)
{
if (chkEnableBackRes.Checked)
{
btnRestoreShares.Enabled = true;
btnBackupShares.Enabled = true;
}
else
{
btnRestoreShares.Enabled = false;
btnBackupShares.Enabled = false;
}
}
private void btnBackupShares_Click(object sender, EventArgs e)
{
var stockList = _stockRepository.GetAllStocks();
var jsonStr = JsonSerializer.Serialize(stockList);
sfdSaver.Title = "Backup your sharedata";
sfdSaver.Filter = "Backup file (.json)|*.json";
sfdSaver.FileName = $"StockBackup{DateTime.Now.ToFileTime()}";
if (sfdSaver.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(sfdSaver.FileName, jsonStr);
}
}
private void btnRestoreShares_Click(object sender, EventArgs e)
{
ofdOpener.Title = "Restore your share data";
ofdOpener.Filter = "Restore file (.json)|*.json";
ofdOpener.CheckFileExists = true;
if (ofdOpener.ShowDialog() == DialogResult.OK)
{
var jsonStr = File.ReadAllText(ofdOpener.FileName);
var stockList = JsonSerializer.Deserialize<List<StockMember>>(jsonStr);
_stockRepository.RestoreStockMembers(stockList);
};
}
}
}

View File

@ -57,4 +57,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="$this.TrayHeight" type="System.Int32, System.Private.CoreLib, Version=5.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e">
<value>27</value>
</metadata>
</root>