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>
62 lines
No EOL
2 KiB
C#
62 lines
No EOL
2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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.xml";
|
|
|
|
public BookingTextRepository(ISettingsService settingsService, RepositoryCache cache, ILogger<BookingTextRepository> logger)
|
|
: base(settingsService, cache, logger)
|
|
{
|
|
EnsureFileExists(BOOKING_TEXTS);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load booking texts
|
|
/// </summary>
|
|
public List<BookingText> GetBookingTextList() => LoadList<BookingText>(BOOKING_TEXTS);
|
|
|
|
/// <summary>
|
|
/// Save a booking text (insert/update)
|
|
/// </summary>
|
|
public void SaveBookingText(BookingText bookingText)
|
|
{
|
|
var bookingTexts = GetBookingTextList();
|
|
var existingText = bookingTexts.FirstOrDefault(a => a.Id == bookingText.Id);
|
|
|
|
if (existingText == null)
|
|
{
|
|
existingText = new BookingText();
|
|
bookingTexts.Add(existingText);
|
|
bookingText.Id = Guid.NewGuid().ToString();
|
|
}
|
|
bookingText.Copy(existingText);
|
|
Save(bookingTexts, BOOKING_TEXTS);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get booking text
|
|
/// </summary>
|
|
public BookingText GetBookingText(string id)
|
|
{
|
|
var bookingTexts = GetBookingTextList();
|
|
return bookingTexts.FirstOrDefault(t => t.Id == id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete booking text
|
|
/// </summary>
|
|
public void DeleteBookingText(string id)
|
|
{
|
|
var bookingTexts = GetBookingTextList();
|
|
bookingTexts.RemoveAll(t => t.Id == id);
|
|
Save(bookingTexts, BOOKING_TEXTS);
|
|
}
|
|
}
|
|
} |