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 BookingRuleRepository : BaseRepository, IBookingRuleRepository
|
|
{
|
|
private readonly string BOOKING_RULES = "BookingRules.xml";
|
|
|
|
public BookingRuleRepository(ISettingsService settingsService, RepositoryCache cache, ILogger<BookingRuleRepository> logger)
|
|
: base(settingsService, cache, logger)
|
|
{
|
|
EnsureFileExists(BOOKING_RULES);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Load booking rules
|
|
/// </summary>
|
|
public List<BookingRule> GetBookingRuleList() => LoadList<BookingRule>(BOOKING_RULES);
|
|
|
|
/// <summary>
|
|
/// Save a booking rule (insert/update)
|
|
/// </summary>
|
|
public void SaveBookingRule(BookingRule bookingRule)
|
|
{
|
|
var bookingRules = GetBookingRuleList();
|
|
var existingRule = bookingRules.FirstOrDefault(a => a.Id == bookingRule.Id);
|
|
|
|
if (existingRule == null)
|
|
{
|
|
existingRule = new BookingRule();
|
|
bookingRules.Add(existingRule);
|
|
bookingRule.Id = Guid.NewGuid().ToString();
|
|
}
|
|
bookingRule.Copy(existingRule);
|
|
Save(bookingRules, BOOKING_RULES);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get booking rule
|
|
/// </summary>
|
|
public BookingRule GetBookingRule(string id)
|
|
{
|
|
var bookingRules = GetBookingRuleList();
|
|
return bookingRules.FirstOrDefault(t => t.Id == id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Delete booking rule
|
|
/// </summary>
|
|
public void DeleteBookingRule(string id)
|
|
{
|
|
var bookingRules = GetBookingRuleList();
|
|
bookingRules.RemoveAll(t => t.Id == id);
|
|
Save(bookingRules, BOOKING_RULES);
|
|
}
|
|
}
|
|
} |