Skip to content

Commit cbbde47

Browse files
committed
Extended repositories and services with methods that allow to delete multiple items at once.
Improved the database layer by allowing to delete multiple portfolio entries and market orders. Added tests verifying the validity of added methods.
1 parent 42c9c70 commit cbbde47

12 files changed

+197
-47
lines changed

Repository/IRepository.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public interface IRepository<T>
2222
public interface IMarketOrderRepository : IRepository<MarketOrder>
2323
{
2424
public List<MarketOrder> GetAllByPortfolioEntryId(int portfolioEntryId);
25+
public int DeletePortfolioEntryOrders(int portfolioEntryOrder);
2526
}
2627

2728
public interface IPortfolioRepository : IRepository<Portfolio>
@@ -31,5 +32,7 @@ public interface IPortfolioRepository : IRepository<Portfolio>
3132
public interface IPortfolioEntryRepository : IRepository<PortfolioEntry>
3233
{
3334
public List<PortfolioEntry> GetAllByPortfolioId(int portfolioId);
35+
36+
public int DeletePortfolioEntries(int portfolioId);
3437
}
3538
}

Repository/SqlKataMarketOrderRepository.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Database;
45
using Model;
56
using SqlKata.Execution;
@@ -34,5 +35,8 @@ public override MarketOrder FromRow(dynamic d) =>
3435

3536
public List<MarketOrder> GetAllByPortfolioEntryId(int portfolioEntryId) =>
3637
RowsToObjects(Db.Get().Query(tableName).Where("portfolio_entry_id", portfolioEntryId).Get());
38+
39+
public int DeletePortfolioEntryOrders(int portfolioEntryOrder) =>
40+
Db.Get().Query(tableName).Where("portfolio_entry_id", portfolioEntryOrder).Delete();
3741
}
3842
}

Repository/SqlKataPortfolioEntryRepository.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ public SqlKataPortfolioEntryRepository(SqlKataDatabase db) : base(db, "portfolio
2323

2424
public List<PortfolioEntry> GetAllByPortfolioId(int portfolioId) =>
2525
RowsToObjects(Db.Get().Query(tableName).Where("portfolio_id", portfolioId).Get());
26+
27+
public int DeletePortfolioEntries(int portfolioId) =>
28+
Db.Get().Query(tableName).Where("portfolio_id", portfolioId).Delete();
2629
}
2730
}

Repository/SqlKataRepository.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,12 @@ public bool Update(T entry)
3939
public bool Delete(T entry)
4040
{
4141
Db.Get().Query(tableName).Where("id", _getEntryId(entry)).Delete();
42-
// TODO add tests
4342
return true;
4443
}
4544

4645
public T Get(int id)
4746
{
48-
var result = Db.Get().Query(tableName).Where("id", id).First();
47+
var result = Db.Get().Query(tableName).Where("id", id).FirstOrDefault();
4948
if (result == null)
5049
{
5150
return default;

Services/Services/MarketOrderService.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ MarketOrder CreateMarketOrder(decimal filledPrice, decimal fee, decimal size,
1818
MarketOrder GetMarketOrder(int id);
1919

2020
List<MarketOrder> GetPortfolioEntryOrders(int portfolioEntryId);
21+
22+
int DeletePortfolioEntryOrders(int portfolioEntryId);
2123
}
2224

2325
public class MarketOrderServiceImpl : IMarketOrderService
@@ -45,5 +47,8 @@ public MarketOrder CreateMarketOrder(decimal filledPrice, decimal fee, decimal s
4547

4648
public List<MarketOrder> GetPortfolioEntryOrders(int portfolioEntryId) =>
4749
_marketOrderRepository.GetAllByPortfolioEntryId(portfolioEntryId);
50+
51+
public int DeletePortfolioEntryOrders(int portfolioEntryId) =>
52+
_marketOrderRepository.DeletePortfolioEntryOrders(portfolioEntryId);
4853
}
4954
}

Services/Services/PortfolioEntryService.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ public interface IPortfolioEntryService
1111
PortfolioEntry CreatePortfolioEntry(string symbol, int portfolioId);
1212

1313
bool DeletePortfolioEntry(PortfolioEntry entry);
14+
15+
int DeletePortfolioEntries(int portfolioId);
1416

1517
bool UpdatePortfolio(PortfolioEntry entry);
1618

@@ -22,10 +24,12 @@ public interface IPortfolioEntryService
2224
public class PortfolioEntryServiceImpl : IPortfolioEntryService
2325
{
2426
private IPortfolioEntryRepository _portfolioEntryRepository;
27+
private IMarketOrderService _marketOrderService;
2528

26-
public PortfolioEntryServiceImpl(IPortfolioEntryRepository portfolioEntryRepository)
29+
public PortfolioEntryServiceImpl(IPortfolioEntryRepository portfolioEntryRepository, IMarketOrderService marketOrderService)
2730
{
2831
_portfolioEntryRepository = portfolioEntryRepository;
32+
_marketOrderService = marketOrderService;
2933
}
3034

3135

@@ -38,22 +42,24 @@ public PortfolioEntry CreatePortfolioEntry(string symbol, int portfolioId)
3842

3943
public bool DeletePortfolioEntry(PortfolioEntry entry)
4044
{
45+
_marketOrderService.DeletePortfolioEntryOrders(entry.Id);
4146
return _portfolioEntryRepository.Delete(entry);
4247
}
4348

44-
public bool UpdatePortfolio(PortfolioEntry entry)
45-
{
46-
return _portfolioEntryRepository.Update(entry);
47-
}
49+
public bool UpdatePortfolio(PortfolioEntry entry) => _portfolioEntryRepository.Update(entry);
4850

49-
public PortfolioEntry GetPortfolioEntry(int id)
50-
{
51-
return _portfolioEntryRepository.Get(id);
52-
}
51+
public PortfolioEntry GetPortfolioEntry(int id) => _portfolioEntryRepository.Get(id);
52+
53+
public List<PortfolioEntry> GetPortfolioEntries(int portfolioId) => _portfolioEntryRepository.GetAllByPortfolioId(portfolioId);
5354

54-
public List<PortfolioEntry> GetPortfolioEntries(int portfolioId)
55+
public int DeletePortfolioEntries(int portfolioId)
5556
{
56-
return _portfolioEntryRepository.GetAllByPortfolioId(portfolioId);
57+
foreach (var portfolioEntry in GetPortfolioEntries(portfolioId))
58+
{
59+
_marketOrderService.DeletePortfolioEntryOrders(portfolioEntry.Id);
60+
}
61+
62+
return _portfolioEntryRepository.DeletePortfolioEntries(portfolioId);
5763
}
5864
}
5965
}

