public IActionResult TransferMiles() { // Generates a new instance of TransferMilesViewModel var model = new TransferMilesViewModel { Amount = 2000, GiftedClientID = string.Empty }; return(View(model)); }
// -------------------------------------------TEMPORARY------------------------------------------- //public async Task<IActionResult> PurchaseMiles2() //{ // var client = await _clientRepository.GetClientByEmailAsync(User.Identity.Name); // var creditCards = _creditCardRepository.GetCreditCardsAssociatedWithClient(client); // var model = new PurchaseMilesViewModel // { // CreditCards = _combosHelper.GetCreditCards(creditCards) // }; // ViewBag.NewCardHeader = new TabHeader { Text = "Register a new credit card" }; // ViewBag.ExistingCardsHeader = new TabHeader { Text = "Use an existing credit card" }; // return View(model); //} //[HttpPost] //public async Task<IActionResult> PurchaseMiles2(PurchaseMilesViewModel model) //{ // if (model.Quantity > 2000) // { // ModelState.AddModelError(string.Empty, "Can only purchase up to 2000 miles per transaction"); // } // var client = await _clientRepository.GetClientByEmailAsync(User.Identity.Name); // if (client == null) // { // return NotFound(); // } // if (model.CreditCardInfoId == 0) // { // model.CreditCardInfo = await _creditCardRepository.CheckExistingCreditCardByNumberAsync(model.CreditCardInfo); // } // else // { // var creditCard = await _creditCardRepository.GetByIdAsync(model.CreditCardInfoId); // } // await _milesTransactionRepository.PurchaseMilesAsync(model.Quantity, client, creditCard); // return View(); //} // -------------------------------------------TEMPORARY------------------------------------------- public IActionResult TransferMiles() { var model = new TransferMilesViewModel { Quantity = 2000 }; model.ValueToPay = Math.Round(model.Quantity * 0.035m, 0); return(View(model)); }
public async Task <IActionResult> TransferMiles(TransferMilesViewModel model) { if (ModelState.IsValid) { if (model.Quantity != 2000) { ModelState.AddModelError(string.Empty, "Can only transfer 2000 miles per transaction"); } var client = await _clientRepository.GetClientByEmailAsync(User.Identity.Name); if (client == null) { return(NotFound()); } var receivingClient = await _clientRepository.GetClientByNumberAsync(model.ClientToTransferToNumber); if (receivingClient == null) { ModelState.AddModelError(model.ClientToTransferToNumber, "Client number does not exist"); } model.CreditCardInfo.Client = client; await _creditCardRepository.CheckExistingCreditCardByNumberAsync(model.CreditCardInfo); ViewData["Message"] = await _milesTransactionRepository.TransferMilesAsync(model.Quantity, receivingClient, client); ResetModel(model); return(View(model)); } return(View(model)); }
public async Task <IActionResult> TransferMiles(TransferMilesViewModel model) { if (ModelState.IsValid) { // Gets the current Client var client = await _clientRepository.GetByEmailAsync(this.User.Identity.Name); // Gets the client to gift Miles var giftedClient = await _clientRepository.GetByIdAsync(Convert.ToInt32(model.GiftedClientID)); // Checks if the giftedClient exists if (giftedClient == null) { this.ModelState.AddModelError(string.Empty, "The selected Client does not exist. Please try again!"); return(View(model)); } //Checks if the maximum value of transfered miles has been reached var transferMiles = model.Amount + client.TransferedMiles; if (transferMiles > 20000) { this.ModelState.AddModelError(string.Empty, "You can only transfer a maximum of 20.000 Miles per Year"); return(View(model)); } // Gets a list of the client Bonus Miles var clientMiles = _mileRepository.GetAllBonusMiles(client.Id); // Creates a new var to hold the client Total Bonus Miles var clientTotalMiles = _mileRepository.GetClientTotalBonusMiles(client.Id); // Checks if the client has enough Miles to perform the Transfer Operation if (clientTotalMiles < model.Amount) { this.ModelState.AddModelError(string.Empty, "You dont have the necessary Miles to perform this transfer. Please try again with a lower value!"); return(View(model)); } // Creates a new Mile Object for the giftedClient var mile = _converterHelper.ToMile(giftedClient.Id, model.Amount, 1); // Creates the Transaction on the giftedClient end var receivedMiles = _converterHelper.ToTransaction(giftedClient.Id, mile.Qtd); // Creates the Transaction on the Client that will transfer miles end // IMPORTANT NOTE: // SUBTRACT THE MILES QUANTITY var giftedMiles = _converterHelper.ToTransaction(giftedClient.Id, -mile.Qtd); // Creates a new Mile on the DataBase try { await _mileRepository.CreateAsync(mile); } catch (Exception) { this.ModelState.AddModelError(string.Empty, "There was an error processing your transfer.Please try again later"); return(View(model)); } // Creates two transaction on the Database // One for the gifter and another for the receiver try { await _transactionRepository.CreateAsync(receivedMiles); await _transactionRepository.CreateAsync(giftedMiles); } catch (Exception) { this.ModelState.AddModelError(string.Empty, "There was an error processing your transfer.Please try again later"); return(View(model)); } // Transfers the Miles var transferSuccess = await _mileRepository.SpendMilesAsync(clientMiles, transferMiles); if (!transferSuccess) { this.ModelState.AddModelError(string.Empty, "There was a critical error with the transfer algorithm. Please contact the Administrator as soon as possible"); return(View(model)); } // Updates the client Transfer Miles on the DataBase try { client.TransferedMiles = transferMiles; await _clientRepository.UpdateAsync(client); } catch (Exception) { this.ModelState.AddModelError(string.Empty, "There was an error processing your transfer.Please try again later"); return(View(model)); } ViewBag.Message = "Your transfer was successful!"; return(View()); } this.ModelState.AddModelError(string.Empty, "Please select a valid Client Id"); return(View(model)); }