using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Schaad.Accounting.Datasets; using Schaad.Accounting.Interfaces; using Schaad.Accounting.Models; namespace Schaad.Accounting.Repositories { public class BankTransactionRepository : BaseRepository, IBankTransactionRepository { private readonly string BANK_TRANSACTIONS = "BankTransactions.xml"; public BankTransactionRepository(ISettingsService settingsService, RepositoryCache cache, ILogger logger) : base(settingsService, cache, logger) { } public Task> GetBankTransactionListAsync() => LoadListAsync(BANK_TRANSACTIONS); /// /// Save new bank transactions, ignore existing ones (no update) /// public async Task SaveBankTransactionListAsync(string bankAccountNumber, List bankTransactions) { var count = 0; var transactions = await GetBankTransactionListAsync(); foreach (var bankTrx in bankTransactions) { var transaction = transactions.FirstOrDefault(t => t.Id == bankTrx.Id); if (transaction == null) { transaction = new BankTransaction() { Id = bankTrx.Id, BookingDate = bankTrx.BookingDate, ValueDate = bankTrx.ValueDate, Value = (decimal)bankTrx.Value, Text = bankTrx.Text, BankAccountNumber = bankAccountNumber, Debtor = bankTrx.Debtor, Creditor = bankTrx.Creditor, }; transactions.Add(transaction); count++; } } await SaveAsync(transactions, BANK_TRANSACTIONS); return count; } public async Task GetBankTransactionAsync(string id) { var transactions = await GetBankTransactionListAsync(); return transactions.FirstOrDefault(t => t.Id == id); } } }