Esempio n. 1
0
        public IActionResult GetExpenseById(Guid id)
        {
            if (!_expenseRepository.ExpenseExists(id))
            {
                return(NotFound());
            }

            var expenseFromStore = _expenseRepository.GetExpenseById(id);

            var mappedExpense = Mapper.Map <ExpenseDto>(expenseFromStore);

            return(Ok(mappedExpense));
        }
        public async Task <Expense> SaveExpense(ExpenseViewModel expenseVm)
        {
            try
            {
                if (expenseVm == null)
                {
                    throw new ArgumentException($"expense cannot be null");
                }
                var expenseRecordExist = await expenseRepository.ExpenseExists(
                    expenseVm.Title, expenseVm.ExpenseDate.HasValue?
                    expenseVm.ExpenseDate.Value : DateTime.Now);

                if (expenseRecordExist)
                {
                    return(null);
                }
                var expense = BuildExpenseRecordFromModel(expenseVm);
                expense.Amount = !expenseVm.Amount.Trim().EndsWith("EUR", StringComparison.InvariantCultureIgnoreCase) ?
                                 double.Parse(expenseVm.Amount) : await SetAmount(expenseVm.Amount);

                return(await expenseRepository.SaveExpense(expense));
            }
            catch (Exception)
            {
                throw;
            }
        }