示例#1
0
        /// <summary>
        /// Completes a purchase from a User(client)
        /// Updates the User's BonusMiles
        /// Creates a Transaction with details of the purchase
        /// If it's a ticket, creates a reservation
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task <IActionResult> PurchasePremium(int?id)
        {
            try
            {
                if (id == null)
                {
                    throw new Exception("No item found!");
                }

                var offer = await _premiumRepository.GetByIdAsync(id.Value);

                if (offer == null)
                {
                    throw new Exception("No item found!");
                }

                var user = await _userHelper.GetUserByUsernameAsync(User.Identity.Name);

                if (user == null)
                {
                    await _userHelper.LogoutAsync();

                    return(RedirectToAction(nameof(HomeController.IndexClient), nameof(HomeController)));
                }
                //create a transaction
                var trans = _transactionHelper.CreatePurchaseTransaction(user, offer);

                var response = await _transactionRepository.AddTransanctionAsync(trans);

                if (!response.Success)
                {
                    //send notification to admin to check transaction
                    //send an error to User saying that a problem ocurred with the purchase
                    throw new Exception(response.Message);
                }

                //criar a reserva
                //guardar na BD
                //enviar a reserva pelo email

                user.BonusMiles = trans.EndBalance;
                var result = await _userHelper.UpdateUserAsync(user);

                //update User fails but transaction and reservation were successfull
                if (!result.Succeeded)
                {
                    throw new Exception("You purchase was successfull. Your balance should reflect it in the next hours.");
                }

                return(View()); //return success message
            }
            catch (Exception exception)
            {
                ModelState.AddModelError(string.Empty, exception.Message);
            }
            return(View()); //return success message
        }
        public async Task <IActionResult> Purchase(TransactionViewModel model)
        {
            try
            {
                var user = await GetCurrentUser();

                if (user == null)
                {
                    return(RedirectToAction(nameof(AccountController.LoginClient), "Account"));
                }

                var operation = _transactionRepository.GetTransactionHistory(user);
                if (operation + model.Value > 20000)
                {
                    throw new Exception("Cannot complete this operation. The amount exceeds your maximum amount per year.");
                }

                var transaction = _clientConverterHelper.CreatePurchaseTransaction(model, user);
                transaction.StartBalance = user.BonusMiles;
                transaction.EndBalance   = transaction.StartBalance + transaction.Value;

                user.BonusMiles += transaction.Value;
                var result2 = await _userHelper.UpdateUserAsync(user);

                if (!result2.Succeeded)
                {
                    //send error
                }

                var result = await _transactionRepository.AddTransanctionAsync(transaction);

                if (!result.Success)
                {
                    return(Json("An error ocurred while submitting your request. Please try again."));
                }

                return(Json("You purchase was successfull. Your balance should reflect it in the next hours."));
            }
            catch (Exception e)
            {
                return(Json(e.Message));
            }
        }