Services/Services/PortfolioService.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ public interface IPortfolioService
1818
public class PortfolioServiceImpl : IPortfolioService
1919
{
2020
private IPortfolioRepository _portfolioRepository;
21+
private IPortfolioEntryService _portfolioEntryService;
2122

22-
public PortfolioServiceImpl(IPortfolioRepository portfolioRepository)
23+
public PortfolioServiceImpl(IPortfolioRepository portfolioRepository, IPortfolioEntryService portfolioEntryService)
2324
{
2425
this._portfolioRepository = portfolioRepository;
26+
this._portfolioEntryService = portfolioEntryService;
2527
}
2628

2729
public Portfolio CreatePortfolio(string name, string description, Currency currency)
@@ -36,6 +38,7 @@ public Portfolio CreatePortfolio(string name, string description, Currency curre
3638

3739
public bool DeletePortfolio(Portfolio portfolio)
3840
{
41+
_portfolioEntryService.DeletePortfolioEntries(portfolio.Id);
3942
return _portfolioRepository.Delete(portfolio);
4043
}
4144

Tests/Integration/Repository/MarketOrderTest.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ public class MarketOrderRepositoryTest : IClassFixture<SqlKataMarketOrderReposit
3939
{
4040
private SqlKataMarketOrderRepositoryFixture _marketOrderRepositoryFixture;
4141

42-
// TODO test Delete method
4342
public MarketOrderRepositoryTest(SqlKataMarketOrderRepositoryFixture marketOrderRepositoryFixture)
4443
{
4544
this._marketOrderRepositoryFixture = marketOrderRepositoryFixture;
@@ -200,7 +199,7 @@ public void GetAllByPortfolioEntry_Returns_Correct_Orders()
200199
.DefaultPortfolioEntryId);
201200
Assert.Empty(presumablyEmptyList);
202201
Assert.Empty(presumablyEmptyList2);
203-
202+
204203
Assert.Equal(3, loadedPortfolios.Count);
205204
Assert.Equal(new List<MarketOrder> {marketOrder1, marketOrder2, marketOrder3}, loadedPortfolios);
206205

@@ -210,5 +209,37 @@ public void GetAllByPortfolioEntry_Returns_Correct_Orders()
210209
Assert.Equal(2, loadedPortfoliosSecondary.Count);
211210
Assert.Equal(new List<MarketOrder> {marketOrder4, marketOrder5}, loadedPortfoliosSecondary);
212211
}
212+
213+
[Fact]
214+
public void Delete_Deletes()
215+
{
216+
// fixture unique to this test
217+
var marketOrderRepositoryFixture = new SqlKataMarketOrderRepositoryFixture();
218+
219+
// arrange
220+
var marketOrder1 = new MarketOrder(new Decimal(10000.39), 10, new Decimal(1.1), DateTime.Now, true,
221+
PortfolioEntryId: marketOrderRepositoryFixture.DefaultPortfolioEntryId);
222+
var marketOrder2 = new MarketOrder(new Decimal(11000.39), 11, new Decimal(1.2),
223+
DateTime.Now.Subtract(TimeSpan.FromSeconds(3600)), true,
224+
PortfolioEntryId: marketOrderRepositoryFixture.DefaultPortfolioEntryId);
225+
226+
227+
// act
228+
marketOrder1 = marketOrder1 with
229+
{
230+
Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder1)
231+
};
232+
marketOrder2 = marketOrder2 with
233+
{
234+
Id = marketOrderRepositoryFixture.MarketOrderRepository.Add(marketOrder2)
235+
};
236+
237+
marketOrderRepositoryFixture.MarketOrderRepository.Delete(marketOrder1);
238+
239+
// assert
240+
Assert.Null(marketOrderRepositoryFixture.MarketOrderRepository.Get(marketOrder1.Id));
241+
Assert.Equal(marketOrder2, marketOrderRepositoryFixture.MarketOrderRepository.Get(marketOrder2.Id));
242+
Assert.Equal(1, marketOrderRepositoryFixture.MarketOrderRepository.GetAll().Count);
243+
}
213244
}
214245
}

Tests/Integration/Repository/PortfolioEntryTest.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public class PortfolioEntryRepositoryTest : IClassFixture<SqlKataPortfolioEntryR
3838
{
3939
private SqlKataPortfolioEntryRepositoryFixture _portfolioEntryRepositoryFixture;
4040

41-
// TODO test Delete method
4241
public PortfolioEntryRepositoryTest(SqlKataPortfolioEntryRepositoryFixture marketOrderRepositoryFixture)
4342
{
4443
this._portfolioEntryRepositoryFixture = marketOrderRepositoryFixture;
@@ -157,10 +156,10 @@ public void GetAllByPortfolioId_Returns_Correct_Entries()
157156
var loadedPortfolios =
158157
portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAllByPortfolioId(
159158
portfolioEntryRepositoryFixture.DefaultPortfolioId);
160-
159+
161160
Assert.Empty(presumablyEmptyList);
162161
Assert.Empty(presumablyEmptyList2);
163-
162+
164163
Assert.Equal(new List<PortfolioEntry> {portfolioEntry1, portfolioEntry2, portfolioEntry3},
165164
loadedPortfolios);
166165

@@ -175,13 +174,16 @@ public void GetAllByPortfolioId_Returns_Correct_Entries()
175174
[Fact]
176175
public void AddUpdate_Updates()
177176
{
177+
// fixture unique to this test
178+
var portfolioEntryRepositoryFixture = new SqlKataPortfolioEntryRepositoryFixture();
179+
178180
// arrange
179-
var btcEntry = new PortfolioEntry("btc", _portfolioEntryRepositoryFixture.DefaultPortfolioId);
180-
var ethEntry = new PortfolioEntry("eth", _portfolioEntryRepositoryFixture.DefaultPortfolioId);
181+
var btcEntry = new PortfolioEntry("btc", portfolioEntryRepositoryFixture.DefaultPortfolioId);
182+
var ethEntry = new PortfolioEntry("eth", portfolioEntryRepositoryFixture.DefaultPortfolioId);
181183

182184
// act
183-
int btcId = _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(btcEntry);
184-
int ethId = _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(ethEntry);
185+
int btcId = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(btcEntry);
186+
int ethId = portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(ethEntry);
185187
ethEntry = ethEntry with
186188
{
187189
Id = ethId
@@ -193,10 +195,35 @@ public void AddUpdate_Updates()
193195
// change it's name
194196
Symbol = "ltc"
195197
};
196-
_portfolioEntryRepositoryFixture.PortfolioEntryRepository.Update(btcEntry);
198+
portfolioEntryRepositoryFixture.PortfolioEntryRepository.Update(btcEntry);
199+
200+
Assert.Equal(btcEntry, portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(btcEntry.Id));
201+
Assert.Equal(ethEntry, portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(ethEntry.Id));
202+
}
197203

198-
Assert.Equal(btcEntry, _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(btcEntry.Id));
199-
Assert.Equal(ethEntry, _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(ethEntry.Id));
204+
[Fact]
205+
public void Delete_Deletes()
206+
{
207+
// arrange
208+
var firstEntry = new PortfolioEntry("btc", _portfolioEntryRepositoryFixture.DefaultPortfolioId);
209+
var secondEntry = new PortfolioEntry("ltc", _portfolioEntryRepositoryFixture.DefaultPortfolioId);
210+
211+
// act
212+
firstEntry = firstEntry with
213+
{
214+
Id = _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(firstEntry)
215+
};
216+
217+
secondEntry = secondEntry with
218+
{
219+
Id = _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Add(secondEntry)
220+
};
221+
_portfolioEntryRepositoryFixture.PortfolioEntryRepository.Delete(firstEntry);
222+
223+
// assert
224+
Assert.Null(_portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(firstEntry.Id));
225+
Assert.Equal(secondEntry, _portfolioEntryRepositoryFixture.PortfolioEntryRepository.Get(secondEntry.Id));
226+
Assert.Equal(1, _portfolioEntryRepositoryFixture.PortfolioEntryRepository.GetAll().Count);
200227
}
201228
}
202229
}

