Example #1
0
        public async Task <IActionResult> CreateExtraExpense()
        {
            var list = await _categoryRepository.FindAll();

            CreateAnotherExpenseFormViewModel viewModel = new CreateAnotherExpenseFormViewModel()
            {
                Categorias = list,
                TipoId     = 3
            };

            return(View(viewModel));
        }
Example #2
0
        public async Task <IActionResult> CreateAnotherExpense(CreateAnotherExpenseFormViewModel viewModel)
        {
            if (!ModelState.IsValid || viewModel.Valor == 0)
            {
                return(View(viewModel));
            }
            try
            {
                var user = await _userManager.GetUserAsync(User);

                var category = await _categoryRepository.FindByIdAsync(viewModel.CategoriaId);

                var type = await _typeRepository.FindTypeById(viewModel.TipoId);

                Despesa expense = new Despesa()
                {
                    Categoria   = category,
                    CategoriaId = category.Id,
                    Data        = DateTime.UtcNow,
                    Descricao   = viewModel.Descricao,
                    Tipo        = type,
                    TipoId      = type.Id,
                    User        = user,
                    UserId      = user.Id,
                    Valor       = viewModel.Valor
                };

                await _anothersExpenseRepository.RegisterNewExpenseAsync(expense);

                if (expense.TipoId == 2)
                {
                    return(RedirectToAction(nameof(VariableExpenseIndex), new { id = user.Id }));
                }
                else
                {
                    return(RedirectToAction(nameof(ExtraExpenseIndex), new { id = user.Id }));
                }
            }
            catch (Exception e)
            {
                return(RedirectToAction(nameof(Error), new { message = "Erro ao cadastrar" }));
            }
        }