Skip to content

Commit df7a953

Browse files
committed
Added documentation to IRepository SqlKata implementations
1 parent 6d12b2b commit df7a953

File tree

5 files changed

+90
-67
lines changed

5 files changed

+90
-67
lines changed

Database/SqlSchema.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ namespace Database
44
{
55
public class SqlSchema
66
{
7+
public const string TableIdPrimaryKey = "id";
8+
79
public const string TablePortfolios = "portfolios";
8-
public const string PortfoliosId = "id";
10+
public const string PortfoliosId = TableIdPrimaryKey;
911
public const string PortfoliosName = "name";
1012
public const string PortfoliosDescription = "description";
1113
public const string PortfoliosCurrencyCode = "currency_code";
1214

1315
public const string TablePortfolioEntries = "portfolio_entries";
14-
public const string PortfolioEntriesId = "id";
16+
public const string PortfolioEntriesId = TableIdPrimaryKey;
1517
public const string PortfolioEntriesSymbol = "symbol";
1618
public const string PortfolioEntriesPortfolioId = "portfolio_id";
1719

1820
public const string TableMarketOrders = "market_orders";
19-
public const string MarketOrdersId = "id";
21+
public const string MarketOrdersId = TableIdPrimaryKey;
2022
public const string MarketOrdersFilledPrice = "filled_price";
2123
public const string MarketOrdersFee = "fee";
2224
public const string MarketOrdersSize = "size";

Repository/SqlKataMarketOrderRepository.cs

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,38 +7,54 @@
77

88
namespace Repository
99
{
10+
/// <summary>
11+
/// Implements the IMarketOrderRepository by extending SqlKataRepository and implementing necessary methods
12+
/// </summary>
1013
public class SqlKataMarketOrderRepository : SqlKataRepository<MarketOrder>, IMarketOrderRepository
1114
{
15+
// decimal precision to be used when storing order price and size (enough to support satoshis - smallest unit of bitcoin)
1216
private const int DecimalPrecision = 100000000;
13-
17+
1418
public SqlKataMarketOrderRepository(SqlKataDatabase db) : base(db, SqlSchema.TableMarketOrders)
1519
{
1620
}
1721

18-
protected override int _getEntryId(MarketOrder entry) => entry.Id;
22+
protected override int GetEntryId(MarketOrder entry) => entry.Id;
1923

20-
public override object ToRow(MarketOrder entry)
24+
protected override object ToRow(MarketOrder entry)
2125
{
2226
return new
2327
{
28+
// make sure to multiply the price, fee and size with the decimal precision
2429
filled_price = (long) (entry.FilledPrice * DecimalPrecision),
2530
fee = (long) (entry.Fee * DecimalPrecision),
2631
size = (long) (entry.Size * DecimalPrecision),
32+
// date stored as unix timestamps
2733
date = ((DateTimeOffset) entry.Date).ToUnixTimeSeconds(),
2834
buy = entry.Buy ? 1 : 0,
2935
portfolio_entry_id = entry.PortfolioEntryId,
3036
};
3137
}
3238

33-
public override MarketOrder FromRow(dynamic d) =>
34-
new(Decimal.Divide(d.filled_price, DecimalPrecision), Decimal.Divide(d.fee, DecimalPrecision), Decimal.Divide(d.size, DecimalPrecision),
35-
DateTimeOffset.FromUnixTimeSeconds((int) d.date).DateTime.ToLocalTime(), d.buy > 0,
36-
(int) d.id, (int) d.portfolio_entry_id);
39+
protected override MarketOrder FromRow(dynamic d) =>
40+
// make sure to divide the price, fee and size by the decimal precision
41+
new(
42+
Decimal.Divide(d.filled_price, DecimalPrecision),
43+
Decimal.Divide(d.fee, DecimalPrecision),
44+
Decimal.Divide(d.size, DecimalPrecision),
45+
DateTimeOffset.FromUnixTimeSeconds((int) d.date).DateTime.ToLocalTime(),
46+
d.buy > 0,
47+
(int) d.id,
48+
(int) d.portfolio_entry_id
49+
);
3750

3851
public List<MarketOrder> GetAllByPortfolioEntryId(int portfolioEntryId) =>
39-
RowsToObjects(Db.Get().Query(tableName).Where(SqlSchema.MarketOrdersPortfolioEntryId, portfolioEntryId).Get());
52+
// implement the method using the WHERE statement
53+
RowsToObjects(Db.Get().Query(TableName).Where(SqlSchema.MarketOrdersPortfolioEntryId, portfolioEntryId)
54+
.Get());
4055

4156
public int DeletePortfolioEntryOrders(int portfolioEntryId) =>
42-
Db.Get().Query(tableName).Where(SqlSchema.MarketOrdersPortfolioEntryId, portfolioEntryId).Delete();
57+
// implement the method using the WHERE statement
58+
Db.Get().Query(TableName).Where(SqlSchema.MarketOrdersPortfolioEntryId, portfolioEntryId).Delete();
4359
}
4460
}

Repository/SqlKataPortfolioEntryRepository.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,29 @@
55

66
namespace Repository
77
{
8+
/// <summary>
9+
/// Implements the IPortfolioEntryRepository by extending SqlKataRepository and implementing necessary methods
10+
/// </summary>
811
public class SqlKataPortfolioEntryRepository : SqlKataRepository<PortfolioEntry>, IPortfolioEntryRepository
912
{
1013
public SqlKataPortfolioEntryRepository(SqlKataDatabase db) : base(db, SqlSchema.TablePortfolioEntries)
1114
{
1215
}
1316

14-
protected override int _getEntryId(PortfolioEntry entry) => entry.Id;
17+
protected override int GetEntryId(PortfolioEntry entry) => entry.Id;
1518

16-
public override object ToRow(PortfolioEntry entry) => new
19+
protected override object ToRow(PortfolioEntry entry) => new
1720
{
1821
symbol = entry.Symbol,
1922
portfolio_id = entry.PortfolioId
2023
};
2124

22-
public override PortfolioEntry FromRow(dynamic d) => new((string) d.symbol, (int) d.portfolio_id, (int) d.id);
25+
protected override PortfolioEntry FromRow(dynamic d) => new((string) d.symbol, (int) d.portfolio_id, (int) d.id);
2326

2427
public List<PortfolioEntry> GetAllByPortfolioId(int portfolioId) =>
25-
RowsToObjects(Db.Get().Query(tableName).Where(SqlSchema.PortfolioEntriesPortfolioId, portfolioId).Get());
28+
RowsToObjects(Db.Get().Query(TableName).Where(SqlSchema.PortfolioEntriesPortfolioId, portfolioId).Get());
2629

2730
public int DeletePortfolioEntries(int portfolioId) =>
28-
Db.Get().Query(tableName).Where(SqlSchema.PortfolioEntriesPortfolioId, portfolioId).Delete();
31+
Db.Get().Query(TableName).Where(SqlSchema.PortfolioEntriesPortfolioId, portfolioId).Delete();
2932
}
3033
}

Repository/SqlKataPortfolioRepository.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,26 @@
33

44
namespace Repository
55
{
6+
7+
/// <summary>
8+
/// Implements the IPortfolioRepository by extending SqlKataRepository and implementing necessary methods
9+
/// </summary>
610
public class SqlKataPortfolioRepository : SqlKataRepository<Portfolio>, IPortfolioRepository
711
{
812
public SqlKataPortfolioRepository(SqlKataDatabase db) : base(db, SqlSchema.TablePortfolios)
913
{
1014
}
1115

12-
protected override int _getEntryId(Portfolio entry) => entry.Id;
16+
protected override int GetEntryId(Portfolio entry) => entry.Id;
1317

14-
public override object ToRow(Portfolio entry) => new
18+
protected override object ToRow(Portfolio entry) => new
1519
{
1620
name = entry.Name,
1721
description = entry.Description,
1822
currency_code = (int) entry.Currency
1923
};
2024

21-
public override Portfolio FromRow(dynamic d) =>
25+
protected override Portfolio FromRow(dynamic d) =>
2226
new((string) d.name, (string) d.description, (Currency) d.currency_code, (int) d.id);
2327
}
2428
}

Repository/SqlKataRepository.cs

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,68 +9,66 @@ namespace Repository
99
// Implements IRepository using a SqlKataDatabase
1010
public abstract class SqlKataRepository<T> : IRepository<T>
1111
{
12+
// hold a reference to the SqlKataDatabase instance
1213
protected readonly SqlKataDatabase Db;
13-
protected readonly string tableName;
14+
15+
// name of the table this repository utilizes
16+
protected readonly string TableName;
1417

18+
/// <param name="db">Instance of the database object</param>
19+
/// <param name="tableName">Name of the table to be utilized</param>
1520
public SqlKataRepository(SqlKataDatabase db, string tableName)
1621
{
17-
this.Db = db;
18-
this.tableName = tableName;
22+
Db = db;
23+
TableName = tableName;
1924
}
2025

21-
public abstract object ToRow(T entry);
26+
/// <summary>
27+
/// Converts a object of type T to a generic untyped object (to a table row)
28+
/// </summary>
29+
/// <param name="entry">An object of type T to be converted</param>
30+
/// <returns>Generic object (table row) representing the given object of type T</returns>
31+
protected abstract object ToRow(T entry);
2232

23-
public abstract T FromRow(dynamic d);
33+
/// <summary>
34+
/// Converts a generic object (table row) to an object of type T
35+
/// </summary>
36+
/// <param name="d">Object to be converted to an object of type T</param>
37+
/// <returns>Returns an object of type T converted from the generic object (table row)</returns>
38+
protected abstract T FromRow(dynamic d);
2439

25-
protected abstract int _getEntryId(T entry);
40+
/// <summary>
41+
/// Returns an ID of the given object of type T
42+
/// </summary>
43+
/// <param name="entry">Object whose ID should be returner</param>
44+
/// <returns>ID of the given object</returns>
45+
protected abstract int GetEntryId(T entry);
2646

27-
public int Add(T entry)
28-
{
29-
var id = Db.Get().Query(tableName).InsertGetId<int>(ToRow(entry));
30-
return id;
31-
}
32-
33-
public bool Update(T entry)
34-
{
35-
var updated = Db.Get().Query(tableName).Where("id", _getEntryId(entry)).Update(ToRow(entry));
36-
return updated > 0;
37-
}
47+
// use the InsertGetId method of the SqlKata library in order to insert a new row and get it's ID
48+
public int Add(T entry) => Db.Get().Query(TableName).InsertGetId<int>(ToRow(entry));
3849

39-
public bool Delete(T entry)
40-
{
41-
Db.Get().Query(tableName).Where("id", _getEntryId(entry)).Delete();
42-
return true;
43-
}
50+
// updates the given object of type T using the WHERE and UPDATE method calls, returns a boolean flag indicating
51+
// whether at least one table row was updated
52+
public bool Update(T entry) =>
53+
Db.Get().Query(TableName).Where(SqlSchema.TableIdPrimaryKey, GetEntryId(entry)).Update(ToRow(entry)) > 0;
4454

55+
// deletes the given object of type T and returns a boolean flag indicating whether at least one table row was deleted
56+
public bool Delete(T entry) => Db.Get().Query(TableName).Where(SqlSchema.TableIdPrimaryKey, GetEntryId(entry)).Delete() > 0;
57+
4558
public T Get(int id)
4659
{
47-
var result = Db.Get().Query(tableName).Where("id", id).FirstOrDefault();
48-
if (result == null)
49-
{
50-
return default;
51-
}
52-
53-
return FromRow(result);
60+
// find a table rows based on the given ID
61+
var result = Db.Get().Query(TableName).Where(SqlSchema.TableIdPrimaryKey, id).FirstOrDefault();
62+
63+
// convert the given table row a ta an object of type T
64+
return result == null ? default : FromRow(result);
5465
}
5566

56-
public List<T> GetAll() => RowsToObjects(Db.Get().Query(tableName).Select().Get());
67+
// select all rows from the database and converts them to objects of type T
68+
public List<T> GetAll() => RowsToObjects(Db.Get().Query(TableName).Select().Get());
5769

58-
protected List<T> RowsToObjects(IEnumerable<dynamic> rows)
59-
{
60-
List<T> items = new List<T>();
61-
foreach (var row in rows)
62-
{
63-
items.Add(FromRow(row));
64-
}
65-
66-
return items;
67-
}
68-
}
69-
70-
public class SqlKataRepositoryException : Exception
71-
{
72-
public SqlKataRepositoryException(string message) : base(message)
73-
{
74-
}
70+
// converts a collection of table rows to a list of objects of type T
71+
protected List<T> RowsToObjects(IEnumerable<dynamic> rows) => rows.Select(row => (T) FromRow(row)).ToList();
72+
7573
}
7674
}

0 commit comments

Comments
 (0)