AccountingNext/Schaad.Accounting.Db/Repositories/BankTransactionRepository.cs
Claudio Schaad 24bdfe60bc Store data as JSON instead of XML, with startup migration
Repositories now read and write indented, human-readable JSON
(System.Text.Json, shared settings in JsonStorage). File names and folders
are unchanged apart from the extension.

XmlToJsonMigrator runs at startup before the app serves requests. It converts
the known Data/*.xml files under every year/mandator, verifies the JSON reads
back to the same data, and moves the XML to
{year}/backup/{mandator}/xml-{timestamp}/. Bank-statement XMLs in uploads/ are
never touched. Any failure stops startup and leaves that file's XML in place.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 13:34:23 +02:00

59 lines
2.3 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
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.json";
public BankTransactionRepository(ISettingsService settingsService, RepositoryCache cache, ILogger<BankTransactionRepository> logger)
: base(settingsService, cache, logger)
{
}
public Task<List<BankTransaction>> GetBankTransactionListAsync() => LoadListAsync<BankTransaction>(BANK_TRANSACTIONS);
/// <summary>
/// Save new bank transactions, ignore existing ones (no update)
/// </summary>
public async Task<int> SaveBankTransactionListAsync(string bankAccountNumber, List<BankTransactionDataset> bankTransactions)
{
var count = 0;
var transactions = await GetBankTransactionListAsync();
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++;
}
}
await SaveAsync(transactions, BANK_TRANSACTIONS);
return count;
}
public async Task<BankTransaction> GetBankTransactionAsync(string id)
{
var transactions = await GetBankTransactionListAsync();
return transactions.FirstOrDefault(t => t.Id == id);
}
}
}