예제 #1
0
        public async Task <ITravelExpenses> UpdateAsync(ITravelExpenses entity)
        {
            entity.CheckArgument(nameof(entity));
            entity.Travel.CheckArgument(nameof(entity.Travel));
            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.TravelId == entity.Travel.Id))
            {
                var tmpExpense = entity.Expenses.SingleOrDefault(i => i.Id == item.Id);

                if (tmpExpense == null)
                {
                    await expenseController.DeleteAsync(item.Id);
                }
            }

            var result = new TravelExpenses();
            var travel = await travelController.UpdateAsync(entity.Travel);

            result.TravelEntity.CopyProperties(travel);
            foreach (var item in entity.Expenses)
            {
                if (item.Id == 0)
                {
                    item.TravelId = entity.Travel.Id;
                    var insEntity = await expenseController.InsertAsync(item);

                    item.CopyProperties(insEntity);
                }
                else
                {
                    var updEntity = await expenseController.UpdateAsync(item);

                    item.CopyProperties(updEntity);
                }
            }
            return(result);
        }
예제 #2
0
        public async Task <ITravelExpenses> InsertAsync(ITravelExpenses entity)
        {
            entity.CheckArgument(nameof(entity));
            entity.Travel.CheckArgument(nameof(entity.Travel));
            entity.Expenses.CheckArgument(nameof(entity.Expenses));

            var result = new TravelExpenses();

            result.TravelEntity.CopyProperties(entity.Travel);
            await travelController.InsertAsync(result.TravelEntity);

            foreach (var item in entity.Expenses)
            {
                var expense = new Expense();

                expense.Travel = result.TravelEntity;
                expense.CopyProperties(item);
                await expenseController.InsertAsync(expense);

                result.ExpenseEntities.Add(expense);
            }
            return(result);
        }