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
{
///
/// NeoVac water and heating bills of the current mandator, stored once per mandator (not per
/// year like the other data files) because the billing period doesn't align with calendar years.
///
public class WaterHeatingBillRepository : BaseRepository, IWaterHeatingBillRepository
{
private const string WATER_HEATING = "water-heating.json";
public WaterHeatingBillRepository(ISettingsService settingsService, RepositoryCache cache, ILogger logger)
: base(settingsService, cache, logger)
{
}
public Task> GetWaterHeatingBillListAsync()
=> LoadListAsync(settingsService.GetMandatorPath(), WATER_HEATING);
public async Task SaveWaterHeatingBillAsync(WaterHeatingBill bill)
{
var bills = await GetWaterHeatingBillListAsync();
var idx = bills.FindIndex(b => b.Id == bill.Id);
if (idx >= 0)
{
bills[idx] = bill;
}
else
{
bill.Id = Guid.NewGuid().ToString();
bills.Add(bill);
}
await SaveAsync(bills, settingsService.GetMandatorPath(), WATER_HEATING);
}
public async Task GetWaterHeatingBillAsync(string id)
{
var bills = await GetWaterHeatingBillListAsync();
return bills.FirstOrDefault(b => b.Id == id);
}
public async Task DeleteWaterHeatingBillAsync(string id)
{
var bills = await GetWaterHeatingBillListAsync();
bills.RemoveAll(b => b.Id == id);
await SaveAsync(bills, settingsService.GetMandatorPath(), WATER_HEATING);
}
}
}