Skip to content

Commit ae77bc1

Browse files
committed
Added GetAllByPortfolioId method to IPortfolioEntryRepository interface
Implemented GetAllByPortfolioId method in SqlKataPortfolioEntryRepository and covered it with a simple test Implemented PortfolioEntryService (not tested yet)
1 parent d421f4c commit ae77bc1

File tree

7 files changed

+170
-45
lines changed

7 files changed

+170
-45
lines changed

Database/SqlSchema.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Database
44
{
55
public class SqlSchema
66
{
7+
// TODO column names into constants
78
public static void Init(SqlKataDatabase db)
89
{
910
db.Get().Statement(@"

Repository/IRepository.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,9 @@ public interface IMarketOrderRepository : IRepository<MarketOrder>
2626
public interface IPortfolioRepository : IRepository<Portfolio>
2727
{
2828
}
29+
30+
public interface IPortfolioEntryRepository : IRepository<PortfolioEntry>
31+
{
32+
public List<PortfolioEntry> GetAllByPortfolioId(int portfolioId);
33+
}
2934
}

Repository/SqlKataPortfolioEntryRepository.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
using System.Collections.Generic;
12
using Database;
23
using Model;
4+
using SqlKata.Execution;
35

46
namespace Repository
57
{
6-
public class SqlKataPortfolioEntryRepository : SqlKataRepository<PortfolioEntry>
8+
public class SqlKataPortfolioEntryRepository : SqlKataRepository<PortfolioEntry>, IPortfolioEntryRepository
79
{
810
public SqlKataPortfolioEntryRepository(SqlKataDatabase db) : base(db, "portfolio_entries")
911
{
@@ -16,7 +18,10 @@ public SqlKataPortfolioEntryRepository(SqlKataDatabase db) : base(db, "portfolio
1618
symbol = entry.Symbol,
1719
portfolio_id = entry.PortfolioId
1820
};
19-
21+
2022
public override PortfolioEntry FromRow(dynamic d) => new((string) d.symbol, (int) d.portfolio_id, (int) d.id);
23+
24+
public List<PortfolioEntry> GetAllByPortfolioId(int portfolioId) =>
25+
RowsToObjects(Db.Get().Query(tableName).Where("portfolio_id", portfolioId).Get());
2126
}
2227
}

Repository/SqlKataRepository.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ namespace Repository
1010
public abstract class SqlKataRepository<T> : IRepository<T>
1111
{
1212
protected readonly SqlKataDatabase Db;
13-
private readonly string _tableName;
13+
protected readonly string tableName;
1414

1515
public SqlKataRepository(SqlKataDatabase db, string tableName)
1616
{
1717
this.Db = db;
18-
this._tableName = tableName;
18+
this.tableName = tableName;
1919
}
2020

2121
public abstract object ToRow(T entry);
@@ -26,26 +26,26 @@ public SqlKataRepository(SqlKataDatabase db, string tableName)
2626

2727
public int Add(T entry)
2828
{
29-
var id = Db.Get().Query(_tableName).InsertGetId<int>(ToRow(entry));
29+
var id = Db.Get().Query(tableName).InsertGetId<int>(ToRow(entry));
3030
return id;
3131
}
3232

3333
public bool Update(T entry)
3434
{
35-
var updated = Db.Get().Query(_tableName).Where("id", _getEntryId(entry)).Update(ToRow(entry));
35+
var updated = Db.Get().Query(tableName).Where("id", _getEntryId(entry)).Update(ToRow(entry));
3636
return updated > 0;
3737
}
3838

3939
public bool Delete(T entry)
4040
{
41-
Db.Get().Query(_tableName).Where("id", _getEntryId(entry)).AsDelete();
41+
Db.Get().Query(tableName).Where("id", _getEntryId(entry)).AsDelete();
4242
// TODO
4343
return true;
4444
}
4545

4646
public T Get(int id)
4747
{
48-
var result = Db.Get().Query(_tableName).Where("id", id).First();
48+
var result = Db.Get().Query(tableName).Where("id", id).First();
4949
if (result == null)
5050
{
5151
return default;
@@ -54,27 +54,12 @@ public T Get(int id)
5454
return FromRow(result);
5555
}
5656

57-
public List<T> GetAll()
58-
{
59-
var result = Db.Get().Query(_tableName).Get();
60-
if (result == null)
61-
{
62-
return default;
63-
}
64-
65-
var list = new List<T>();
66-
foreach (var row in result)
67-
{
68-
list.Add(FromRow(row));
69-
}
70-
71-
return list;
72-
}
57+
public List<T> GetAll() => RowsToObjects(Db.Get().Query(tableName).Select().Get());
7358

74-
public List<T> All()
59+
protected List<T> RowsToObjects(IEnumerable<dynamic> rows)
7560
{
7661
List<T> items = new List<T>();
77-
foreach (var row in Db.Get().Query(_tableName).Select().Get())
62+
foreach (var row in rows)
7863
{
7964
items.Add(FromRow(row));
8065
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 IPortfolioEntryService
10+
{
11+
PortfolioEntry CreatePortfolioEntry(string symbol, int portfolioId);
12+
13+
bool DeletePortfolioEntry(PortfolioEntry entry);
14+
15+
bool UpdatePortfolio(PortfolioEntry entry);
16+
17+
PortfolioEntry GetPortfolioEntry(int id);
18+
19+
List<PortfolioEntry> GetPortfolioEntries(int portfolioId);
20+
}
21+
22+
public class PortfolioEntryServiceImpl : IPortfolioEntryService
23+
{
24+
private IPortfolioEntryRepository _portfolioEntryRepository;
25+
26+
public PortfolioEntryServiceImpl(IPortfolioEntryRepository portfolioEntryRepository)
27+
{
28+
_portfolioEntryRepository = portfolioEntryRepository;
29+
}
30+
31+
32+
public PortfolioEntry CreatePortfolioEntry(string symbol, int portfolioId)
33+
{
34+
var portfolioEntry = new PortfolioEntry(symbol, portfolioId);
35+
portfolioEntry = portfolioEntry with {Id = _portfolioEntryRepository.Add(portfolioEntry)};
36+
return portfolioEntry;
37+
}
38+
39+
public bool DeletePortfolioEntry(PortfolioEntry entry)
40+
{
41+
return _portfolioEntryRepository.Delete(entry);
42+
}
43+
44+
public bool UpdatePortfolio(PortfolioEntry entry)
45+
{
46+
return _portfolioEntryRepository.Update(entry);
47+
}
48+
49+
public PortfolioEntry GetPortfolioEntry(int id)
50+
{
51+
return _portfolioEntryRepository.Get(id);
52+
}
53+
54+
public List<PortfolioEntry> GetPortfolioEntries(int portfolioId)
55+
{
56+
return _portfolioEntryRepository.GetAllByPortfolioId(portfolioId);
57+
}
58+
}
59+
}

Tests/Integration/Repository/MarketOrderTest.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using Repository;
77
using SqlKata.Compilers;
88
using Xunit;
9-
using Xunit.Abstractions;
109

1110
namespace Tests.Integration.Repository
1211
{
@@ -39,8 +38,7 @@ public class MarketOrderRepositoryTest : IClassFixture<SqlKataMarketOrderReposit
3938
private SqlKataMarketOrderRepositoryFixture _marketOrderRepositoryFixture;
4039

4140
// TODO test Delete method
42-
public MarketOrderRepositoryTest(SqlKataMarketOrderRepositoryFixture marketOrderRepositoryFixture,
43-
ITestOutputHelper testOutputHelper)
41+
public MarketOrderRepositoryTest(SqlKataMarketOrderRepositoryFixture marketOrderRepositoryFixture)
4442
{
4543
this._marketOrderRepositoryFixture = marketOrderRepositoryFixture;
4644
}
@@ -86,26 +84,37 @@ public void Added_And_GetAll_AreEqual()
8684
{
8785
// fixture unique to this test
8886
var marketOrderRepositoryFixture = new SqlKataMarketOrderRepositoryFixture();
89-
87+
9088
// arrange
9189
var marketOrder1 = new MarketOrder(new Decimal(10000.39), 10, new Decimal(1.1), DateTime.Now, true,
9290
PortfolioEntryId: marketOrderRepositoryFixture.DefaultPortfolioEntryId);
93-
var marketOrder2 = new MarketOrder(new Decimal(11000.39), 11, new Decimal(1.2), DateTime.Now.Subtract(TimeSpan.FromSeconds(3600)), true,
91+
var marketOrder2 = new MarketOrder(new Decimal(11000.39), 11, new Decimal(1.2),
92+
DateTime.Now.Subtract(TimeSpan.FromSeconds(3600)), true,
9493
PortfolioEntryId: marketOrderRepositoryFixture.DefaultPortfolioEntryId);
95-
var marketOrder3 = new MarketOrder(new Decimal(12000.39), 12, new Decimal(1.3), DateTime.Now.Subtract(TimeSpan.FromDays(30)), false,
94+
var marketOrder3 = new MarketOrder(new Decimal(12000.39), 12, new Decimal(1.3),
95+
DateTime.Now.Subtract(TimeSpan.FromDays(30)), false,
9696
PortfolioEntryId: marketOrderRepositoryFixture.DefaultPortfolioEntryId);
9797

9898
// act
99-
marketOrder1 = marketOrder1 with {Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder1)};
100-
marketOrder2 = marketOrder2 with {Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder2)};
101-
marketOrder3 = marketOrder3 with {Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder3)};
102-
99+
marketOrder1 = marketOrder1 with
100+
{
101+
Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder1)
102+
};
103+
marketOrder2 = marketOrder2 with
104+
{
105+
Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder2)
106+
};
107+
marketOrder3 = marketOrder3 with
108+
{
109+
Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder3)
110+
};
111+
103112
// assert
104113
var loadedPortfolios = marketOrderRepositoryFixture.MarketOrderRepository.GetAll();
105114
Assert.Equal(3, loadedPortfolios.Count);
106-
Assert.Equal(new List<MarketOrder>{marketOrder1, marketOrder2, marketOrder3}, loadedPortfolios);
115+
Assert.Equal(new List<MarketOrder> {marketOrder1, marketOrder2, marketOrder3}, loadedPortfolios);
107116
}
108-
117+
109118
[Fact]
110119
public void AddUpdate_Updates()
111120
{

Tests/Integration/Repository/PortfolioEntryTest.cs

Lines changed: 68 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class SqlKataPortfolioEntryRepositoryFixture : IDisposable
1515
public SqlKataPortfolioEntryRepository PortfolioEntryRepository;
1616
private SqliteConnection _dbConnection;
1717
public int DefaultPortfolioId;
18+
public int SecondaryPortfolioId;
1819

1920
public SqlKataPortfolioEntryRepositoryFixture()
2021
{
@@ -24,6 +25,7 @@ public SqlKataPortfolioEntryRepositoryFixture()
2425
this.PortfolioRepository = new(db);
2526
this.PortfolioEntryRepository = new(db);
2627
DefaultPortfolioId = PortfolioRepository.Add(new("Foo", "Bar", Currency.Czk));
28+
SecondaryPortfolioId = PortfolioRepository.Add(new("Bar", "Bar", Currency.Czk));
2729
}
2830

2931
public void Dispose()
@@ -74,27 +76,86 @@ public void Added_And_Get_AreEqual()
7476
Assert.Equal(portfolioEntry,
7577
_portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(portfolioEntry.Id));
7678
}
77-
79+
7880
[Fact]
7981
public void Added_And_GetAll_AreEqual()
8082
{
8183
// fixture unique to this test
8284
var portfolioEntryRepositoryFixture = new SqlKataPortfolioEntryRepositoryFixture();
83-
85+
8486
// arrange
8587
var portfolioEntry1 = new PortfolioEntry("btc", portfolioEntryRepositoryFixture.DefaultPortfolioId);
8688
var portfolioEntry2 = new PortfolioEntry("ada", portfolioEntryRepositoryFixture.DefaultPortfolioId);
8789
var portfolioEntry3 = new PortfolioEntry("ltc", portfolioEntryRepositoryFixture.DefaultPortfolioId);
8890

8991
// act
90-
portfolioEntry1 = portfolioEntry1 with {Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry1)};
91-
portfolioEntry2 = portfolioEntry2 with {Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry2)};
92-
portfolioEntry3 = portfolioEntry3 with {Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry3)};
93-
92+
portfolioEntry1 = portfolioEntry1 with
93+
{
94+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry1)
95+
};
96+
portfolioEntry2 = portfolioEntry2 with
97+
{
98+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry2)
99+
};
100+
portfolioEntry3 = portfolioEntry3 with
101+
{
102+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry3)
103+
};
104+
94105
// assert
95106
var loadedPortfolios = portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAll();
96107
Assert.Equal(3, loadedPortfolios.Count);
97-
Assert.Equal(new List<PortfolioEntry>{portfolioEntry1, portfolioEntry2, portfolioEntry3}, loadedPortfolios);
108+
Assert.Equal(new List<PortfolioEntry> {portfolioEntry1, portfolioEntry2, portfolioEntry3},
109+
loadedPortfolios);
110+
}
111+
112+
[Fact]
113+
public void GetAllByPortfolioId_Returns_Correct_Entries()
114+
{
115+
// fixture unique to this test
116+
var portfolioEntryRepositoryFixture = new SqlKataPortfolioEntryRepositoryFixture();
117+
118+
// arrange
119+
var portfolioEntry1 = new PortfolioEntry("btc", portfolioEntryRepositoryFixture.DefaultPortfolioId);
120+
var portfolioEntry2 = new PortfolioEntry("ada", portfolioEntryRepositoryFixture.DefaultPortfolioId);
121+
var portfolioEntry3 = new PortfolioEntry("ltc", portfolioEntryRepositoryFixture.DefaultPortfolioId);
122+
123+
var portfolioEntry4 = new PortfolioEntry("btc", portfolioEntryRepositoryFixture.SecondaryPortfolioId);
124+
var portfolioEntry5 = new PortfolioEntry("eth", portfolioEntryRepositoryFixture.SecondaryPortfolioId);
125+
126+
// act
127+
portfolioEntry1 = portfolioEntry1 with
128+
{
129+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry1)
130+
};
131+
portfolioEntry2 = portfolioEntry2 with
132+
{
133+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry2)
134+
};
135+
portfolioEntry3 = portfolioEntry3 with
136+
{
137+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry3)
138+
};
139+
140+
portfolioEntry4 = portfolioEntry4 with
141+
{
142+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry4)
143+
};
144+
portfolioEntry5 = portfolioEntry5 with
145+
{
146+
Id = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(portfolioEntry5)
147+
};
148+
149+
// assert
150+
var loadedPortfolios = portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAllByPortfolioId(portfolioEntryRepositoryFixture.DefaultPortfolioId);
151+
Assert.Equal(3, loadedPortfolios.Count);
152+
Assert.Equal(new List<PortfolioEntry> {portfolioEntry1, portfolioEntry2, portfolioEntry3},
153+
loadedPortfolios);
154+
155+
var loadedPortfoliosSecondaryPortfolio = portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAllByPortfolioId(portfolioEntryRepositoryFixture.SecondaryPortfolioId);
156+
Assert.Equal(2, loadedPortfoliosSecondaryPortfolio.Count);
157+
Assert.Equal(new List<PortfolioEntry> {portfolioEntry4, portfolioEntry5},
158+
loadedPortfoliosSecondaryPortfolio);
98159
}
99160

100161
[Fact]

0 commit comments

Comments
 (0)