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
{
///
/// Water 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 WaterBillRepository : BaseRepository, IWaterBillRepository
{
private const string WATER = "water.json";
public WaterBillRepository(ISettingsService settingsService, RepositoryCache cache, ILogger logger)
: base(settingsService, cache, logger)
{
}
public Task> GetWaterBillListAsync()
=> LoadListAsync(settingsService.GetMandatorPath(), WATER);
public async Task SaveWaterBillAsync(WaterBill bill)
{
var bills = await GetWaterBillListAsync();
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);
}
public async Task GetWaterBillAsync(string id)
{
var bills = await GetWaterBillListAsync();
return bills.FirstOrDefault(b => b.Id == id);
}
public async Task DeleteWaterBillAsync(string id)
{
var bills = await GetWaterBillListAsync();
bills.RemoveAll(b => b.Id == id);
await SaveAsync(bills, settingsService.GetMandatorPath(), WATER);
}
}
}