Skip to content

Commit f37d8e8

Browse files
committed
WIP commit
1 parent 4dc88bd commit f37d8e8

17 files changed

+261
-49
lines changed

.idea/.idea.CryptoTracker/.idea/sqldialects.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Database/SqlKataDatabase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,16 @@ public SqlKataDatabase(IDbConnection dbConnection, Compiler compiler)
1414
{
1515
_dbConnection = dbConnection;
1616
_queryFactory = new(dbConnection, compiler);
17-
17+
1818
Console.WriteLine("SqlKataDatabase constructor invoked");
19+
SqlSchema.Init(this);
1920
}
2021

2122
public QueryFactory Get() => _queryFactory;
2223

2324
public void Dispose()
2425
{
26+
// TODO check whether dispose is called
2527
Console.WriteLine("Disposing SqlKataDatabase");
2628
_dbConnection.Close();
2729
}

Database/SqlSchema.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
namespace Database
2+
{
3+
public class SqlSchema
4+
{
5+
public static void Init(SqlKataDatabase db)
6+
{
7+
db.Get().Statement(@"
8+
9+
CREATE TABLE IF NOT EXISTS portfolios (
10+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
11+
name TEXT NOT NULL,
12+
description TEXT NOT NULL
13+
);
14+
15+
CREATE TABLE IF NOT EXISTS portfolio_entries (
16+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
17+
symbol TEXT NOT NULL,
18+
portfolio_id INTEGER NOT NULL,
19+
FOREIGN KEY(portfolio_id) REFERENCES portfolios(portfolio_id)
20+
);
21+
22+
CREATE TABLE IF NOT EXISTS market_orders (
23+
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
24+
currency TEXT NOT NULL,
25+
filled_price INTEGER NOT NULL,
26+
fee INTEGER NOT NULL,
27+
size INTEGER NOT NULL,
28+
date INTEGER NOT NULL,
29+
buy INTEGER NOT NULL,
30+
portfolio_entry_id INTEGER NOT NULL,
31+
FOREIGN KEY(portfolio_entry_id) REFERENCES portfolio_entries(portfolio_entry_id)
32+
);
33+
34+
");
35+
}
36+
}
37+
}

Model/Model.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Model
44
{
5-
public record MarketOrder(string Symbol, Currency Currency, decimal FilledPrice, decimal Fee, decimal Size,
6-
DateTime Date, bool Buy, int Id = -1, int PortfolioId = -1);
5+
public record MarketOrder(Currency Currency, decimal FilledPrice, decimal Fee, decimal Size,
6+
DateTime Date, bool Buy, int Id = -1, int PortfolioEntryId = -1);
77

88
public record Portfolio(string Name, string Description, int Id = -1);
99

10+
public record PortfolioEntry(string Symbol, int PortfolioId = -1, int Id = -1);
11+
1012
public enum Currency
1113
{
1214
Czk,

Repository/IRepository.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Model;
2+
3+
namespace Repository
4+
{
5+
public interface IRepository<T>
6+
{
7+
public object ToRow(T entry);
8+
9+
public void Add(T entry);
10+
11+
public void Update(T entry);
12+
13+
public void Delete(T entry);
14+
}
15+
16+
public interface IMarketOrderRepository : IRepository<MarketOrder>
17+
{
18+
}
19+
20+
public interface IPortfolioRepository : IRepository<Portfolio>
21+
{
22+
}
23+
}

Repository/MarketOrders/IMarketOrderRepository.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

Repository/MarketOrders/SqlKataMarketOrderRepository.cs

Lines changed: 0 additions & 31 deletions
This file was deleted.

Repository/Repository.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313

1414
<ItemGroup>
1515
<PackageReference Include="Microsoft.Data.Sqlite.Core" Version="5.0.4" />
16+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.1" />
1617
<PackageReference Include="SqlKata.Execution" Version="2.3.2" />
18+
<PackageReference Include="xunit.assert" Version="2.4.1" />
19+
<PackageReference Include="xunit.core" Version="2.4.1" />
1720
</ItemGroup>
1821

1922
</Project>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using Database;
3+
using Model;
4+
5+
namespace Repository
6+
{
7+
public class SqlKataMarketOrderRepository : SqlKataRepository<MarketOrder>
8+
{
9+
public SqlKataMarketOrderRepository(SqlKataDatabase db) : base(db, "market_orders")
10+
{
11+
}
12+
13+
protected override int _getEntryId(MarketOrder entry) => entry.Id;
14+
15+
public override object ToRow(MarketOrder entry) => new
16+
{
17+
currency = entry.Currency.ToString(),
18+
filled_price = (int) (entry.FilledPrice * 100),
19+
fee = (int) (entry.Fee * 100),
20+
size = (int) (entry.Size * 100),
21+
date = entry.Date.ToBinary(),
22+
buy = ((DateTimeOffset) entry.Date).ToUnixTimeSeconds(),
23+
portfolio_entrY_id = entry.PortfolioEntryId,
24+
};
25+
26+
public override MarketOrder FromRow(dynamic d) => new(Currency.Czk, 0, 0, 0, DateTime.Now, true, 0, 0);
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using Database;
2+
using Model;
3+
4+
namespace Repository
5+
{
6+
public class SqlKataPortfolioEntryRepository : SqlKataRepository<PortfolioEntry>
7+
{
8+
public SqlKataPortfolioEntryRepository(SqlKataDatabase db) : base(db, "portfolio_entries")
9+
{
10+
}
11+
12+
protected override int _getEntryId(PortfolioEntry entry) => entry.Id;
13+
14+
public override object ToRow(PortfolioEntry entry) => new
15+
{
16+
symbol = entry.Symbol,
17+
portfolio_id = entry.PortfolioId
18+
};
19+
20+
public override PortfolioEntry FromRow(dynamic d) => new(d.symbol, d.portfolio_entry_id, d.id);
21+
}
22+
}

0 commit comments

Comments
 (0)