From 5f0c4ef9901603651ecb63e70875036947080a3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Tue, 1 Feb 2022 21:15:31 +0100 Subject: [PATCH] New projct BrowserDriver infered to help with webscraping --- BrowserHelper/BrowserHelper.csproj | 14 + BrowserHelper/Driver/BrowserDriver.cs | 33 ++ BrowserHelper/Driver/DriverFixture.cs | 40 ++ BrowserHelper/Driver/IBrowserDriver.cs | 9 + BrowserHelper/Driver/IDriverFixture.cs | 10 + .../Extensions/HtmlTableExtension.cs | 248 ++++++++++++ .../WebDriverInitializerExtension.cs | 39 ++ .../Extensions/WebElementExtension.cs | 35 ++ BrowserHelper/IMessages.cs | 8 + BrowserHelper/Messages.cs | 14 + BrowserHelper/Settings/TestSettings.cs | 15 + HandleNewWindows/HandleNewWindows.csproj | 25 -- HandleNewWindows/Program.cs | 23 -- HandleNewWindows/frmEditStock.Designer.cs | 381 ------------------ HandleNewWindows/frmEditStock.cs | 153 ------- HandleNewWindows/frmEditStock.resx | 60 --- StockInfoCoreApp.sln | 14 +- 17 files changed, 472 insertions(+), 649 deletions(-) create mode 100644 BrowserHelper/BrowserHelper.csproj create mode 100644 BrowserHelper/Driver/BrowserDriver.cs create mode 100644 BrowserHelper/Driver/DriverFixture.cs create mode 100644 BrowserHelper/Driver/IBrowserDriver.cs create mode 100644 BrowserHelper/Driver/IDriverFixture.cs create mode 100644 BrowserHelper/Extensions/HtmlTableExtension.cs create mode 100644 BrowserHelper/Extensions/WebDriverInitializerExtension.cs create mode 100644 BrowserHelper/Extensions/WebElementExtension.cs create mode 100644 BrowserHelper/IMessages.cs create mode 100644 BrowserHelper/Messages.cs create mode 100644 BrowserHelper/Settings/TestSettings.cs delete mode 100644 HandleNewWindows/HandleNewWindows.csproj delete mode 100644 HandleNewWindows/Program.cs delete mode 100644 HandleNewWindows/frmEditStock.Designer.cs delete mode 100644 HandleNewWindows/frmEditStock.cs delete mode 100644 HandleNewWindows/frmEditStock.resx diff --git a/BrowserHelper/BrowserHelper.csproj b/BrowserHelper/BrowserHelper.csproj new file mode 100644 index 0000000..974a7c8 --- /dev/null +++ b/BrowserHelper/BrowserHelper.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/BrowserHelper/Driver/BrowserDriver.cs b/BrowserHelper/Driver/BrowserDriver.cs new file mode 100644 index 0000000..d83aaed --- /dev/null +++ b/BrowserHelper/Driver/BrowserDriver.cs @@ -0,0 +1,33 @@ +using OpenQA.Selenium; +using OpenQA.Selenium.Chrome; +using OpenQA.Selenium.Firefox; +using WebDriverManager; +using WebDriverManager.DriverConfigs.Impl; + +namespace BrowserHelper.Driver; +public class BrowserDriver : IBrowserDriver +{ + public IWebDriver GetChromeDriver(bool headless = true) + { + new DriverManager().SetUpDriver(new ChromeConfig()); + ChromeOptions options = new ChromeOptions(); + if (headless) + options.AddArguments("--headless"); + return new ChromeDriver(options); + } + + public IWebDriver GetFirefoxDriver(bool headless = true) + { + new DriverManager().SetUpDriver(new FirefoxConfig()); + FirefoxOptions options = new FirefoxOptions(); + if (headless) + options.AddArguments("--headless"); + return new FirefoxDriver(options); + } +} + +public enum BrowserType +{ + Chrome, + Firefox +} \ No newline at end of file diff --git a/BrowserHelper/Driver/DriverFixture.cs b/BrowserHelper/Driver/DriverFixture.cs new file mode 100644 index 0000000..e30ae5c --- /dev/null +++ b/BrowserHelper/Driver/DriverFixture.cs @@ -0,0 +1,40 @@ +using BrowserHelper.Settings; +using OpenQA.Selenium; +using System; + +namespace BrowserHelper.Driver +{ + + public class DriverFixture : IDriverFixture, IDisposable + { + IWebDriver driver; + private readonly TestSettings _testSettings; + private readonly IBrowserDriver _browserDriver; + + public DriverFixture(TestSettings testSettings, IBrowserDriver browserDriver) + { + _testSettings = testSettings; + _browserDriver = browserDriver; + driver = GetWebDriver(); + driver.Navigate().GoToUrl(_testSettings.ApplicationUrl); + + } + + public IWebDriver Driver => driver; + + public IWebDriver GetWebDriver() + { + return _testSettings.BrowserType switch + { + BrowserType.Chrome => _browserDriver.GetChromeDriver(_testSettings.Headless), + BrowserType.Firefox => _browserDriver.GetFirefoxDriver(_testSettings.Headless), + _ => _browserDriver.GetChromeDriver() + }; + } + + public void Dispose() + { + driver.Quit(); + } + } +} diff --git a/BrowserHelper/Driver/IBrowserDriver.cs b/BrowserHelper/Driver/IBrowserDriver.cs new file mode 100644 index 0000000..72c7a5d --- /dev/null +++ b/BrowserHelper/Driver/IBrowserDriver.cs @@ -0,0 +1,9 @@ +using OpenQA.Selenium; + +namespace BrowserHelper.Driver; + +public interface IBrowserDriver +{ + IWebDriver GetChromeDriver(bool headless = true); + IWebDriver GetFirefoxDriver(bool headless = true); +} diff --git a/BrowserHelper/Driver/IDriverFixture.cs b/BrowserHelper/Driver/IDriverFixture.cs new file mode 100644 index 0000000..576b022 --- /dev/null +++ b/BrowserHelper/Driver/IDriverFixture.cs @@ -0,0 +1,10 @@ +using OpenQA.Selenium; + +namespace BrowserHelper.Driver +{ + public interface IDriverFixture + { + IWebDriver Driver { get; } + + } +} diff --git a/BrowserHelper/Extensions/HtmlTableExtension.cs b/BrowserHelper/Extensions/HtmlTableExtension.cs new file mode 100644 index 0000000..2ecf248 --- /dev/null +++ b/BrowserHelper/Extensions/HtmlTableExtension.cs @@ -0,0 +1,248 @@ +using OpenQA.Selenium; +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BrowserHelper.Extensions +{ + public static class HtmlTableExtension + { + //Read of Table + private static List ReadTable(IWebElement table) + { + var tableDataCollection = new List(); + var columns = table.FindElements(By.TagName("th")); + var rows = table.FindElements(By.TagName("tr")); + int rowIndex = 0; + foreach (var row in rows) + { + int colIndex = 0; + var colDatas = row.FindElements(By.TagName("td")); + if (colDatas.Count != 0) + foreach (var colValue in colDatas) + { + tableDataCollection.Add(new TableDataCollection + { + RowNumber = rowIndex, + ColumnName = columns[colIndex].Text != "" ? + columns[colIndex].Text : colIndex.ToString(), + ColumnValue = colValue.Text, + ColumnSpecialValue = GetControl(colValue) + }); + colIndex++; + } + rowIndex++; + } + return tableDataCollection; + } + + private static List ReadTablePartly(IWebElement table, string[] shares, string[] shareHeaders) + { + var tableDataCollection = new List(); + var columns = table.FindElements(By.TagName("th")); + var rows = table.FindElements(By.TagName("tr")); + var antal = shares.Length; + int rowIndex = 0; + foreach (var row in rows) + { + int colIndex = 0; + var colDatas = row.FindElements(By.TagName("td")); + if (colDatas.Count != 0 && shares.Contains(colDatas[0].Text)) + { + if (colDatas.Count != 0) + foreach (var colValue in colDatas) + { + if (shareHeaders.Contains(columns[colIndex].Text)) + { + tableDataCollection.Add(new TableDataCollection + { + RowNumber = rowIndex, + ColumnName = columns[colIndex].Text != "" ? + columns[colIndex].Text : colIndex.ToString(), + ColumnValue = colValue.Text, + ColumnSpecialValue = GetControl(colValue) + }); + } + colIndex++; + } + rowIndex++; + antal--; + } + if (antal < 1) break; + } + return tableDataCollection; + } + + + private static ColumnSpecialValue GetControl(IWebElement columnValue) + { + ColumnSpecialValue? columnSpecialValue = null; + if (columnValue.FindElements(By.TagName("a")).Count > 0) + { + columnSpecialValue = new ColumnSpecialValue + { + ElementCollection = columnValue.FindElements(By.TagName("a")), + ControlType = ControlType.hyperlink + }; + } + if (columnValue.FindElements(By.TagName("input")).Count > 0) + { + columnSpecialValue = new ColumnSpecialValue + { + ElementCollection = columnValue.FindElements(By.TagName("input")), + ControlType = ControlType.input + }; + } + return columnSpecialValue; + } + + public static List ReadHandledStock(this IWebElement table) + { + var tableDataCollection = new List(); + var columns = table.FindElements(By.TagName("th")); + var rows = table.FindElements(By.TagName("tr")); + var antal = rows.Count; + int rowIndex = 0; + foreach (var row in rows) + { + var colDatas = row.FindElements(By.TagName("td")); + if (colDatas.Count != 0) + { + if (colDatas.Count != 0) + tableDataCollection.Add(new TableDataCollection + { + RowNumber = rowIndex, + ColumnName = columns[0].Text, + ColumnValue = colDatas[0].Text, + ColumnSpecialValue = null + }); + } + rowIndex++; + antal--; + if (antal < 0) break; + } + + return tableDataCollection; + } + + + public static List GetCertainStocks(this IWebElement element, string[] shares, string[] shareHeaders) + { + List shareList = new List(); + var reducedList = ReadTablePartly(element, shares, shareHeaders); + int rNr = -1; + Share Aktie = null; + foreach (var shareRow in reducedList) + { + if (shareRow.RowNumber != rNr) + { + if (Aktie != null) + { + shareList.Add(Aktie); + } + Aktie = new(); + rNr = shareRow.RowNumber; + } + switch (shareRow.ColumnName) + { + case "Aktie": + { + Aktie.AktieNamn = shareRow.ColumnValue; + break; + } + case "Senast": + { + Aktie.SenastePris = Convert.ToDecimal(shareRow.ColumnValue); + break; + } + case "Tid": + { + Aktie.RegTime = TimeOnly.Parse(shareRow.ColumnValue); + break; + } + } + } + shareList.Add(Aktie); + return shareList; + } + + + public static void PerformActionOnCell(this IWebElement element, string targetColumnIndex, string refColumnName, string refColumnValue, string controlToOperate = null) + { + var table = ReadTable(element); + + foreach (int rowNumber in GetDynamicRowNumber(table, refColumnName, refColumnValue)) + { + var cell = (from e in table + where e.ColumnName == targetColumnIndex && e.RowNumber == rowNumber + select e.ColumnSpecialValue).SingleOrDefault(); + + if (controlToOperate != null && cell != null) + { + IWebElement elementToClick = null; + + if (cell.ControlType == ControlType.hyperlink) + { + elementToClick = (from c in cell.ElementCollection + where c.Text == controlToOperate.ToString() + select c).SingleOrDefault(); + } + if (cell.ControlType == ControlType.input) + { + elementToClick = (from c in cell.ElementCollection + where c.GetAttribute("value") == controlToOperate.ToString() + select c).SingleOrDefault(); + + } + elementToClick?.Click(); + } + else + { + cell.ElementCollection?.First().Click(); + } + } + + } + + private static IEnumerable GetDynamicRowNumber(List tableCollection, string columnName, string columnValue) + { + foreach (var table in tableCollection) + { + if (table.ColumnName == columnName && table.ColumnValue == columnValue) + yield return table.RowNumber; + } + } + } + + public class TableDataCollection + { + public int RowNumber { get; set; } + public string? ColumnName { get; set; } + public string? ColumnValue { get; set; } + public ColumnSpecialValue? ColumnSpecialValue { get; set; } + } + + public class ColumnSpecialValue + { + public IEnumerable? ElementCollection { get; set; } + public ControlType? ControlType { get; set; } + } + + public enum ControlType + { + hyperlink, + input, + option, + select + } + + public class Share + { + public string AktieNamn { get; set; } + public decimal SenastePris { get; set; } + public TimeOnly RegTime { get; set; } + } +} diff --git a/BrowserHelper/Extensions/WebDriverInitializerExtension.cs b/BrowserHelper/Extensions/WebDriverInitializerExtension.cs new file mode 100644 index 0000000..cb1c7d5 --- /dev/null +++ b/BrowserHelper/Extensions/WebDriverInitializerExtension.cs @@ -0,0 +1,39 @@ +using BrowserHelper.Driver; +using Microsoft.Extensions.DependencyInjection; +using System.IO; +using System.Reflection; +using System.Text.Json; +using System.Text.Json.Serialization; +using BrowserHelper.Settings; + +namespace BrowserHelper.Extensions; +public static class WebDriverInitializerExtension +{ + public static IServiceCollection UseWebDriverInitializer( + this IServiceCollection services) + { + services.AddSingleton(ReadConfig()); + + return services; + } + + private static TestSettings ReadConfig() + { + var configFile = File + .ReadAllText(Path.GetDirectoryName( + Assembly.GetExecutingAssembly().Location) + + "/appsettings.json"); + + var jsonSerializeOptions = new JsonSerializerOptions() + { + PropertyNameCaseInsensitive = true + }; + + jsonSerializeOptions.Converters.Add(new JsonStringEnumConverter()); + + var testSettings = JsonSerializer.Deserialize(configFile, jsonSerializeOptions); + + return testSettings; + } +} + diff --git a/BrowserHelper/Extensions/WebElementExtension.cs b/BrowserHelper/Extensions/WebElementExtension.cs new file mode 100644 index 0000000..757dc9a --- /dev/null +++ b/BrowserHelper/Extensions/WebElementExtension.cs @@ -0,0 +1,35 @@ +using OpenQA.Selenium; +using OpenQA.Selenium.Support.UI; + +namespace BrowserHelper.Extensions +{ + public static class WebElementExtension + { + + public static void SelectDropDownByText(this IWebElement element, string text) + { + var select = new SelectElement(element); + select.SelectByText(text); + } + public static void SelectDropDownByValue(this IWebElement element, string value) + { + var select = new SelectElement(element); + select.SelectByValue(value); + } + public static void SelectDropDownByIndex(this IWebElement element, int index) + { + var select = new SelectElement(element); + select.SelectByIndex(index); + } + public static void ClearAndEnterText(this IWebElement element, string value) + { + element.Clear(); + element.SendKeys(value); + } + + public static string GetSelectedDropDownValue(this IWebElement element) + { + return new SelectElement(element).SelectedOption.Text; + } + } +} diff --git a/BrowserHelper/IMessages.cs b/BrowserHelper/IMessages.cs new file mode 100644 index 0000000..b34c3d2 --- /dev/null +++ b/BrowserHelper/IMessages.cs @@ -0,0 +1,8 @@ +namespace BrowserHelper +{ + public interface IMessages + { + string SayGoodBye(); + string SayHello(); + } +} \ No newline at end of file diff --git a/BrowserHelper/Messages.cs b/BrowserHelper/Messages.cs new file mode 100644 index 0000000..c1f4fc2 --- /dev/null +++ b/BrowserHelper/Messages.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BrowserHelper +{ + public class Messages : IMessages + { + public string SayHello() => "Hello Viewer"; + public string SayGoodBye() => "GoddBye, farewell, and good day!"; + } +} diff --git a/BrowserHelper/Settings/TestSettings.cs b/BrowserHelper/Settings/TestSettings.cs new file mode 100644 index 0000000..978b76d --- /dev/null +++ b/BrowserHelper/Settings/TestSettings.cs @@ -0,0 +1,15 @@ +using System; +using BrowserHelper.Driver; + +namespace BrowserHelper.Settings +{ + public class TestSettings + { + public BrowserType BrowserType { get; set; } + public bool Headless { get; set; } + public Uri ApplicationUrl { get; set; } + public int TimeoutInterval { get; set; } + public string[] StockWishes { get; set; } + public string[] StocWishCols { get; set; } + } +} diff --git a/HandleNewWindows/HandleNewWindows.csproj b/HandleNewWindows/HandleNewWindows.csproj deleted file mode 100644 index f202d6a..0000000 --- a/HandleNewWindows/HandleNewWindows.csproj +++ /dev/null @@ -1,25 +0,0 @@ - - - - WinExe - net5.0-windows - true - - - - - - - - - - - - - - - Form - - - - \ No newline at end of file diff --git a/HandleNewWindows/Program.cs b/HandleNewWindows/Program.cs deleted file mode 100644 index 14cb0b2..0000000 --- a/HandleNewWindows/Program.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace HandleNewWindows -{ - static class Program - { - /// - /// The main entry point for the application. - /// - [STAThread] - static void Main() - { - Application.SetHighDpiMode(HighDpiMode.SystemAware); - Application.EnableVisualStyles(); - Application.SetCompatibleTextRenderingDefault(false); - Application.Run(new Form1()); - } - } -} diff --git a/HandleNewWindows/frmEditStock.Designer.cs b/HandleNewWindows/frmEditStock.Designer.cs deleted file mode 100644 index dad1b6c..0000000 --- a/HandleNewWindows/frmEditStock.Designer.cs +++ /dev/null @@ -1,381 +0,0 @@ - -namespace StockInfoCore -{ - partial class frmEditStock - { - /// - /// Required designer variable. - /// - private System.ComponentModel.IContainer components = null; - - /// - /// Clean up any resources being used. - /// - /// true if managed resources should be disposed; otherwise, false. - protected override void Dispose(bool disposing) - { - if (disposing && (components != null)) - { - components.Dispose(); - } - base.Dispose(disposing); - } - - #region Windows Form Designer generated code - - /// - /// Required method for Designer support - do not modify - /// the contents of this method with the code editor. - /// - private void InitializeComponent() - { - this.btnClose = new System.Windows.Forms.Button(); - this.lblStockExtId = new System.Windows.Forms.Label(); - this.txtStockExtId = new System.Windows.Forms.TextBox(); - this.txtBuyPrice = new System.Windows.Forms.TextBox(); - this.lblBuyPrice = new System.Windows.Forms.Label(); - this.txtBuyDate = new System.Windows.Forms.TextBox(); - this.lblBuyDate = new System.Windows.Forms.Label(); - this.txtBoughtAmount = new System.Windows.Forms.TextBox(); - this.lblBoughtAmount = new System.Windows.Forms.Label(); - this.txtActValue = new System.Windows.Forms.TextBox(); - this.lblActValue = new System.Windows.Forms.Label(); - this.txtActDate = new System.Windows.Forms.TextBox(); - this.lblActDate = new System.Windows.Forms.Label(); - this.txtActAmount = new System.Windows.Forms.TextBox(); - this.lblRemaining = new System.Windows.Forms.Label(); - this.txtSoldPrice = new System.Windows.Forms.TextBox(); - this.lblSoldValue = new System.Windows.Forms.Label(); - this.txtSoldDate = new System.Windows.Forms.TextBox(); - this.lblSoldDate = new System.Windows.Forms.Label(); - this.txtComment = new System.Windows.Forms.TextBox(); - this.lblComment = new System.Windows.Forms.Label(); - this.btnSaveToDB = new System.Windows.Forms.Button(); - this.groupBox1 = new System.Windows.Forms.GroupBox(); - this.txtLatestSoldDate = new System.Windows.Forms.TextBox(); - this.label2 = new System.Windows.Forms.Label(); - this.txtLatestSoldPrice = new System.Windows.Forms.TextBox(); - this.label1 = new System.Windows.Forms.Label(); - this.txtStockName = new System.Windows.Forms.TextBox(); - this.label3 = new System.Windows.Forms.Label(); - this.groupBox1.SuspendLayout(); - this.SuspendLayout(); - // - // btnClose - // - this.btnClose.Location = new System.Drawing.Point(437, 414); - this.btnClose.Name = "btnClose"; - this.btnClose.Size = new System.Drawing.Size(75, 23); - this.btnClose.TabIndex = 1; - this.btnClose.Text = "Close"; - this.btnClose.UseVisualStyleBackColor = true; - this.btnClose.Click += new System.EventHandler(this.btnClose_Click); - // - // lblStockExtId - // - this.lblStockExtId.AutoSize = true; - this.lblStockExtId.Location = new System.Drawing.Point(38, 44); - this.lblStockExtId.Name = "lblStockExtId"; - this.lblStockExtId.Size = new System.Drawing.Size(62, 15); - this.lblStockExtId.TabIndex = 2; - this.lblStockExtId.Text = "StockExtId"; - // - // txtStockExtId - // - this.txtStockExtId.Location = new System.Drawing.Point(153, 41); - this.txtStockExtId.Name = "txtStockExtId"; - this.txtStockExtId.Size = new System.Drawing.Size(269, 23); - this.txtStockExtId.TabIndex = 3; - // - // txtBuyPrice - // - this.txtBuyPrice.Location = new System.Drawing.Point(153, 104); - this.txtBuyPrice.Name = "txtBuyPrice"; - this.txtBuyPrice.Size = new System.Drawing.Size(111, 23); - this.txtBuyPrice.TabIndex = 5; - // - // lblBuyPrice - // - this.lblBuyPrice.AutoSize = true; - this.lblBuyPrice.Location = new System.Drawing.Point(38, 107); - this.lblBuyPrice.Name = "lblBuyPrice"; - this.lblBuyPrice.Size = new System.Drawing.Size(56, 15); - this.lblBuyPrice.TabIndex = 4; - this.lblBuyPrice.Text = "Buy price"; - // - // txtBuyDate - // - this.txtBuyDate.Location = new System.Drawing.Point(153, 133); - this.txtBuyDate.Name = "txtBuyDate"; - this.txtBuyDate.Size = new System.Drawing.Size(142, 23); - this.txtBuyDate.TabIndex = 7; - // - // lblBuyDate - // - this.lblBuyDate.AutoSize = true; - this.lblBuyDate.Location = new System.Drawing.Point(38, 136); - this.lblBuyDate.Name = "lblBuyDate"; - this.lblBuyDate.Size = new System.Drawing.Size(73, 15); - this.lblBuyDate.TabIndex = 6; - this.lblBuyDate.Text = "Bought Date"; - // - // txtBoughtAmount - // - this.txtBoughtAmount.Location = new System.Drawing.Point(153, 162); - this.txtBoughtAmount.Name = "txtBoughtAmount"; - this.txtBoughtAmount.Size = new System.Drawing.Size(111, 23); - this.txtBoughtAmount.TabIndex = 9; - this.txtBoughtAmount.TextChanged += new System.EventHandler(this.txtBoughtAmount_TextChanged); - // - // lblBoughtAmount - // - this.lblBoughtAmount.AutoSize = true; - this.lblBoughtAmount.Location = new System.Drawing.Point(38, 165); - this.lblBoughtAmount.Name = "lblBoughtAmount"; - this.lblBoughtAmount.Size = new System.Drawing.Size(93, 15); - this.lblBoughtAmount.TabIndex = 8; - this.lblBoughtAmount.Text = "Bought Number"; - // - // txtActValue - // - this.txtActValue.Location = new System.Drawing.Point(153, 191); - this.txtActValue.Name = "txtActValue"; - this.txtActValue.Size = new System.Drawing.Size(111, 23); - this.txtActValue.TabIndex = 11; - // - // lblActValue - // - this.lblActValue.AutoSize = true; - this.lblActValue.Location = new System.Drawing.Point(38, 194); - this.lblActValue.Name = "lblActValue"; - this.lblActValue.Size = new System.Drawing.Size(76, 15); - this.lblActValue.TabIndex = 10; - this.lblActValue.Text = "Current price"; - // - // txtActDate - // - this.txtActDate.Location = new System.Drawing.Point(153, 220); - this.txtActDate.Name = "txtActDate"; - this.txtActDate.Size = new System.Drawing.Size(142, 23); - this.txtActDate.TabIndex = 13; - // - // lblActDate - // - this.lblActDate.AutoSize = true; - this.lblActDate.Location = new System.Drawing.Point(38, 223); - this.lblActDate.Name = "lblActDate"; - this.lblActDate.Size = new System.Drawing.Size(61, 15); - this.lblActDate.TabIndex = 12; - this.lblActDate.Text = "Value date"; - // - // txtActAmount - // - this.txtActAmount.Location = new System.Drawing.Point(153, 249); - this.txtActAmount.Name = "txtActAmount"; - this.txtActAmount.Size = new System.Drawing.Size(111, 23); - this.txtActAmount.TabIndex = 15; - // - // lblRemaining - // - this.lblRemaining.AutoSize = true; - this.lblRemaining.Location = new System.Drawing.Point(38, 252); - this.lblRemaining.Name = "lblRemaining"; - this.lblRemaining.Size = new System.Drawing.Size(109, 15); - this.lblRemaining.TabIndex = 14; - this.lblRemaining.Text = "Remaining number"; - // - // txtSoldPrice - // - this.txtSoldPrice.Location = new System.Drawing.Point(153, 278); - this.txtSoldPrice.Name = "txtSoldPrice"; - this.txtSoldPrice.Size = new System.Drawing.Size(111, 23); - this.txtSoldPrice.TabIndex = 17; - // - // lblSoldValue - // - this.lblSoldValue.AutoSize = true; - this.lblSoldValue.Location = new System.Drawing.Point(38, 281); - this.lblSoldValue.Name = "lblSoldValue"; - this.lblSoldValue.Size = new System.Drawing.Size(59, 15); - this.lblSoldValue.TabIndex = 16; - this.lblSoldValue.Text = "Sold price"; - // - // txtSoldDate - // - this.txtSoldDate.Location = new System.Drawing.Point(153, 307); - this.txtSoldDate.Name = "txtSoldDate"; - this.txtSoldDate.Size = new System.Drawing.Size(142, 23); - this.txtSoldDate.TabIndex = 19; - // - // lblSoldDate - // - this.lblSoldDate.AutoSize = true; - this.lblSoldDate.Location = new System.Drawing.Point(38, 310); - this.lblSoldDate.Name = "lblSoldDate"; - this.lblSoldDate.Size = new System.Drawing.Size(57, 15); - this.lblSoldDate.TabIndex = 18; - this.lblSoldDate.Text = "Sold Date"; - // - // txtComment - // - this.txtComment.Location = new System.Drawing.Point(153, 336); - this.txtComment.Multiline = true; - this.txtComment.Name = "txtComment"; - this.txtComment.Size = new System.Drawing.Size(269, 72); - this.txtComment.TabIndex = 21; - // - // lblComment - // - this.lblComment.AutoSize = true; - this.lblComment.Location = new System.Drawing.Point(39, 339); - this.lblComment.Name = "lblComment"; - this.lblComment.Size = new System.Drawing.Size(61, 15); - this.lblComment.TabIndex = 20; - this.lblComment.Text = "Comment"; - // - // btnSaveToDB - // - this.btnSaveToDB.Location = new System.Drawing.Point(347, 414); - this.btnSaveToDB.Name = "btnSaveToDB"; - this.btnSaveToDB.Size = new System.Drawing.Size(75, 23); - this.btnSaveToDB.TabIndex = 25; - this.btnSaveToDB.Text = "Save"; - this.btnSaveToDB.UseVisualStyleBackColor = true; - this.btnSaveToDB.Click += new System.EventHandler(this.btnSaveToDB_Click); - // - // groupBox1 - // - this.groupBox1.Controls.Add(this.txtLatestSoldDate); - this.groupBox1.Controls.Add(this.label2); - this.groupBox1.Controls.Add(this.txtLatestSoldPrice); - this.groupBox1.Controls.Add(this.label1); - this.groupBox1.Location = new System.Drawing.Point(301, 81); - this.groupBox1.Name = "groupBox1"; - this.groupBox1.Size = new System.Drawing.Size(255, 83); - this.groupBox1.TabIndex = 26; - this.groupBox1.TabStop = false; - this.groupBox1.Text = "Latest Sold"; - // - // txtLatestSoldDate - // - this.txtLatestSoldDate.Location = new System.Drawing.Point(127, 52); - this.txtLatestSoldDate.Name = "txtLatestSoldDate"; - this.txtLatestSoldDate.ReadOnly = true; - this.txtLatestSoldDate.Size = new System.Drawing.Size(111, 23); - this.txtLatestSoldDate.TabIndex = 9; - // - // label2 - // - this.label2.AutoSize = true; - this.label2.Location = new System.Drawing.Point(12, 55); - this.label2.Name = "label2"; - this.label2.Size = new System.Drawing.Size(56, 15); - this.label2.TabIndex = 8; - this.label2.Text = "Sold date"; - // - // txtLatestSoldPrice - // - this.txtLatestSoldPrice.Location = new System.Drawing.Point(127, 23); - this.txtLatestSoldPrice.Name = "txtLatestSoldPrice"; - this.txtLatestSoldPrice.ReadOnly = true; - this.txtLatestSoldPrice.Size = new System.Drawing.Size(111, 23); - this.txtLatestSoldPrice.TabIndex = 7; - // - // label1 - // - this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(12, 26); - this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(59, 15); - this.label1.TabIndex = 6; - this.label1.Text = "Sold price"; - // - // txtStockName - // - this.txtStockName.Location = new System.Drawing.Point(153, 12); - this.txtStockName.Name = "txtStockName"; - this.txtStockName.Size = new System.Drawing.Size(269, 23); - this.txtStockName.TabIndex = 27; - // - // label3 - // - this.label3.AutoSize = true; - this.label3.Location = new System.Drawing.Point(38, 15); - this.label3.Name = "label3"; - this.label3.Size = new System.Drawing.Size(46, 15); - this.label3.TabIndex = 28; - this.label3.Text = "StockId"; - // - // frmEditStock - // - this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(570, 452); - this.Controls.Add(this.label3); - this.Controls.Add(this.txtStockName); - this.Controls.Add(this.groupBox1); - this.Controls.Add(this.btnSaveToDB); - this.Controls.Add(this.txtComment); - this.Controls.Add(this.lblComment); - this.Controls.Add(this.txtSoldDate); - this.Controls.Add(this.lblSoldDate); - this.Controls.Add(this.txtSoldPrice); - this.Controls.Add(this.lblSoldValue); - this.Controls.Add(this.txtActAmount); - this.Controls.Add(this.lblRemaining); - this.Controls.Add(this.txtActDate); - this.Controls.Add(this.lblActDate); - this.Controls.Add(this.txtActValue); - this.Controls.Add(this.lblActValue); - this.Controls.Add(this.txtBoughtAmount); - this.Controls.Add(this.lblBoughtAmount); - this.Controls.Add(this.txtBuyDate); - this.Controls.Add(this.lblBuyDate); - this.Controls.Add(this.txtBuyPrice); - this.Controls.Add(this.lblBuyPrice); - this.Controls.Add(this.txtStockExtId); - this.Controls.Add(this.lblStockExtId); - this.Controls.Add(this.btnClose); - this.Name = "frmEditStock"; - this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; - this.Text = "frmRegisterStock"; - this.Shown += new System.EventHandler(this.frmRegisterStock_Shown); - this.groupBox1.ResumeLayout(false); - this.groupBox1.PerformLayout(); - this.ResumeLayout(false); - this.PerformLayout(); - - } - - #endregion - private System.Windows.Forms.Button btnClose; - private System.Windows.Forms.Label lblStockExtId; - private System.Windows.Forms.TextBox txtStockExtId; - private System.Windows.Forms.TextBox txtBuyPrice; - private System.Windows.Forms.Label lblBuyPrice; - private System.Windows.Forms.TextBox txtBuyDate; - private System.Windows.Forms.Label lblBuyDate; - private System.Windows.Forms.TextBox txtBoughtAmount; - private System.Windows.Forms.Label lblBoughtAmount; - private System.Windows.Forms.TextBox txtActValue; - private System.Windows.Forms.Label lblActValue; - private System.Windows.Forms.TextBox txtActDate; - private System.Windows.Forms.Label lblActDate; - private System.Windows.Forms.TextBox txtActAmount; - private System.Windows.Forms.Label lblRemaining; - private System.Windows.Forms.TextBox txtSoldPrice; - private System.Windows.Forms.Label lblSoldValue; - private System.Windows.Forms.TextBox txtSoldDate; - private System.Windows.Forms.Label lblSoldDate; - private System.Windows.Forms.TextBox txtComment; - private System.Windows.Forms.Label lblComment; - private System.Windows.Forms.Button btnSaveToDB; - private System.Windows.Forms.GroupBox groupBox1; - private System.Windows.Forms.TextBox txtLatestSoldDate; - private System.Windows.Forms.Label label2; - private System.Windows.Forms.TextBox txtLatestSoldPrice; - private System.Windows.Forms.Label label1; - private System.Windows.Forms.TextBox txtStockName; - private System.Windows.Forms.Label label3; - } -} \ No newline at end of file diff --git a/HandleNewWindows/frmEditStock.cs b/HandleNewWindows/frmEditStock.cs deleted file mode 100644 index 1a46ffd..0000000 --- a/HandleNewWindows/frmEditStock.cs +++ /dev/null @@ -1,153 +0,0 @@ -using DataDomain; -using StockDAL.Interface; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace StockInfoCore -{ - public partial class frmEditStock : Form - { - Color hdr = Color.Red; - private readonly IStockRepository _stockRepository; - public int CurrentStockMember { get; set; } - - //public Dictionary Stocks { get; set; } - //public List RegisteredStocks { get; set; } = new List(); - - public frmEditStock(IStockRepository stockRepository) - { - InitializeComponent(); - _stockRepository = stockRepository; - } - - //private void LoadStockCombo() - //{ - // if (Stocks.Count() > 0) - // { - // foreach (var key in Stocks.Keys) - // { - // cmbStockChoser.Items.Add(key); - // } - // } - - //} - - private void btnClose_Click(object sender, EventArgs e) - { - this.Close(); - } - - private void frmRegisterStock_Shown(object sender, EventArgs e) - { - LoadStockData(); - - //LoadStockCombo(); - } - - private void LoadStockData() - { - var stockChosen = _stockRepository.GetStockMember(CurrentStockMember); - txtStockExtId.Text = stockChosen.StockExtId; - txtActValue.Text = stockChosen.ActValue.ToString(); - //txtActDate.Text = (DateTime.Today + stockChosen.ActDate.Value.TimeOfDay).ToString(); - var stockSold = _stockRepository.LatestSell(stockChosen.StockId.Trim()); - if (stockSold != null) - { - if (stockSold.LatestSoldDate != null) - { - txtLatestSoldDate.Text = stockSold.LatestSoldDate.Value.ToString(); - txtLatestSoldPrice.Text = stockSold.SoldStockPrice.ToString(); - } - else - { - txtLatestSoldDate.Text = string.Empty; - txtLatestSoldPrice.Text = string.Empty; - } - } - else - { - txtLatestSoldDate.Text = string.Empty; - txtLatestSoldPrice.Text = string.Empty; - } - - txtBuyPrice.Text = stockChosen.BuyValue.ToString(); // = decimal.Parse(string.IsNullOrEmpty(txtBuyPrice.Text) ? "0" : txtBuyPrice.Text); - txtBoughtAmount.Text = stockChosen.PostAmount.ToString(); // = long.Parse(string.IsNullOrEmpty(txtBoughtAmount.Text) ? "0" : txtBoughtAmount.Text); - txtActDate.Text = stockChosen.ActDate.ToString(); // = DateTime.Parse(txtActDate.Text); - txtBuyDate.Text = stockChosen.BuyDate.ToString(); // = string.IsNullOrWhiteSpace(txtBuyDate.Text) ? DateTime.Today : DateTime.Parse(txtBuyDate.Text); - txtActValue.Text = stockChosen.ActValue.ToString(); // = decimal.Parse(string.IsNullOrEmpty(txtActValue.Text) ? "0" : txtActValue.Text); - txtActAmount.Text = stockChosen.ActAmount.ToString(); // = long.Parse(string.IsNullOrEmpty(txtActAmount.Text) ? "0" : txtActAmount.Text); - txtSoldDate.Text = stockChosen.SoldDate.ToString(); // = null; //DateTime.MaxValue; - txtSoldPrice.Text = stockChosen.SoldValue.ToString(); // = decimal.Parse("0"); - txtComment.Text = stockChosen.Comment; - } - - - - private void AddValidateData() - { - var currentStock = new StockMember(); - currentStock.StockId = txtStockName.Text.Trim(); - currentStock.StockExtId = txtStockExtId.Text; - currentStock.BuyValue = decimal.Parse(string.IsNullOrEmpty(txtBuyPrice.Text) ? "0" : txtBuyPrice.Text); - currentStock.PostAmount = long.Parse(string.IsNullOrEmpty(txtBoughtAmount.Text) ? "0" : txtBoughtAmount.Text); - currentStock.ActDate = DateTime.Parse(txtActDate.Text); - currentStock.BuyDate = string.IsNullOrWhiteSpace(txtBuyDate.Text) ? DateTime.Today : DateTime.Parse(txtBuyDate.Text); - currentStock.ActValue = decimal.Parse(string.IsNullOrEmpty(txtActValue.Text) ? "0" : txtActValue.Text); - currentStock.ActAmount = long.Parse(string.IsNullOrEmpty(txtActAmount.Text) ? "0" : txtActAmount.Text); - currentStock.SoldDate = DateTime.Parse(txtSoldDate.Text); - currentStock.SoldValue = decimal.Parse(txtSoldPrice.Text); - currentStock.Comment = txtComment.Text; - //RegisteredStocks.Add(currentStock); - try - { - _stockRepository.SaveStockMember(currentStock); - initiateRegWin(); - - } - catch (Exception ex) - { - MessageBox.Show($"Problems with update :{ex.Message}"); - } - } - - private void initiateRegWin() - { - txtStockExtId.Text = ""; - txtBuyPrice.Text = ""; - txtBuyDate.Text = ""; - txtBoughtAmount.Text = ""; - txtActValue.Text = ""; - txtActDate.Text = ""; - txtActAmount.Text = ""; - txtSoldPrice.Text = ""; - txtSoldDate.Text = ""; - txtComment.Text = ""; - } - - private void btnSaveToDB_Click(object sender, EventArgs e) - { - AddValidateData(); - } - - private void txtBoughtAmount_TextChanged(object sender, EventArgs e) - { - txtActAmount.Text = txtBoughtAmount.Text; - } - - private void lwRegBuffer_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e) - { - using (Brush hBr = new SolidBrush(hdr)) - { - e.Graphics.FillRectangle(hBr, e.Bounds); - e.DrawText(); - } - } - } -} diff --git a/HandleNewWindows/frmEditStock.resx b/HandleNewWindows/frmEditStock.resx deleted file mode 100644 index f298a7b..0000000 --- a/HandleNewWindows/frmEditStock.resx +++ /dev/null @@ -1,60 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file diff --git a/StockInfoCoreApp.sln b/StockInfoCoreApp.sln index dc0e969..02150b0 100644 --- a/StockInfoCoreApp.sln +++ b/StockInfoCoreApp.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 16.0.31205.134 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.32112.339 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StockInfoCore", "StockInfoCore\StockInfoCore.csproj", "{99122CA3-5625-4004-BED6-5423873FACAC}" EndProject @@ -21,7 +21,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StockBL", "StockBL\StockBL. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StockBL.Interface", "StockBL.Interface\StockBL.Interface.csproj", "{BEAD00C3-74AD-416B-9B36-EBE40DAFF16F}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HandleNewWindows", "HandleNewWindows\HandleNewWindows.csproj", "{791F4A63-F1CD-410A-9E9C-071DA8E32880}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BrowserHelper", "BrowserHelper\BrowserHelper.csproj", "{7F507F7A-609F-468F-AA38-41F3240EBF1B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -65,10 +65,10 @@ Global {BEAD00C3-74AD-416B-9B36-EBE40DAFF16F}.Debug|Any CPU.Build.0 = Debug|Any CPU {BEAD00C3-74AD-416B-9B36-EBE40DAFF16F}.Release|Any CPU.ActiveCfg = Release|Any CPU {BEAD00C3-74AD-416B-9B36-EBE40DAFF16F}.Release|Any CPU.Build.0 = Release|Any CPU - {791F4A63-F1CD-410A-9E9C-071DA8E32880}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {791F4A63-F1CD-410A-9E9C-071DA8E32880}.Debug|Any CPU.Build.0 = Debug|Any CPU - {791F4A63-F1CD-410A-9E9C-071DA8E32880}.Release|Any CPU.ActiveCfg = Release|Any CPU - {791F4A63-F1CD-410A-9E9C-071DA8E32880}.Release|Any CPU.Build.0 = Release|Any CPU + {7F507F7A-609F-468F-AA38-41F3240EBF1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7F507F7A-609F-468F-AA38-41F3240EBF1B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7F507F7A-609F-468F-AA38-41F3240EBF1B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7F507F7A-609F-468F-AA38-41F3240EBF1B}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE