public async Task <IBillExpenses> UpdateAsync(IBillExpenses entity) { entity.CheckArgument(nameof(entity)); entity.Bill.CheckArgument(nameof(entity.Bill)); entity.Expenses.CheckArgument(nameof(entity.Expenses)); //Delete all costs that are no longer included in the list. foreach (var item in await expenseController.QueryAsync(e => e.BillId == entity.Bill.Id)) { var tmpExpense = entity.Expenses.SingleOrDefault(i => i.Id == item.Id); if (tmpExpense == null) { await expenseController.DeleteAsync(item.Id); } } var result = new BillExpenses(); var travel = await billController.UpdateAsync(entity.Bill); result.BillEntity.CopyProperties(travel); foreach (var item in entity.Expenses) { if (item.Id == 0) { item.BillId = entity.Bill.Id; var insEntity = await expenseController.InsertAsync(item); item.CopyProperties(insEntity); } else { var updEntity = await expenseController.UpdateAsync(item); item.CopyProperties(updEntity); } } return(result); }
public async Task <IBillExpenses> InsertAsync(IBillExpenses entity) { entity.CheckArgument(nameof(entity)); entity.Bill.CheckArgument(nameof(entity.Bill)); entity.Expenses.CheckArgument(nameof(entity.Expenses)); var result = new BillExpenses(); result.BillEntity.CopyProperties(entity.Bill); await billController.InsertAsync(result.BillEntity); foreach (var item in entity.Expenses) { var expense = new Expense(); expense.Bill = result.BillEntity; expense.CopyProperties(item); await expenseController.InsertAsync(expense); result.ExpenseEntities.Add(expense); } return(result); }