AccountingNext/Schaad.Accounting.Db/Repositories/BankTransactionRepository.cs
Claudio Schaad 1c7ba7f416 PR D: per-scope repository cache
Add a Scoped RepositoryCache that memoizes deserialized XML lists,
keyed by absolute file path. Each XML file is loaded at most once per
SignalR circuit; Save invalidates the corresponding entry so
subsequent reads reload from disk.

BaseRepository gains a LoadList<T>(fileName) helper; each of the seven
repositories now defines Get*List() as a one-liner over that helper
and takes RepositoryCache via the constructor.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-07-02 20:52:24 +02:00

63 lines
No EOL
2.2 KiB
C#

using System.Collections.Generic;
using System.Linq;
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)
: base(settingsService, cache)
{
}
/// <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);
}
}
}