public virtual async Task SyncBudgetGroup(RemoteBudget.RemoteBudget remoteBudget, BudgetGroup budgetGroup) { var budgetId = remoteBudget?.Id ?? throw new ArgumentNullException(nameof(remoteBudget)); RemoteBudget.RemoteBudgetGroup remoteGroup = remoteBudget?.BudgetGroups ?.Where(g => g.Label.ToLower() == budgetGroup.Name.ToLower()) .FirstOrDefault() ?? throw new Exception($"Budget group not found: {budgetGroup.Name}"); var taskList = new List <Task>(); budgetGroup.BudgetItems.ToList().ForEach(item => taskList.Add(AddOrUpdateBudgetItem(budgetId, item, remoteGroup))); remoteGroup.BudgetItems?.ToList().ForEach( remoteItem => taskList.Add(RemoveUnusedBudgetItems(budgetId, remoteItem, budgetGroup))); await Task.WhenAll(taskList); }
private async Task AddOrUpdateBudgetItem(string budgetId, BudgetItem item, RemoteBudget.RemoteBudgetGroup remoteGroup) { if (item.Amount == 0m) { return; } var remoteItem = remoteGroup.BudgetItems?.Where(i => i.Label == item.Name).FirstOrDefault(); if (remoteItem == null) { await remoteBudgetCalls.CreateBudgetItem(budgetId, remoteGroup.Id, item.Type, item.Name, item.Amount); return; } await remoteBudgetCalls.UpdateBudgetItem(budgetId, remoteItem.Id, item.Amount); }