Skip to content

Commit 59b20d1

Browse files
committed
WIP, added a Service project, added PER tests
1 parent 582b8e9 commit 59b20d1

18 files changed

+421
-13
lines changed

CryptoTracker.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Database", "Database\Databa
2020
EndProject
2121
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{BA8D3187-DED2-467F-AF29-D47C827505A3}"
2222
EndProject
23+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Services", "Services\Services\Services.csproj", "{40E3DDA3-F389-48B4-98AC-3DD9DE58FFEB}"
24+
EndProject
25+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Utils", "Utils\Utils.csproj", "{49FA8FDA-6F79-4F83-9035-CE80789A3A16}"
26+
EndProject
2327
Global
2428
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2529
Debug|Any CPU = Debug|Any CPU
@@ -50,6 +54,14 @@ Global
5054
{BA8D3187-DED2-467F-AF29-D47C827505A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
5155
{BA8D3187-DED2-467F-AF29-D47C827505A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
5256
{BA8D3187-DED2-467F-AF29-D47C827505A3}.Release|Any CPU.Build.0 = Release|Any CPU
57+
{40E3DDA3-F389-48B4-98AC-3DD9DE58FFEB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
58+
{40E3DDA3-F389-48B4-98AC-3DD9DE58FFEB}.Debug|Any CPU.Build.0 = Debug|Any CPU
59+
{40E3DDA3-F389-48B4-98AC-3DD9DE58FFEB}.Release|Any CPU.ActiveCfg = Release|Any CPU
60+
{40E3DDA3-F389-48B4-98AC-3DD9DE58FFEB}.Release|Any CPU.Build.0 = Release|Any CPU
61+
{49FA8FDA-6F79-4F83-9035-CE80789A3A16}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
62+
{49FA8FDA-6F79-4F83-9035-CE80789A3A16}.Debug|Any CPU.Build.0 = Debug|Any CPU
63+
{49FA8FDA-6F79-4F83-9035-CE80789A3A16}.Release|Any CPU.ActiveCfg = Release|Any CPU
64+
{49FA8FDA-6F79-4F83-9035-CE80789A3A16}.Release|Any CPU.Build.0 = Release|Any CPU
5365
EndGlobalSection
5466
GlobalSection(SolutionProperties) = preSolution
5567
HideSolutionNode = FALSE

Database/SqlSchema.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS portfolio_entries (
1818
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
1919
symbol TEXT NOT NULL,
2020
portfolio_id INTEGER NOT NULL,
21-
FOREIGN KEY(portfolio_id) REFERENCES portfolios(portfolio_id)
21+
FOREIGN KEY(portfolio_id) REFERENCES portfolios(id)
2222
);
2323
2424
CREATE TABLE IF NOT EXISTS market_orders (
@@ -30,7 +30,7 @@ CREATE TABLE IF NOT EXISTS market_orders (
3030
date INTEGER NOT NULL,
3131
buy INTEGER NOT NULL,
3232
portfolio_entry_id INTEGER NOT NULL,
33-
FOREIGN KEY(portfolio_entry_id) REFERENCES portfolio_entries(portfolio_entry_id)
33+
FOREIGN KEY(portfolio_entry_id) REFERENCES portfolio_entries(id)
3434
);
3535
3636
");

Model/Model.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System;
22

3+
public record PortfolioEntry(string Symbol, int PortfolioId = -1, int Id = -1);
4+
35
namespace Model
46
{
57
public record MarketOrder(Currency Currency, decimal FilledPrice, decimal Fee, decimal Size,
68
DateTime Date, bool Buy, int Id = -1, int PortfolioEntryId = -1);
79

810
public record Portfolio(string Name, string Description, int Id = -1);
911

10-
public record PortfolioEntry(string Symbol, int PortfolioId = -1, int Id = -1);
11-
1212
public enum Currency
1313
{
1414
Czk,

Repository/IRepository.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ public interface IRepository<T>
1010

1111
public T Get(int id);
1212

13-
public void Update(T entry);
13+
public bool Update(T entry);
1414

15-
public void Delete(T entry);
15+
public bool Delete(T entry);
1616
}
1717

1818
public interface IMarketOrderRepository : IRepository<MarketOrder>

Repository/Repository.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<ItemGroup>
1010
<ProjectReference Include="..\Model\Model.csproj" />
1111
<ProjectReference Include="..\Database\Database.csproj" />
12+
<ProjectReference Include="..\Utils\Utils.csproj" />
1213
</ItemGroup>
1314

1415
<ItemGroup>

Repository/SqlKataMarketOrderRepository.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public SqlKataMarketOrderRepository(SqlKataDatabase db) : base(db, "market_order
2323
portfolio_entrY_id = entry.PortfolioEntryId,
2424
};
2525

26-
public override MarketOrder FromRow(dynamic d) => new(Currency.Czk, 0, 0, 0, DateTime.Now, true, 0, 0);
26+
public override MarketOrder FromRow(dynamic d)
27+
{
28+
bool parsed = Enum.TryParse(d.currency, out Currency currency);
29+
if (!parsed)
30+
{
31+
throw new SqlKataRepositoryException($"Failed to parse currency {d.currency}");
32+
}
33+
34+
return new(currency, d.filled_price, d.fee, d.size, Utils.Utils.UnixTimeStampToDateTime(d.date), d.buy > 0,
35+
0, 0);
36+
}
2737
}
2838
}

Repository/SqlKataPortfolioEntryRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ public SqlKataPortfolioEntryRepository(SqlKataDatabase db) : base(db, "portfolio
1717
portfolio_id = entry.PortfolioId
1818
};
1919

20-
public override PortfolioEntry FromRow(dynamic d) => new(d.symbol, d.portfolio_entry_id, d.id);
20+
public override PortfolioEntry FromRow(dynamic d) => new((string) d.symbol, (int) d.portfolio_id, (int) d.id);
2121
}
2222
}

Repository/SqlKataRepository.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,17 @@ public int Add(T entry)
2929
return id;
3030
}
3131

