Add project files.
This commit is contained in:
15
StockDal/AppConnection.cs
Normal file
15
StockDal/AppConnection.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RepositoryPattern
|
||||
{
|
||||
|
||||
public class StockDBConnection
|
||||
{
|
||||
public static string ConnectionString => ConfigurationManager.ConnectionStrings["StockDB"].ConnectionString;
|
||||
}
|
||||
}
|
||||
19
StockDal/StockDal.csproj
Normal file
19
StockDal/StockDal.csproj
Normal file
@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net5.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Dapper" Version="2.0.78" />
|
||||
<PackageReference Include="HtmlAgilityPack" Version="1.11.29" />
|
||||
<PackageReference Include="Selenium.WebDriverBackedSelenium" Version="3.141.0" />
|
||||
<PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
|
||||
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\StockDal.Interface\StockDal.Interface.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
230
StockDal/StockMarketRepository.cs
Normal file
230
StockDal/StockMarketRepository.cs
Normal file
@ -0,0 +1,230 @@
|
||||
using Newtonsoft.Json;
|
||||
using OpenQA.Selenium;
|
||||
using OpenQA.Selenium.Chrome;
|
||||
using StockDal.Interface;
|
||||
using StockDomain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StockDal
|
||||
{
|
||||
public class StockMarketRepository : IStockMarketRepository
|
||||
{
|
||||
public Dictionary<string, DiTraderStockRow> StockMarketList { get; set; }
|
||||
|
||||
public List<DiTraderStockRow> DumpObjects { get; set; } = new List<DiTraderStockRow>();
|
||||
public DiTraderStockRow SaveRow { get; set; }
|
||||
public StringBuilder TextResults { get; set; }
|
||||
public bool ViewBrowser { get; set; }
|
||||
|
||||
public IWebDriver driver;
|
||||
|
||||
public StockMarketRepository()
|
||||
{
|
||||
ViewBrowser = false;
|
||||
}
|
||||
|
||||
private void Find_Data()
|
||||
{
|
||||
TextResults = new StringBuilder();
|
||||
IList<IWebElement> searchElements = driver.FindElements(By.TagName("tbody"));
|
||||
foreach (IWebElement i in searchElements)
|
||||
{
|
||||
|
||||
HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument();
|
||||
var text = i.GetAttribute("innerHTML");
|
||||
htmlDocument.LoadHtml(text);
|
||||
var inputs = htmlDocument.DocumentNode.Descendants("tr").ToList();
|
||||
foreach (var items in inputs)
|
||||
{
|
||||
HtmlAgilityPack.HtmlDocument htmlDocument1 = new HtmlAgilityPack.HtmlDocument();
|
||||
htmlDocument1.LoadHtml(items.InnerHtml);
|
||||
var tds = htmlDocument1.DocumentNode.Descendants("td").ToList();
|
||||
var appendText = "";
|
||||
var fNr = 0;
|
||||
foreach (var item in tds)
|
||||
{
|
||||
var intext = item.InnerText.Replace("\r\n", "");
|
||||
appendText += appendText.Length == 0 ? intext : " " + intext;
|
||||
if (tds.Count == 10)
|
||||
{
|
||||
AddValueToListRow(fNr++, intext);
|
||||
}
|
||||
}
|
||||
if (!string.IsNullOrEmpty(appendText))
|
||||
{
|
||||
TextResults.Append(appendText + "\r\n");
|
||||
}
|
||||
htmlDocument1 = null;
|
||||
}
|
||||
htmlDocument = null;
|
||||
|
||||
TextResults.Append("\r\n");
|
||||
}
|
||||
|
||||
// var oxe = StockMarketList;
|
||||
}
|
||||
|
||||
private void SaveLogging()
|
||||
{
|
||||
var output = JsonConvert.SerializeObject(DumpObjects, Formatting.Indented);
|
||||
File.WriteAllText($"D:\\TimCoDemos\\DemoLogs\\Log{DateTime.Now.ToShortDateString()}.txt",output);
|
||||
}
|
||||
|
||||
private void AddValueToListRow(int pos, string value)
|
||||
{
|
||||
switch (pos)
|
||||
{
|
||||
case 0:
|
||||
{
|
||||
SaveRow = new DiTraderStockRow();
|
||||
SaveRow.StockName = value;
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
{
|
||||
SaveRow.ProcChange = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
SaveRow.RealChange = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
|
||||
break;
|
||||
}
|
||||
case 3:
|
||||
{
|
||||
SaveRow.BuyPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
|
||||
break;
|
||||
}
|
||||
case 4:
|
||||
{
|
||||
SaveRow.SellPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
|
||||
break;
|
||||
}
|
||||
case 5:
|
||||
{
|
||||
SaveRow.LatestPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
|
||||
break;
|
||||
}
|
||||
case 6:
|
||||
{
|
||||
SaveRow.HighestPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
|
||||
break;
|
||||
}
|
||||
case 7:
|
||||
{
|
||||
SaveRow.LowestPrice = string.IsNullOrWhiteSpace(value) ? 0 : decimal.Parse(value);
|
||||
break;
|
||||
}
|
||||
case 8:
|
||||
{
|
||||
|
||||
SaveRow.Volume = string.IsNullOrWhiteSpace(value) ? 0 : long.Parse(value.Replace(" ", ""));
|
||||
break;
|
||||
}
|
||||
case 9:
|
||||
{
|
||||
SaveRow.TimeOfDay = value==""?TimeSpan.Parse("00:01"): TimeSpan.Parse(value);
|
||||
//StockMarketList.Add(SaveRow.StockName, SaveRow);
|
||||
try
|
||||
{
|
||||
StockMarketList.Add(SaveRow.StockName, SaveRow);
|
||||
}
|
||||
catch (ArgumentException ae)
|
||||
{
|
||||
try
|
||||
{
|
||||
StockMarketList.Add(SaveRow.StockName + "-2", SaveRow);
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
DumpObjects.Add(SaveRow);
|
||||
}
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void OpenBrowser(bool burl2 = false)
|
||||
{
|
||||
var driverService = ChromeDriverService.CreateDefaultService();
|
||||
driverService.HideCommandPromptWindow = true;
|
||||
|
||||
if (ViewBrowser)
|
||||
{
|
||||
if (driver == null)
|
||||
{
|
||||
driver = new ChromeDriver(driverService);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (driver == null)
|
||||
{
|
||||
var options = new ChromeOptions();
|
||||
options.AddArgument("headless");
|
||||
driver = new ChromeDriver(driverService, options);
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
//var url0 = "https://money.cnn.com/data/hotstocks/index.html";
|
||||
var url = "https://trader.di.se/index.php/stocklist/index/2055?list=7126";
|
||||
var url2 = "https://trader.di.se/index.php/stocklist/index/2055?list=7116";
|
||||
//var url1 = "https://www.finansportalen.se/aktiekurser/";
|
||||
driver.Navigate().GoToUrl(burl2 ? url2 : url);
|
||||
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void LoadStockMarketList()
|
||||
{
|
||||
StockMarketList = new Dictionary<string, DiTraderStockRow>();
|
||||
DumpObjects = new List<DiTraderStockRow>();
|
||||
OpenBrowser();
|
||||
Find_Data();
|
||||
OpenBrowser(true);
|
||||
Find_Data();
|
||||
if (DumpObjects.Any())
|
||||
{
|
||||
SaveLogging();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void LoadStockMarketList(bool viewBrowser)
|
||||
{
|
||||
StockMarketList = new Dictionary<string, DiTraderStockRow>();
|
||||
ViewBrowser = viewBrowser;
|
||||
OpenBrowser();
|
||||
Find_Data();
|
||||
}
|
||||
|
||||
public void RefreshMarketList()
|
||||
{
|
||||
StockMarketList = new Dictionary<string, DiTraderStockRow>();
|
||||
driver.Navigate().Refresh();
|
||||
Find_Data();
|
||||
}
|
||||
|
||||
public void Clean()
|
||||
{
|
||||
driver?.Quit();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
104
StockDal/StockMemberRepository.cs
Normal file
104
StockDal/StockMemberRepository.cs
Normal file
@ -0,0 +1,104 @@
|
||||
using Dapper;
|
||||
using RepositoryPattern;
|
||||
using StockDal.Interface;
|
||||
using StockDomain;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
|
||||
namespace StockDal
|
||||
{
|
||||
public class StockMemberRepository : IStockMemberRepository
|
||||
{
|
||||
public bool Delete(string stockMemberId)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void InsertMany(List<StockMember> stockMembers)
|
||||
{
|
||||
using IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString);
|
||||
if (db.State == ConnectionState.Closed)
|
||||
db.Open();
|
||||
db.Execute(
|
||||
@"INSERT INTO [dbo].[StockMember]
|
||||
(StockId
|
||||
,StockExtId
|
||||
,BuyValue
|
||||
,BuyDate
|
||||
,ActValue
|
||||
,ActDate
|
||||
,SoldValue
|
||||
,SoldDate
|
||||
,Comment
|
||||
,PostAmount)
|
||||
VALUES
|
||||
(@StockId
|
||||
,@StockExtId
|
||||
,@BuyValue
|
||||
,@BuyDate
|
||||
,@ActValue
|
||||
,@ActDate
|
||||
,@SoldValue
|
||||
,@SoldDate
|
||||
,@Comment
|
||||
,@PostAmount)
|
||||
", stockMembers);
|
||||
}
|
||||
|
||||
public void UpdateActPrice(int Id, decimal price)
|
||||
{
|
||||
using IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString);
|
||||
if (db.State == ConnectionState.Closed)
|
||||
db.Open();
|
||||
|
||||
db.Execute(
|
||||
@"UPDATE [dbo].[StockMember]
|
||||
SET ActValue = @val,
|
||||
ActDate = @date
|
||||
WHERE Id = @id
|
||||
", new { val = price, date = DateTime.Today, id = Id });
|
||||
}
|
||||
|
||||
public IEnumerable<StockMember> GetStocks()
|
||||
{
|
||||
using IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString);
|
||||
if (db.State == ConnectionState.Closed)
|
||||
db.Open();
|
||||
return db.Query<StockMember>(@"SELECT Id
|
||||
,StockId
|
||||
,StockExtId
|
||||
,BuyValue
|
||||
,BuyDate
|
||||
,ActValue
|
||||
,ActDate
|
||||
,SoldValue
|
||||
,SoldDate
|
||||
,Comment
|
||||
,PostAmount
|
||||
FROM dbo.StockMember
|
||||
Where SoldDate is null
|
||||
ORDER BY PostAmount * (actvalue - BuyValue) desc", commandType: CommandType.Text);
|
||||
}
|
||||
public IEnumerable<StockMember> GetAllStocks()
|
||||
{
|
||||
using IDbConnection db = new SqlConnection(StockDBConnection.ConnectionString);
|
||||
if (db.State == ConnectionState.Closed)
|
||||
db.Open();
|
||||
return db.Query<StockMember>(@"SELECT Id
|
||||
,StockId
|
||||
,StockExtId
|
||||
,BuyValue
|
||||
,BuyDate
|
||||
,ActValue
|
||||
,ActDate
|
||||
,SoldValue
|
||||
,SoldDate
|
||||
,Comment
|
||||
,PostAmount
|
||||
FROM dbo.StockMember
|
||||
ORDER BY PostAmount * (actvalue - BuyValue) desc", commandType: CommandType.Text);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user