public async Task <IBillExpense> GetByIdAsync(int id) { var result = default(BillExpense); var bill = await billController.GetByIdAsync(id); if (bill != null) { result = new BillExpense(); result.Bill.CopyProperties(bill); foreach (var item in await expenseController.QueryAsync(e => e.BillId == id)) { result.ExpenseEntities.Add(item); } } else { throw new Exception("Entity can't find!"); } return(result); }
public async Task <IBillExpense> UpdateAsync(IBillExpense 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 BillExpense(); var bill = await billController.UpdateAsync(entity.Bill); result.BillEntity.CopyProperties(bill); 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 <IBillExpense> InsertAsync(IBillExpense entity) { entity.CheckArgument(nameof(entity)); entity.Bill.CheckArgument(nameof(entity.Bill)); entity.Expenses.CheckArgument(nameof(entity.Expenses)); var result = new BillExpense(); result.BillEntity.CopyProperties(entity.Bill); await billController.InsertAsync(result.BillEntity); foreach (var item in entity.Expenses) { var expense = new Expense(); expense.CopyProperties(item); expense.Bill = result.BillEntity; await expenseController.InsertAsync(expense); result.ExpenseEntities.Add(expense); } return(result); }