コード例 #1
0
        public async Task <IActionResult> Matriculate(MatriculateViewModel matriculateViewModel)
        {
            if (ModelState.IsValid)
            {
                if (await _matriculationRepository.AlreadyMatriculate(matriculateViewModel.UserId, matriculateViewModel.CourseId))
                {
                    _logger.LogInformation("Usuário já possui matrícula nesse curso");
                    TempData["ErrorMessage"] = "Você já está matriculado neste curso";
                    return(View(matriculateViewModel));
                }
                else if (matriculateViewModel.Price > matriculateViewModel.UserWalletBalance)
                {
                    _logger.LogInformation("Saldo insuficiente");
                    TempData["ErrorMessage"] = "Saldo insuficiente";
                    return(View(matriculateViewModel));
                }
                else
                {
                    DateTime CreationDate = DateTime.Now;
                    //string generatedIdentifier = GenerateIdentifier(10);

                    Matriculation matriculation = new Matriculation
                    {
                        UserId             = matriculateViewModel.UserId,
                        CourseId           = matriculateViewModel.CourseId,
                        TotalValue         = matriculateViewModel.Price,
                        CreationDate       = CreationDate,
                        ExternalIdentifier = null
                    };

                    //_logger.LogInformation("Enviando email com detalhes da matrícula");
                    //string subject = "Matrícula concluída com sucesso";

                    // string message = string.Format("Seu veículo já o aguarda. Você poderá pegá-lo dia {0}" +
                    //   " e deverá devolvê-lo dia {1}. O preço será R${2},00. Divirtá-se !!! ", aluguel.Inicio, aluguel.Fim, aluguel.PriceTotal);

                    //  await _email.EnviarEmail(usuario.Email, assunto, mensagem);

                    await _matriculationRepository.Insert(matriculation);

                    _logger.LogInformation("Matrícula feita");

                    _logger.LogInformation("Atualizando saldo do usuario");
                    var userWallet = await _walletRepository.GetWalletByUserId(matriculateViewModel.UserId);

                    userWallet.Balance -= matriculation.TotalValue;
                    await _walletRepository.Update(userWallet);

                    _logger.LogInformation("Saldo atualizado");

                    return(RedirectToAction("Index", "Users"));
                }
            }
            _logger.LogInformation("Informações inválidas");
            return(View());
        }
コード例 #2
0
        public async Task <IActionResult> Matriculate(string courseId)
        {
            _logger.LogInformation("Nova atualização");
            User user = await _userRepository.GetLoggedUser(User);

            Course course = await _courseRepository.GetById(courseId);

            var matriculateViewModel = new MatriculateViewModel
            {
                UserId            = user.Id,
                UserWalletBalance = _walletRepository.GetBalanceByUserId(user.Id),
                CourseId          = courseId,
                CourseName        = course.Title,
                Price             = course.Price
            };

            return(View(matriculateViewModel));
        }