コード例 #1
0
        public async Task <int> AddExpenseAsync(Expense expense)
        {
            if (!string.IsNullOrEmpty(expense.ExpenseCategoryLabel))
            {
                var newExpenseCategory = await _expenseCategoryRepository.GetExpenseCategoryForUserAsync(expense.ExpenseCategoryLabel, expense.UserId);

                if (newExpenseCategory != null)
                {
                    expense.ExpenseCategoryId = newExpenseCategory.ExpenseCategoryId;
                }
            }

            var newExpenseEntry = new ExpenseEntry
            {
                UserId            = expense.UserId,
                DateAdded         = DateTime.Now,
                Description       = expense.Description,
                Title             = expense.Title,
                ExpenseCategoryId = expense.ExpenseCategoryId,
                Value             = expense.Value
            };

            _dbContext.ExpenseEntries.Add(newExpenseEntry);

            await _dbContext.SaveChangesAsync();

            return(newExpenseEntry.ExpenseEntryId);
        }