using System;
using System.Collections.Generic;
using System.Linq;
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)
: base(settingsService, cache)
{
EnsureFileExists(BOOKING_RULES);
}
///
/// Load booking rules
///
public List GetBookingRuleList() => LoadList(BOOKING_RULES);
///
/// Save a booking rule (insert/update)
///
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);
}
///
/// Get booking rule
///
public BookingRule GetBookingRule(string id)
{
var bookingRules = GetBookingRuleList();
return bookingRules.FirstOrDefault(t => t.Id == id);
}
///
/// Delete booking rule
///
public void DeleteBookingRule(string id)
{
var bookingRules = GetBookingRuleList();
bookingRules.RemoveAll(t => t.Id == id);
Save(bookingRules, BOOKING_RULES);
}
}
}