From 03878ae8af36026f8ead716f30851a41684b5a6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sun, 7 Mar 2021 15:57:33 +0100 Subject: [PATCH] =?UTF-8?q?Backup=20/=20Restore=20funktioner=20inf=C3=B6rt?= =?UTF-8?q?=20f=C3=B6r=20kommande=20uppdateringar?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- StockBackup132595878779447701.json | 1 + StockBackup132596024522676307.json | 1 + StockDAL/StockRepository.cs | 40 ++++++++++ StockDal.Interface/IStockRepository.cs | 2 + StockInfo/frmInitial.Designer.cs | 105 +++++++++++++++++-------- StockInfo/frmInitial.cs | 48 ++++++++++- StockInfo/frmInitial.resx | 3 + 7 files changed, 167 insertions(+), 33 deletions(-) create mode 100644 StockBackup132595878779447701.json create mode 100644 StockBackup132596024522676307.json diff --git a/StockBackup132595878779447701.json b/StockBackup132595878779447701.json new file mode 100644 index 0000000..6f991f2 --- /dev/null +++ b/StockBackup132595878779447701.json @@ -0,0 +1 @@ +[{"Id":2,"StockId":"Ericsson A","StockExtId":"Ericsson A","BuyValue":107.4,"BuyDate":"2021-01-15T00:00:00","ActValue":108.4,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":3,"StockId":"NENT Group B","StockExtId":"NENT Group B","BuyValue":413.0,"BuyDate":"2021-02-09T00:00:00","ActValue":386.0,"ActDate":"2021-03-01T17:29:00","ActAmount":0,"SoldValue":19506,"SoldDate":"2021-02-12T00:00:00","Comment":"","PostAmount":45},{"Id":4,"StockId":"Platzer Fastigheter Holding B","StockExtId":"Platzer Fastigheter Holding B","BuyValue":103.8,"BuyDate":"2021-02-15T00:00:00","ActValue":108.0,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":5,"StockId":"Poolia B","StockExtId":"Poolia B","BuyValue":6.46,"BuyDate":"2021-02-19T00:00:00","ActValue":8.36,"ActDate":"2021-03-07T00:00:00","ActAmount":200,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":200},{"Id":6,"StockId":"Latour B","StockExtId":"Latour B","BuyValue":192.5,"BuyDate":"2021-01-15T00:00:00","ActValue":202.0,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":7,"StockId":"ABB","StockExtId":"ABB","BuyValue":242.4,"BuyDate":"2021-02-10T00:00:00","ActValue":247.8,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":8,"StockId":"Tele2 B","StockExtId":"Tele2 B","BuyValue":106.55,"BuyDate":"2021-02-23T00:00:00","ActValue":110.9,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":9,"StockId":"SAS","StockExtId":"SAS","BuyValue":1.702,"BuyDate":"2021-01-15T00:00:00","ActValue":1.92,"ActDate":"2021-03-07T00:00:00","ActAmount":1000,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":1000},{"Id":10,"StockId":"Holmen A","StockExtId":"Holmen A","BuyValue":399.0,"BuyDate":"2021-02-01T00:00:00","ActValue":398.0,"ActDate":"2021-03-07T00:00:00","ActAmount":5,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":5},{"Id":11,"StockId":"Telia Company","StockExtId":"Telia Company","BuyValue":34.95,"BuyDate":"2021-02-18T00:00:00","ActValue":34.84,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":12,"StockId":"Unibap","StockExtId":"Unibap","BuyValue":36.4,"BuyDate":"2021-01-19T00:00:00","ActValue":35.5,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":13,"StockId":"Holmen A","StockExtId":"Holmen A","BuyValue":431.0,"BuyDate":"2021-01-25T00:00:00","ActValue":398.0,"ActDate":"2021-03-07T00:00:00","ActAmount":10,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":10},{"Id":14,"StockId":"NENT Group B","StockExtId":"NENT Group B","BuyValue":418.0,"BuyDate":"2021-02-18T00:00:00","ActValue":390.8,"ActDate":"2021-03-07T00:00:00","ActAmount":10,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":10},{"Id":15,"StockId":"Holmen A","StockExtId":"Holmen A","BuyValue":431.0,"BuyDate":"2021-01-22T00:00:00","ActValue":398.0,"ActDate":"2021-03-07T00:00:00","ActAmount":20,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":20},{"Id":16,"StockId":"Swedbank A","StockExtId":"Swedbank A","BuyValue":164.16,"BuyDate":"2021-01-26T00:00:00","ActValue":156.8,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":17,"StockId":"Betsson B","StockExtId":"Betsson B","BuyValue":84.7,"BuyDate":"2021-01-18T00:00:00","ActValue":69.1,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":18,"StockId":"NENT Group B","StockExtId":"NENT Group B","BuyValue":477.4,"BuyDate":"2021-01-20T00:00:00","ActValue":390.8,"ActDate":"2021-03-07T00:00:00","ActAmount":10,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":10},{"Id":19,"StockId":"Hufvudstaden A","StockExtId":"Hufvudstaden A","BuyValue":132.1,"BuyDate":"2021-01-18T00:00:00","ActValue":121.3,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":20,"StockId":"Sweco A","StockExtId":"Sweco A","BuyValue":136.365,"BuyDate":"2021-02-17T00:00:00","ActValue":128.5,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":21,"StockId":"NENT Group B","StockExtId":"NENT Group B","BuyValue":435.1133,"BuyDate":"2021-02-01T00:00:00","ActValue":390.8,"ActDate":"2021-03-07T00:00:00","ActAmount":30,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":30},{"Id":22,"StockId":"Sagax D","StockExtId":"Sagax D","BuyValue":31.95,"BuyDate":"2021-03-04T00:00:00","ActValue":31.85,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":23,"StockId":"Handelsbanken A","StockExtId":"Handelsbanken A","BuyValue":94.04,"BuyDate":"2021-03-05T00:00:00","ActValue":94.58,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50}] \ No newline at end of file diff --git a/StockBackup132596024522676307.json b/StockBackup132596024522676307.json new file mode 100644 index 0000000..8a47d3a --- /dev/null +++ b/StockBackup132596024522676307.json @@ -0,0 +1 @@ +[{"Id":46,"StockId":"Handelsbanken A","StockExtId":"Handelsbanken A","BuyValue":94.04,"BuyDate":"2021-03-05T00:00:00","ActValue":94.58,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":47,"StockId":"NENT Group B","StockExtId":"NENT Group B","BuyValue":435.1133,"BuyDate":"2021-02-01T00:00:00","ActValue":390.8,"ActDate":"2021-03-07T00:00:00","ActAmount":30,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":30},{"Id":48,"StockId":"NENT Group B","StockExtId":"NENT Group B","BuyValue":413.0,"BuyDate":"2021-02-09T00:00:00","ActValue":386.0,"ActDate":"2021-03-01T17:29:00","ActAmount":0,"SoldValue":19506.0,"SoldDate":"2021-02-12T00:00:00","Comment":"","PostAmount":45},{"Id":49,"StockId":"Platzer Fastigheter Holding B","StockExtId":"Platzer Fastigheter Holding B","BuyValue":103.8,"BuyDate":"2021-02-15T00:00:00","ActValue":108.0,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":50,"StockId":"Poolia B","StockExtId":"Poolia B","BuyValue":6.46,"BuyDate":"2021-02-19T00:00:00","ActValue":8.36,"ActDate":"2021-03-07T00:00:00","ActAmount":200,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":200},{"Id":51,"StockId":"Latour B","StockExtId":"Latour B","BuyValue":192.5,"BuyDate":"2021-01-15T00:00:00","ActValue":202.0,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":52,"StockId":"ABB","StockExtId":"ABB","BuyValue":242.4,"BuyDate":"2021-02-10T00:00:00","ActValue":247.8,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":53,"StockId":"Tele2 B","StockExtId":"Tele2 B","BuyValue":106.55,"BuyDate":"2021-02-23T00:00:00","ActValue":110.9,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":54,"StockId":"SAS","StockExtId":"SAS","BuyValue":1.702,"BuyDate":"2021-01-15T00:00:00","ActValue":1.92,"ActDate":"2021-03-07T00:00:00","ActAmount":1000,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":1000},{"Id":55,"StockId":"Holmen A","StockExtId":"Holmen A","BuyValue":399.0,"BuyDate":"2021-02-01T00:00:00","ActValue":398.0,"ActDate":"2021-03-07T00:00:00","ActAmount":5,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":5},{"Id":56,"StockId":"Sagax D","StockExtId":"Sagax D","BuyValue":31.95,"BuyDate":"2021-03-04T00:00:00","ActValue":31.85,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":57,"StockId":"Telia Company","StockExtId":"Telia Company","BuyValue":34.95,"BuyDate":"2021-02-18T00:00:00","ActValue":34.84,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":58,"StockId":"Holmen A","StockExtId":"Holmen A","BuyValue":431.0,"BuyDate":"2021-01-25T00:00:00","ActValue":398.0,"ActDate":"2021-03-07T00:00:00","ActAmount":10,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":10},{"Id":59,"StockId":"NENT Group B","StockExtId":"NENT Group B","BuyValue":418.0,"BuyDate":"2021-02-18T00:00:00","ActValue":390.8,"ActDate":"2021-03-07T00:00:00","ActAmount":10,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":10},{"Id":60,"StockId":"Holmen A","StockExtId":"Holmen A","BuyValue":431.0,"BuyDate":"2021-01-22T00:00:00","ActValue":398.0,"ActDate":"2021-03-07T00:00:00","ActAmount":20,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":20},{"Id":61,"StockId":"Swedbank A","StockExtId":"Swedbank A","BuyValue":164.16,"BuyDate":"2021-01-26T00:00:00","ActValue":156.8,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":62,"StockId":"Betsson B","StockExtId":"Betsson B","BuyValue":84.7,"BuyDate":"2021-01-18T00:00:00","ActValue":69.1,"ActDate":"2021-03-07T00:00:00","ActAmount":50,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":50},{"Id":63,"StockId":"NENT Group B","StockExtId":"NENT Group B","BuyValue":477.4,"BuyDate":"2021-01-20T00:00:00","ActValue":390.8,"ActDate":"2021-03-07T00:00:00","ActAmount":10,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":10},{"Id":64,"StockId":"Hufvudstaden A","StockExtId":"Hufvudstaden A","BuyValue":132.1,"BuyDate":"2021-01-18T00:00:00","ActValue":121.3,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":65,"StockId":"Sweco A","StockExtId":"Sweco A","BuyValue":136.365,"BuyDate":"2021-02-17T00:00:00","ActValue":128.5,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":66,"StockId":"Unibap","StockExtId":"Unibap","BuyValue":36.4,"BuyDate":"2021-01-19T00:00:00","ActValue":35.5,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100},{"Id":67,"StockId":"Ericsson A","StockExtId":"Ericsson A","BuyValue":107.4,"BuyDate":"2021-01-15T00:00:00","ActValue":108.4,"ActDate":"2021-03-07T00:00:00","ActAmount":100,"SoldValue":0.0,"SoldDate":null,"Comment":"","PostAmount":100}] \ No newline at end of file diff --git a/StockDAL/StockRepository.cs b/StockDAL/StockRepository.cs index d03cc93..6d1a292 100644 --- a/StockDAL/StockRepository.cs +++ b/StockDAL/StockRepository.cs @@ -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 stockMembers) { using var context = new StockContext(); @@ -71,5 +78,38 @@ namespace StockDAL context.SaveChanges(); } + public void RestoreStockMembers(List 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); + } + } } } diff --git a/StockDal.Interface/IStockRepository.cs b/StockDal.Interface/IStockRepository.cs index 442715f..cdb4a88 100644 --- a/StockDal.Interface/IStockRepository.cs +++ b/StockDal.Interface/IStockRepository.cs @@ -12,6 +12,8 @@ namespace StockDAL.Interface IEnumerable GetAllRemainingStocks(); IEnumerable GetAllStocks(); void InsertMany(List stockMembers); + void RemoveAllStocks(); + void RestoreStockMembers(List stockMembers); void SaveStockMember(StockMember stockMember); void UpdateActualForSell(int id, int sellAmount, decimal sellPrice, DateTime sellDate); void UpdateActualPrice(int id, decimal price); diff --git a/StockInfo/frmInitial.Designer.cs b/StockInfo/frmInitial.Designer.cs index f83f32c..90924a9 100644 --- a/StockInfo/frmInitial.Designer.cs +++ b/StockInfo/frmInitial.Designer.cs @@ -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; } } diff --git a/StockInfo/frmInitial.cs b/StockInfo/frmInitial.cs index 33694ae..e8a9fad 100644 --- a/StockInfo/frmInitial.cs +++ b/StockInfo/frmInitial.cs @@ -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() @@ -86,7 +90,7 @@ namespace StockInfo { Cursor.Current = Cursors.WaitCursor; _stockMarketRepository.LoadStockMarketList(); - stockWindow = new frmMyStocks( _stockRepository, _stockMarketRepository); + stockWindow = new frmMyStocks(_stockRepository, _stockMarketRepository); stockWindow.Stocks = _stockMarketRepository.StockMarketList; Cursor.Current = DefaultCursor; stockWindow.ShowDialog(); @@ -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>(jsonStr); + + _stockRepository.RestoreStockMembers(stockList); + }; + } } } diff --git a/StockInfo/frmInitial.resx b/StockInfo/frmInitial.resx index f298a7b..a28f390 100644 --- a/StockInfo/frmInitial.resx +++ b/StockInfo/frmInitial.resx @@ -57,4 +57,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + 27 + \ No newline at end of file