Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
56 lines
2 KiB
C#
56 lines
2 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>
|
||
/// Electricity bills of the current mandator, stored once per mandator (not per year like
|
||
/// the other data files) because the billing period runs 1 July – 30 June.
|
||
/// </summary>
|
||
public class ElectricityBillRepository : BaseRepository, IElectricityBillRepository
|
||
{
|
||
private const string ELECTRICITY = "electricity.json";
|
||
|
||
public ElectricityBillRepository(ISettingsService settingsService, RepositoryCache cache, ILogger<ElectricityBillRepository> logger)
|
||
: base(settingsService, cache, logger)
|
||
{
|
||
}
|
||
|
||
public Task<List<ElectricityBill>> GetElectricityBillListAsync()
|
||
=> LoadListAsync<ElectricityBill>(settingsService.GetMandatorPath(), ELECTRICITY);
|
||
|
||
public async Task SaveElectricityBillAsync(ElectricityBill bill)
|
||
{
|
||
var bills = await GetElectricityBillListAsync();
|
||
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(), ELECTRICITY);
|
||
}
|
||
|
||
public async Task<ElectricityBill> GetElectricityBillAsync(string id)
|
||
{
|
||
var bills = await GetElectricityBillListAsync();
|
||
return bills.FirstOrDefault(b => b.Id == id);
|
||
}
|
||
|
||
public async Task DeleteElectricityBillAsync(string id)
|
||
{
|
||
var bills = await GetElectricityBillListAsync();
|
||
bills.RemoveAll(b => b.Id == id);
|
||
await SaveAsync(bills, settingsService.GetMandatorPath(), ELECTRICITY);
|
||
}
|
||
}
|
||
}
|