@@ -8,57 +8,98 @@ namespace Services
8
8
{
9
9
public interface IPortfolioEntryService
10
10
{
11
+ /// <summary>
12
+ /// Creates a new portfolio entry and adds it to a repository
13
+ /// </summary>
14
+ /// <param name="symbol">Cryptocurrency symbol</param>
15
+ /// <param name="portfolioId">ID of the portfolio the entry should belong to</param>
16
+ /// <returns>Created instance of the `PortfolioEntry` class</returns>
11
17
PortfolioEntry CreatePortfolioEntry ( string symbol , int portfolioId ) ;
12
18
19
+ /// <summary>
20
+ /// Deletes the given portfolio entry from a repository
21
+ /// </summary>
22
+ /// <param name="entry">Entry to be deleted</param>
23
+ /// <returns>A flag indicating whether the entry was deleted</returns>
13
24
bool DeletePortfolioEntry ( PortfolioEntry entry ) ;
14
25
26
+ /// <summary>
27
+ ///
28
+ /// </summary>
29
+ /// <param name="portfolioId"></param>
30
+ /// <returns></returns>
15
31
int DeletePortfolioEntries ( int portfolioId ) ;
16
32
17
- bool UpdatePortfolio ( PortfolioEntry entry ) ;
33
+ /// <summary>
34
+ /// Updates the given entry in the repository. The entry with the same ID in the repository is replaced with the one.
35
+ /// </summary>
36
+ /// <param name="entry">Updated entry to be stored in the repository</param>
37
+ /// <returns>A flag indicating whether the portfolio entry was updated</returns>
38
+ bool UpdatePortfolioEntry ( PortfolioEntry entry ) ;
18
39
40
+ /// <summary>
41
+ /// Loads and returns a portfolio entry from the database using the given ID.
42
+ /// </summary>
43
+ /// <param name="id">ID of the portfolio entry to be loaded</param>
44
+ /// <returns>Found portfolio entry or `null`</returns>
19
45
PortfolioEntry GetPortfolioEntry ( int id ) ;
20
46
47
+ /// <summary>
48
+ /// Returns all portfolio entries that belong to the portfolio identified by the given ID
49
+ /// </summary>
50
+ /// <param name="portfolioId">ID of the portfolio whose entries should be found</param>
51
+ /// <returns>A list of all entries that belong to the given portfolio</returns>
21
52
List < PortfolioEntry > GetPortfolioEntries ( int portfolioId ) ;
22
53
}
23
54
24
55
public class PortfolioEntryServiceImpl : IPortfolioEntryService
25
56
{
26
- private IPortfolioEntryRepository _portfolioEntryRepository ;
27
- private IMarketOrderService _marketOrderService ;
57
+ // dependency on the portfolio entry repository
58
+ private readonly IPortfolioEntryRepository _portfolioEntryRepository ;
59
+
60
+ // dependency on the market order repository
61
+ private readonly IMarketOrderService _marketOrderService ;
28
62
29
63
public PortfolioEntryServiceImpl ( IPortfolioEntryRepository portfolioEntryRepository , IMarketOrderService marketOrderService )
30
64
{
31
65
_portfolioEntryRepository = portfolioEntryRepository ;
32
66
_marketOrderService = marketOrderService ;
33
67
}
34
68
35
-
36
69
public PortfolioEntry CreatePortfolioEntry ( string symbol , int portfolioId )
37
70
{
71
+ // create a new instance of the `PortfolioEntry` class
38
72
var portfolioEntry = new PortfolioEntry ( symbol , portfolioId ) ;
39
- portfolioEntry = portfolioEntry with { Id = _portfolioEntryRepository . Add ( portfolioEntry ) } ;
40
- return portfolioEntry ;
73
+
74
+ // add it to the repository and return it with the generated ID
75
+ return portfolioEntry with { Id = _portfolioEntryRepository . Add ( portfolioEntry ) } ;
41
76
}
42
77
43
78
public bool DeletePortfolioEntry ( PortfolioEntry entry )
44
79
{
80
+ // when deleting a portfolio entry make sure to delete all of its orders
45
81
_marketOrderService . DeletePortfolioEntryOrders ( entry . Id ) ;
82
+
83
+ // finally delete the portfolio entry
46
84
return _portfolioEntryRepository . Delete ( entry ) ;
47
85
}
48
86
49
- public bool UpdatePortfolio ( PortfolioEntry entry ) => _portfolioEntryRepository . Update ( entry ) ;
87
+ public bool UpdatePortfolioEntry ( PortfolioEntry entry ) => _portfolioEntryRepository . Update ( entry ) ;
50
88
51
89
public PortfolioEntry GetPortfolioEntry ( int id ) => _portfolioEntryRepository . Get ( id ) ;
52
90
53
91
public List < PortfolioEntry > GetPortfolioEntries ( int portfolioId ) => _portfolioEntryRepository . GetAllByPortfolioId ( portfolioId ) ;
54
92
55
93
public int DeletePortfolioEntries ( int portfolioId )
56
94
{
95
+ // iterate over all entries of the given portfolio
57
96
foreach ( var portfolioEntry in GetPortfolioEntries ( portfolioId ) )
58
97
{
98
+ // delete all orders of each iterated portfolio entry
59
99
_marketOrderService . DeletePortfolioEntryOrders ( portfolioEntry . Id ) ;
60
100
}
61
-
101
+
102
+ // finally delete entries of the portfolio
62
103
return _portfolioEntryRepository . DeletePortfolioEntries ( portfolioId ) ;
63
104
}
64
105
}
0 commit comments