AccountingNext/Schaad.Accounting.Db/Repositories/BookingTextRepository.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

52 lines
1.7 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Schaad.Accounting.Interfaces;
using Schaad.Accounting.Models;
namespace Schaad.Accounting.Repositories
{
public class BookingTextRepository : BaseRepository, IBookingTextRepository
{
private readonly string BOOKING_TEXTS = "BookingTexts.json";
public BookingTextRepository(ISettingsService settingsService, RepositoryCache cache, ILogger<BookingTextRepository> logger)
: base(settingsService, cache, logger)
{
EnsureFileExists(BOOKING_TEXTS);
}
public Task<List<BookingText>> GetBookingTextListAsync() => LoadListAsync<BookingText>(BOOKING_TEXTS);
public async Task SaveBookingTextAsync(BookingText bookingText)
{
var bookingTexts = await GetBookingTextListAsync();
var idx = bookingTexts.FindIndex(t => t.Id == bookingText.Id);
if (idx >= 0)
{
bookingTexts[idx] = bookingText;
}
else
{
bookingText.Id = Guid.NewGuid().ToString();
bookingTexts.Add(bookingText);
}
await SaveAsync(bookingTexts, BOOKING_TEXTS);
}
public async Task<BookingText> GetBookingTextAsync(string id)
{
var bookingTexts = await GetBookingTextListAsync();
return bookingTexts.FirstOrDefault(t => t.Id == id);
}
public async Task DeleteBookingTextAsync(string id)
{
var bookingTexts = await GetBookingTextListAsync();
bookingTexts.RemoveAll(t => t.Id == id);
await SaveAsync(bookingTexts, BOOKING_TEXTS);
}
}
}