Example #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> Edit(PremiumOfferViewModel model)
        {
            try
            {
                var current = await _premiumRepository.GetByIdAsync(model.Id);

                if (current == null)
                {
                    return(new NotFoundViewResult("_Error404"));
                }

                var partner = await _partnerRepository.GetByIdAsync(model.Id);

                var flight = await _flightRepository.GetByIdAsync(model.FlightId);

                current.Flight     = flight ?? null;
                current.Conditions = string.IsNullOrEmpty(model.Conditions) ? string.Empty : model.Conditions;
                current.Partner    = partner;
                current.Quantity   = model.Quantity;
                current.Price      = model.Price;
                current.Status     = 1;
                current.ModifiedBy = await GetCurrentUser();

                current.UpdateDate = DateTime.UtcNow;

                var result = await _premiumRepository.UpdateOfferAsync(current);

                if (!result.Success)
                {
                    return(new NotFoundViewResult("_Error404"));
                }

                //update notification to SuperUser
                result = await _notificationHelper.UpdateNotificationAsync(current.OfferIdGuid, UserType.SuperUser, "");

                if (!result.Success)
                {
                    //if notification does not exist, creates one
                    await _notificationHelper.CreateNotificationAsync(current.OfferIdGuid, UserType.SuperUser, "", EnumHelper.GetType(current.Type));
                }

                return(RedirectToAction(nameof(PremiumIndex)));
            }
            catch (Exception)
            {
                return(new NotFoundViewResult("_Error500"));
            }
        }
        public async Task <IActionResult> DeleteOffer(int id)
        {
            try
            {
                var offer = await _premiumRepository.GetByIdAsync(id);

                if (offer == null)
                {
                    throw new Exception();
                }

                var result = await _premiumRepository.DeleteAsync(offer);

                return(RedirectToAction("OffersIndex"));
            }
            catch (Exception)
            {
                return(new NotFoundViewResult("_404Error"));
            }
        }