BaseRepository takes an ILogger via its constructor and logs the failing file path and exception in Save's catch block before rethrowing. Every concrete repository takes ILogger<ThisRepo> and passes it up so failures are categorised by repository type. Pulls Microsoft.Extensions.Logging.Abstractions into the Db project. TransactionRepositoryTestShould and AccountRepositoryTestShould pass NullLogger<T>.Instance. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
64 lines
No EOL
2.3 KiB
C#
64 lines
No EOL
2.3 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Microsoft.Extensions.Logging;
|
|
using Schaad.Accounting.Datasets;
|
|
using Schaad.Accounting.Interfaces;
|
|
using Schaad.Accounting.Models;
|
|
|
|
namespace Schaad.Accounting.Repositories
|
|
{
|
|
public class BankTransactionRepository : BaseRepository, IBankTransactionRepository
|
|
{
|
|
private readonly string BANK_TRANSACTIONS = "BankTransactions.xml";
|
|
|
|
public BankTransactionRepository(ISettingsService settingsService, RepositoryCache cache, ILogger<BankTransactionRepository> logger)
|
|
: base(settingsService, cache, logger)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load bank transactions
|
|
/// </summary>
|
|
public List<BankTransaction> GetBankTransactionList() => LoadList<BankTransaction>(BANK_TRANSACTIONS);
|
|
|
|
/// <summary>
|
|
/// Save new bank transactions, ignore existing ones (no update)
|
|
/// </summary>
|
|
public int SaveBankTransactionList(string bankAccountNumber, List<BankTransactionDataset> bankTransactions)
|
|
{
|
|
var count = 0;
|
|
var transactions = GetBankTransactionList();
|
|
foreach (var bankTrx in bankTransactions)
|
|
{
|
|
var transaction = transactions.FirstOrDefault(t => t.Id == bankTrx.Id);
|
|
if (transaction == null)
|
|
{
|
|
transaction = new BankTransaction()
|
|
{
|
|
Id = bankTrx.Id,
|
|
BookingDate = bankTrx.BookingDate,
|
|
ValueDate = bankTrx.ValueDate,
|
|
Value = (decimal)bankTrx.Value,
|
|
Text = bankTrx.Text,
|
|
BankAccountNumber = bankAccountNumber,
|
|
Debtor = bankTrx.Debtor,
|
|
Creditor = bankTrx.Creditor,
|
|
};
|
|
transactions.Add(transaction);
|
|
count++;
|
|
}
|
|
}
|
|
Save(transactions, BANK_TRANSACTIONS);
|
|
return count;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get bank transaction
|
|
/// </summary>
|
|
public BankTransaction GetBankTransaction(string id)
|
|
{
|
|
var transactions = GetBankTransactionList();
|
|
return transactions.FirstOrDefault(t => t.Id == id);
|
|
}
|
|
}
|
|
} |