internal async Task UpdateExpense(Expense myExpense, SplitExpenseVM viewModel) { if (viewModel.FriendIds != "") { try { var newPayers = viewModel.FriendIds.Split(','); var payerIds = new List <int>(); foreach (var payer in newPayers) { payerIds.Add(Convert.ToInt32(payer)); } var oldPayers = PayersForExpense.Where(p => p.ExpenseId == myExpense.Id).ToList(); //Radera de oldpayers som inte är med i payerIds foreach (var payer in oldPayers) { if (payerIds.Where(p => p == payer.UserId).Count() == 0) { PayersForExpense.Remove(payer); } } //Lägg till de payerIds som inte fanns i oldPayers. foreach (var payer in payerIds) { if (oldPayers.Where(p => p.UserId == payer).Count() == 0) { PayersForExpense.Add(new PayersForExpense { UserId = payer, ExpenseId = myExpense.Id }); } } SaveChanges(); } catch (Exception ex) { Console.WriteLine(ex); } } var myEvent = GetEventById(Convert.ToInt32(viewModel.SelectedEvent)); myExpense.Amount = Convert.ToDecimal(viewModel.Amount, CultureInfo.InvariantCulture); myExpense.Description = viewModel.Description; myExpense.CurrencyId = Convert.ToInt32(viewModel.SelectedCurrency); myExpense.Date = Convert.ToDateTime(viewModel.Date); myExpense.EventId = myEvent.Id; myExpense.PurchaserId = viewModel.PurchaserID; myExpense.AmountInStandardCurrency = await CalculateStandardCurrencyAmount(Convert.ToDecimal(viewModel.Amount, CultureInfo.InvariantCulture), viewModel.SelectedCurrency, myEvent.StandardCurrencyId, Convert.ToDateTime(viewModel.Date)); SaveChanges(); }
internal void DeleteExpense(Expense myExpense) { foreach (var payer in myExpense.PayersForExpense) { PayersForExpense.Remove(payer); } SaveChanges(); Expense.Remove(myExpense); SaveChanges(); }