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

68 lines
2.2 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 SubclassRepository : BaseRepository, ISubclassRepository
{
private readonly Dictionary<int, string> classes = new Dictionary<int, string>();
private readonly string SUBCLASSES = "SubClasses.json";
public SubclassRepository(ISettingsService settingsService, RepositoryCache cache, ILogger<SubclassRepository> logger)
: base(settingsService, cache, logger)
{
EnsureFileExists(SUBCLASSES);
classes.Add(ClassIds.Activa, "Aktiven");
classes.Add(ClassIds.Passiva, "Passiven");
classes.Add(ClassIds.Income, "Einnahmen");
classes.Add(ClassIds.Expenses, "Ausgaben");
}
public Task<List<SubClass>> GetSubClassListAsync() => LoadListAsync<SubClass>(SUBCLASSES);
public async Task SaveSubClassAsync(SubClass subClass)
{
var subclasses = await GetSubClassListAsync();
var idx = subclasses.FindIndex(s => s.Id == subClass.Id);
if (idx >= 0)
{
subclasses[idx] = subClass;
}
else
{
subClass.Id = Guid.NewGuid().ToString();
subclasses.Add(subClass);
}
await SaveAsync(subclasses, SUBCLASSES);
}
public async Task<SubClass> GetSubClassAsync(string id)
{
var subclasses = await GetSubClassListAsync();
return subclasses.FirstOrDefault(t => t.Id == id);
}
public async Task DeleteSubClassAsync(string id)
{
var subclasses = await GetSubClassListAsync();
subclasses.RemoveAll(t => t.Id == id);
await SaveAsync(subclasses, SUBCLASSES);
}
public Dictionary<int, string> GetClasses()
{
return classes;
}
public string GetClass(int classId)
{
return classes[classId];
}
}
}