示例#1
0
        public IActionResult ExtendMiles()
        {
            // Generates a new instance of ProlongMilesViewModel
            var model = new ExtendMilesViewModel
            {
                Amount = 2000
            };

            return(View(model));
        }
 public Mile ToMile(ExtendMilesViewModel model, int clientId, DateTime newExpirationDate)
 {
     return(new Mile
     {
         ClientId = clientId,
         Qtd = model.Amount,
         MilesTypeId = 2,
         ExpirationDate = newExpirationDate
     });
 }
 public Transaction ToTransaction(ExtendMilesViewModel model, Mile mile)
 {
     return(new Transaction
     {
         Description = "Extended",
         Value = mile.Qtd,
         TransactionDate = DateTime.Now,
         Price = 70,
         ClientID = mile.ClientId,
         IsAproved = true,
         IsCreditCard = true
     });
 }
示例#4
0
        public async Task <IActionResult> ExtendMiles(ExtendMilesViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Gets the current Client
                var client = await _clientRepository.GetByEmailAsync(this.User.Identity.Name);

                //Checks if the maximum value of transfered miles has been reached
                var prolongMiles = model.Amount + client.ExtendedMiles;

                if (prolongMiles > 20000)
                {
                    this.ModelState.AddModelError(string.Empty, "You can only extend 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 Mile Object for the giftedClient
                var newDate = clientMiles.OrderBy(m => m.ExpirationDate).FirstOrDefault().ExpirationDate.AddYears(1);
                var mile    = _converterHelper.ToMile(model, client.Id, newDate);

                var extendedMiles = _converterHelper.ToTransaction(model, mile);


                // Creates a transaction on the Database
                try
                {
                    await _transactionRepository.CreateAsync(extendedMiles);
                }
                catch (Exception)
                {
                    this.ModelState.AddModelError(string.Empty, "There was an error processing your extension.Please try again later");
                    return(View(model));
                }


                // Extends the Miles
                var extendSuccess = await _mileRepository.SpendMilesAsync(clientMiles, model.Amount);

                if (!extendSuccess)
                {
                    this.ModelState.AddModelError(string.Empty, "There was a critical error with the extension algorithm. Please contact the Administrator as soon as possible");
                    return(View(model));
                }

                // 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 purchase.Please try again later");
                    return(View(model));
                }

                // Updates the client Prolonged Miles on the DataBase
                try
                {
                    client.ExtendedMiles = prolongMiles;
                    await _clientRepository.UpdateAsync(client);
                }
                catch (Exception)
                {
                    this.ModelState.AddModelError(string.Empty, "There was an error processing your purchase.Please try again later");
                    return(View(model));
                }

                ViewBag.Message = "Your extension of miles was successful!";
                return(View(model));
            }

            // Generates a new instance of ExtendMilesViewModel
            var returnModel = new ExtendMilesViewModel
            {
                Amount = 2000
            };

            return(View(model));
        }