32-
public void Update(T entry)
32+
public bool Update(T entry)
3333
{
34-
Db.Get().Query(_tableName).Where("id", _getEntryId(entry)).AsUpdate(ToRow(entry));
34+
var updated = Db.Get().Query(_tableName).Where("id", _getEntryId(entry)).Update(ToRow(entry));
35+
return updated > 0;
3536
}
3637

37-
public void Delete(T entry)
38+
public bool Delete(T entry)
3839
{
3940
Db.Get().Query(_tableName).Where("id", _getEntryId(entry)).AsDelete();
41+
// TODO
42+
return true;
4043
}
4144

4245
public T Get(int id)
@@ -61,4 +64,11 @@ public List<T> All()
6164
return items;
6265
}
6366
}
67+
68+
public class SqlKataRepositoryException : Exception
69+
{
70+
public SqlKataRepositoryException(string message) : base(message)
71+
{
72+
}
73+
}
6474
}

Services/Services.csproj

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net5.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1"/>
11+
<PackageReference Include="xunit" Version="2.4.1"/>
12+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
13+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
14+
<PrivateAssets>all</PrivateAssets>
15+
</PackageReference>
16+
<PackageReference Include="coverlet.collector" Version="1.3.0">
17+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
18+
<PrivateAssets>all</PrivateAssets>
19+
</PackageReference>
20+
</ItemGroup>
21+
22+
</Project>

Services/Services/PortfolioService.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Runtime.CompilerServices;
4+
using Model;
5+
using Repository;
6+
7+
namespace Services
8+
{
9+
public interface IPortfolioService
10+
{
11+
Portfolio CreatePortfolio(string name, string description);
12+
bool DeletePortfolio(Portfolio portfolio);
13+
bool UpdatePortfolio(Portfolio portfolio);
14+
Portfolio GetPortfolio(int id);
15+
List<Portfolio> GetPortfolios();
16+
}
17+
18+
public class PortfolioServiceImpl : IPortfolioService
19+
{
20+
private IPortfolioRepository _portfolioRepository;
21+
22+
public PortfolioServiceImpl(IPortfolioRepository portfolioRepository)
23+
{
24+
this._portfolioRepository = portfolioRepository;
25+
}
26+
27+
public Portfolio CreatePortfolio(string name, string description)
28+
{
29+
var id = _portfolioRepository.Add(new(name, description));
30+
return new(name, description, id);
31+
}
32+
33+
public bool DeletePortfolio(Portfolio portfolio)
34+
{
35+
return _portfolioRepository.Delete(portfolio);
36+
}
37+
38+
public bool UpdatePortfolio(Portfolio portfolio)
39+
{
40+
throw new NotImplementedException();
41+
}
42+
43+
public Portfolio GetPortfolio(int id)
44+
{
45+
throw new NotImplementedException();
46+
}
47+
48+
public List<Portfolio> GetPortfolios()
49+
{
50+
throw new NotImplementedException();
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)