public IActionResult Cadastro(EmprestimoViewModel emprestimoViewModel) { if (ModelState.IsValid) { Livros livro = _dataService.GetLivro(emprestimoViewModel.LivroId); if (emprestimoViewModel.Devolvido) { this._dataService.AumentarQuantidadeLivro(emprestimoViewModel.LivroId); } else { if (livro.Quantidade < 1) { ViewBag.Error = "This book cannot be borrowred because it's out of stock."; EmprestimoViewModel viewModel = GetEmprestimoViewModelAdicao(); return(View(viewModel)); } else { this._dataService.DiminuirQuantidadeLivro(emprestimoViewModel.LivroId); } } if (emprestimoViewModel.Id > 0) { Usuarios usuario = _dataService.GetUsuario(emprestimoViewModel.UsuarioId); Emprestimos emprestimo = new Emprestimos(emprestimoViewModel.Id, livro, usuario, emprestimoViewModel.DataEmprestimo, emprestimoViewModel.DataDevolucao, emprestimoViewModel.Devolvido); _dataService.UpdateEmprestimo(emprestimo); } else { Usuarios usuario = _dataService.GetUsuario(emprestimoViewModel.UsuarioId); Emprestimos emprestimo = new Emprestimos(emprestimoViewModel.Id, livro, usuario, emprestimoViewModel.DataEmprestimo, emprestimoViewModel.DataDevolucao, emprestimoViewModel.Devolvido); _dataService.AddEmprestimo(emprestimo); } return(RedirectToAction("Lista")); } else { return(View()); } }