Tests/Integration/Repository/PortfolioTest.cs

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class PortfolioTest : IClassFixture<SqlKataPortfolioRepositoryFixture>
3333
{
3434
private SqlKataPortfolioRepositoryFixture _portfolioRepositoryFixture;
3535

36-
// TODO test Delete method
3736
public PortfolioTest(SqlKataPortfolioRepositoryFixture portfolioRepositoryFixture)
3837
{
3938
this._portfolioRepositoryFixture = portfolioRepositoryFixture;
@@ -70,13 +69,13 @@ public void Added_And_Get_AreEqual()
7069
Assert.True(id > 0);
7170
Assert.Equal(portfolio, loaded);
7271
}
73-
72+
7473
[Fact]
7574
public void Added_And_GetAll_AreEqual()
7675
{
7776
// fixture unique to this test
7877
var portfolioRepositoryFixture = new SqlKataPortfolioRepositoryFixture();
79-
78+
8079
// arrange
8180
var portfolio1 = new Portfolio("My new portfolio", "Lorem ipsum dolor sit amet", Currency.Czk);
8281
var portfolio2 = new Portfolio("My second portfolio", "Lorem ipsum dolor sit amet", Currency.Eur);
@@ -86,13 +85,13 @@ public void Added_And_GetAll_AreEqual()
8685
portfolio1 = portfolio1 with {Id = portfolioRepositoryFixture.PortfolioRepository.Add(portfolio1)};
8786
portfolio2 = portfolio2 with {Id = portfolioRepositoryFixture.PortfolioRepository.Add(portfolio2)};
8887
portfolio3 = portfolio3 with {Id = portfolioRepositoryFixture.PortfolioRepository.Add(portfolio3)};
89-
88+
9089
// assert
9190
var loadedPortfolios = portfolioRepositoryFixture.PortfolioRepository.GetAll();
9291
Assert.Equal(3, loadedPortfolios.Count);
93-
Assert.Equal(new List<Portfolio>{portfolio1, portfolio2, portfolio3}, loadedPortfolios);
92+
Assert.Equal(new List<Portfolio> {portfolio1, portfolio2, portfolio3}, loadedPortfolios);
9493
}
95-
94+
9695
[Fact]
9796
public void AddUpdate_Updates()
9897
{
@@ -115,10 +114,36 @@ public void AddUpdate_Updates()
115114
Currency = Currency.Eur
116115
};
117116
_portfolioRepositoryFixture.PortfolioRepository.Update(firstPortfolio);
118-
117+
119118
Assert.Equal(firstPortfolio, _portfolioRepositoryFixture.PortfolioRepository.Get(firstPortfolio.Id));
120119
Assert.Equal(secondPortfolio, _portfolioRepositoryFixture.PortfolioRepository.Get(secondPortfolio.Id));
121120
}
122121

122+
[Fact]
123+
public void Delete_Deletes()
124+
{
125+
// fixture unique to this test
126+
var portfolioRepositoryFixture = new SqlKataPortfolioRepositoryFixture();
127+
128+
// arrange
129+
var firstPortfolio = new Portfolio("My new portfolio", "Lorem ipsum dolor sit amet", Currency.Usd);
130+
var secondPortfolio = new Portfolio("My second new portfolio", "Lorem ipsum dolor sit amet", Currency.Eur);
131+
132+
// act
133+
firstPortfolio = firstPortfolio with
134+
{
135+
Id = portfolioRepositoryFixture.PortfolioRepository.Add(firstPortfolio)
136+
};
137+
secondPortfolio = secondPortfolio with
138+
{
139+
Id = portfolioRepositoryFixture.PortfolioRepository.Add(secondPortfolio)
140+
};
141+
portfolioRepositoryFixture.PortfolioRepository.Delete(firstPortfolio);
142+
143+
// assert
144+
Assert.Null(portfolioRepositoryFixture.PortfolioRepository.Get(firstPortfolio.Id));
145+
Assert.Equal(secondPortfolio, portfolioRepositoryFixture.PortfolioRepository.Get(secondPortfolio.Id));
146+
Assert.Equal(1, portfolioRepositoryFixture.PortfolioRepository.GetAll().Count);
147+
}
123148
}
124149
}

0 commit comments

Comments
 (0)