|
1 |
| -using System; |
2 | 1 | using System.Collections.Generic;
|
3 | 2 | using System.Linq;
|
4 | 3 | using Database;
|
@@ -44,21 +43,26 @@ public SqlKataRepository(SqlKataDatabase db, string tableName)
|
44 | 43 | /// <returns>ID of the given object</returns>
|
45 | 44 | protected abstract int GetEntryId(T entry);
|
46 | 45 |
|
| 46 | + /// <summary> |
| 47 | + /// Returns the name of the primary key column |
| 48 | + /// </summary> |
| 49 | + protected string GetPrimaryKeyColumnName => SqlSchema.TableIdPrimaryKey; |
| 50 | + |
47 | 51 | // use the InsertGetId method of the SqlKata library in order to insert a new row and get it's ID
|
48 | 52 | public int Add(T entry) => Db.Get().Query(TableName).InsertGetId<int>(ToRow(entry));
|
49 | 53 |
|
50 | 54 | // updates the given object of type T using the WHERE and UPDATE method calls, returns a boolean flag indicating
|
51 | 55 | // whether at least one table row was updated
|
52 | 56 | public bool Update(T entry) =>
|
53 |
| - Db.Get().Query(TableName).Where(SqlSchema.TableIdPrimaryKey, GetEntryId(entry)).Update(ToRow(entry)) > 0; |
| 57 | + Db.Get().Query(TableName).Where(GetPrimaryKeyColumnName, GetEntryId(entry)).Update(ToRow(entry)) > 0; |
54 | 58 |
|
55 | 59 | // 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; |
| 60 | + public bool Delete(T entry) => Db.Get().Query(TableName).Where(GetPrimaryKeyColumnName, GetEntryId(entry)).Delete() > 0; |
57 | 61 |
|
58 | 62 | public T Get(int id)
|
59 | 63 | {
|
60 | 64 | // find a table rows based on the given ID
|
61 |
| - var result = Db.Get().Query(TableName).Where(SqlSchema.TableIdPrimaryKey, id).FirstOrDefault(); |
| 65 | + var result = Db.Get().Query(TableName).Where(GetPrimaryKeyColumnName, id).FirstOrDefault(); |
62 | 66 |
|
63 | 67 | // convert the given table row a ta an object of type T
|
64 | 68 | return result == null ? default : FromRow(result);
|
|
0 commit comments