AccountingNext/Schaad.Accounting.Db/Repositories/WaterBillRepository.cs
Claudio Schaad eefbfff3eb Add "Wasser" page for the water bills (#11)
Co-authored-by: Claudio Schaad <c.schaad@pog.ch>
Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
2026-10-30 20:19:07 +00:00

56 lines
1.9 KiB
C#

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
{
/// <summary>
/// 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.
/// </summary>
public class WaterBillRepository : BaseRepository, IWaterBillRepository
{
private const string WATER = "water.json";
public WaterBillRepository(ISettingsService settingsService, RepositoryCache cache, ILogger<WaterBillRepository> logger)
: base(settingsService, cache, logger)
{
}
public Task<List<WaterBill>> GetWaterBillListAsync()
=> LoadListAsync<WaterBill>(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<WaterBill> 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);
}
}
}