Пример #1
0
        public ActionResult ResultDetail(string modelCode, string userId)
        {
            if (String.IsNullOrEmpty(modelCode))
            {
                return(PartialView("_ResultError"));
            }

            var dbCar = _dbContext.GetCar(modelCode).FirstOrDefault();

            if (dbCar == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var dbFinance = _dbContext.GetCarFinance(dbCar.Id).FirstOrDefault();

            if (dbFinance == null)
            {
                return(RedirectToAction("Index", "Home"));
            }

            var viewModel = new CarViewModel
            {
                Brand       = Common.Config.Brand,
                Code        = dbCar.Code,
                Model       = dbCar.Model,
                Color       = dbCar.Color,
                Engine      = dbCar.Engine,
                Engine_Name = dbCar.EngineName,
                Url         = dbCar.Url,
                Name        = dbCar.Name,
                Capacity    = dbCar.Capacity,
                Luggage     = dbCar.Luggage,
                Lifestyle   = dbCar.Lifestyle,
                Awd         = dbCar.Awd,
                High        = dbCar.High,
                Convertible = dbCar.Convertible,
                Price       = dbCar.Price,
                Cost        = dbCar.Cost,
                Speed       = dbCar.Speed,
                Mph         = dbCar.Mph,
                Economy     = dbCar.Economy,
                Alt_1       = dbCar.Alt1,
                Alt_2       = dbCar.Alt2,
                Alt_3       = dbCar.Alt3,
                Terms       = dbCar.Terms,
                Finance     = new Models.Finance
                {
                    Info          = dbFinance.Info,
                    Term          = dbFinance.Term ?? 0,
                    Payment       = dbFinance.Payment ?? 0.0,
                    Price         = dbFinance.FinancePrice ?? 0.0,
                    Deposit       = dbFinance.Deposit ?? 0.0,
                    Contribution  = dbFinance.Contribution ?? 0.0,
                    Purchase_Fee  = dbFinance.PurchaseFee ?? 0.0,
                    Final_Payment = dbFinance.FinalPayment ?? 0.0,
                    Credit_Charge = dbFinance.CreditCharge ?? 0.0,
                    ROI           = dbFinance.ROI,
                    APR           = dbFinance.APR
                }
            };

            ViewBag.UserId = userId;

            return(PartialView("_ResultDetail", viewModel));
        }
        /// <summary>
        /// Sends form data to eMaster and GRG APIs and despatches emails.
        /// </summary>
        /// <returns>True/False</returns>
        /// <exception cref="CarNotFoundException">
        /// Car not found.
        /// or
        /// Finance option not found.
        /// </exception>
        public bool Execute()
        {
            var isComplete = false;

            var dbCar = _dbContext.GetCar(_carModel).FirstOrDefault();

            if (dbCar == null)
            {
                throw new CarNotFoundException("Car not found.");
            }

            var dbFinance = _dbContext.GetCarFinance(dbCar.Id).FirstOrDefault();

            if (dbFinance == null)
            {
                throw new CarNotFoundException("Finance option not found.");
            }

            var car = new Models.NewCar()
            {
                Code         = dbCar.Code,
                Color        = dbCar.Color,
                Engine       = dbCar.Engine,
                EngineName   = dbCar.EngineName,
                Model        = dbCar.Model,
                Name         = dbCar.Name,
                Capacity     = dbCar.Capacity,
                Luggage      = dbCar.Luggage,
                Lifestyle    = dbCar.Lifestyle,
                Awd          = dbCar.Awd,
                High         = dbCar.High,
                Convertible  = dbCar.Convertible,
                Price        = dbCar.Price,
                Cost         = dbCar.Cost,
                Speed        = dbCar.Speed,
                Mph          = dbCar.Mph,
                Economy      = dbCar.Economy,
                Terms        = dbCar.Terms,
                Alt_1        = dbCar.Alt1,
                Alt_2        = dbCar.Alt2,
                Alt_3        = dbCar.Alt3,
                APR          = dbFinance.APR,
                Contribution = dbFinance.Contribution,
                CreditCharge = dbFinance.CreditCharge,
                Deposit      = dbFinance.Deposit,
                FinalPayment = dbFinance.FinalPayment,
                PurchaseFee  = dbFinance.PurchaseFee,
                ROI          = dbFinance.ROI,
                Term         = dbFinance.Term,
                FinancePrice = dbFinance.FinancePrice,
                Payment      = dbFinance.Payment,
                Mpg          = Convert.ToDouble(dbCar.Mpg),
                Url          = dbCar.Url,
                Joke         = dbCar.Joke
            };

            _customer.Car = car;
            var requestUrl = string.Empty;

            // Send to eMaster API
            var emResponse = false;

            if (_customer.UserId != "0")
            {
                IProvider eMasterProvider = new EMasterProvider();
                var       emfunc          = new FuncHelper.ExternalApiFunction <Customer, string, bool>(eMasterProvider.SendData);
                emResponse = FuncHelper.DoFuncWithRetry(emfunc, _customer, out requestUrl, TimeSpan.FromSeconds(2));
            }

            // Send to GRG API
            IProvider grassRootsProvider = new GrassRootsProvider();
            var       grgfunc            = new FuncHelper.ExternalApiFunction <Customer, string, bool>(grassRootsProvider.SendData);
            var       grgResponse        = FuncHelper.DoFuncWithRetry(grgfunc, _customer, out requestUrl, TimeSpan.FromSeconds(2));

            Log.Info("Response: " + grgResponse);

            if (!grgResponse)
            {
                var errorEmailSubject     = Config.ErrorEmailSubject;
                var errorEmailDestination = Config.ErrorEmailDestination;
                var errorEmailText        = requestUrl;
                MandrillHelper.SendEmail(errorEmailDestination, errorEmailSubject, null, "An error occured while processing the following URL:\r\n\r\n" + errorEmailText);
            }

            string readHTMLFile;
            string readTextFile;
            var    subject = Config.EmailMeResultsSubject;

            using (var reader = new StreamReader(_htmlTemplatePath))
            {
                readHTMLFile = reader.ReadToEnd();
            }
            using (var reader = new StreamReader(_textTemplatePath))
            {
                readTextFile = reader.ReadToEnd();
            }
            var htmlBody     = readHTMLFile;
            var textBody     = readTextFile;
            var assetPath    = Config.EmailAssetsDomain + Config.EmailAssetsLocation;
            var carAssetPath = Config.EmailAssetsDomain + Config.EmailCarAssetsLocation;
            var carPath      = carAssetPath + "/" + _customer.Car.Code + ".jpg";

            var colour =
                Enum.GetValues(typeof(Colour))
                .Cast <Colour>()
                .FirstOrDefault(v => v.GetDescription() == _customer.Car.Color);
            var hexColour = colour.DisplayName();

            htmlBody = htmlBody.Replace("[[Title]]", _customer.Title)
                       .Replace("[[Firstname]]", _customer.FirstName.ToUpper())
                       .Replace("[[Lastname]]", _customer.LastName.ToUpper())
                       .Replace("[[CarName]]", (Config.Brand + " " + car.EngineName + " " + car.Model).ToUpper())
                       .Replace("[[Location]]", assetPath)
                       .Replace("[[CarImage]]", carPath)
                       .Replace("[[Colour]]", hexColour);

            textBody = textBody.Replace("[[Title]]", _customer.Title)
                       .Replace("[[Firstname]]", _customer.FirstName.ToUpper())
                       .Replace("[[Lastname]]", _customer.LastName.ToUpper())
                       .Replace("[[CarName]]", (Config.Brand + " " + car.EngineName + " " + car.Model).ToUpper())
                       .Replace("[[Location]]", assetPath)
                       .Replace("[[CarImage]]", carPath)
                       .Replace("[[Colour]]", hexColour);

            MandrillHelper.SendEmail(_customer.Email, subject, htmlBody, textBody);

            isComplete = true;

            return(isComplete);
        }
Пример #3
0
        public HttpResponseMessage GetCars()
        {
            var serializer = new JavaScriptSerializer();
            var response   = new HttpResponseMessage();

            try
            {
                var dbCar     = _dbContext.GetCars().ToList();
                var viewModel = (from car in dbCar
                                 let finance = _dbContext.GetCarFinance(car.Id).First()
                                               select new CarViewModel
                {
                    Brand = car.Brand,
                    Code = car.Code,
                    Color = car.Color,
                    Engine = car.Engine,
                    Name = car.Name,
                    Engine_Name = car.EngineName,
                    Model = car.Model,
                    Capacity = car.Capacity,
                    Luggage = car.Luggage,
                    Lifestyle = car.Lifestyle,
                    Awd = car.Awd,
                    High = car.High,
                    Convertible = car.Convertible,
                    Price = car.Price,
                    Cost = car.Cost,
                    Speed = car.Speed,
                    Mph = car.Mph,
                    Economy = car.Economy,
                    Alt_1 = car.Alt1,
                    Alt_2 = car.Alt2,
                    Alt_3 = car.Alt3,
                    Terms = car.Terms,
                    Joke = car.Joke,
                    Url = car.Url,
                    Finance = new Models.Finance
                    {
                        Info = finance.Info,
                        Term = finance.Term ?? 0,
                        Payment = finance.Payment ?? 0.0,
                        Price = finance.FinancePrice ?? 0.0,
                        Deposit = finance.Deposit ?? 0.0,
                        Contribution = finance.Contribution ?? 0.0,
                        Purchase_Fee = finance.PurchaseFee ?? 0.0,
                        Final_Payment = finance.FinalPayment ?? 0.0,
                        Credit_Charge = finance.CreditCharge ?? 0.0,
                        ROI = finance.ROI,
                        APR = finance.APR,
                        Total_Amount = finance.TotalAmount ?? 0.0,
                        Total_Deposit = finance.TotalDeposit ?? 0.0,
                        Dealer_Discount = finance.DealerDiscount ?? 0.0
                    }
                }).ToList();

                //Add Rocket Car
                viewModel.Add(new CarViewModel
                {
                    Brand       = Common.Config.Brand,
                    Engine      = "",
                    Engine_Name = "",
                    Model       = "",
                    Capacity    = "",
                    Luggage     = "",
                    Lifestyle   = "",
                    Alt_1       = "SX92",
                    Alt_2       = "MR92",
                    Alt_3       = "SS92",
                    Code        = "RKT",
                    Color       = "Blazing Red",
                    Name        = "ALPHA CENTURA",
                    Terms       = "",
                    Joke        = "",
                    Url         = ""
                });

                var settings = new JsonSerializerSettings {
                    ContractResolver = new LowercaseContractResolver()
                };
                var json = JsonConvert.SerializeObject(viewModel, Formatting.Indented, settings);

                var sc = new StringContent(json);
                sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");

                response.Content = sc;

                var response2 = new HttpResponseMessage()
                {
                    Content = new StringContent(
                        json,
                        Encoding.UTF8,
                        "application/json"
                        )
                };

                var cacheEnabled  = bool.Parse(Common.Config.CacheEnabled);
                var expiryTimeOut = Int32.Parse(Common.Config.CacheExpiration);

                if (!cacheEnabled)
                {
                    return(response2);
                }
                response.Headers.CacheControl = new CacheControlHeaderValue
                {
                    MaxAge = new TimeSpan(0, expiryTimeOut, 0),
                    Public = true
                };

                return(response);
            }
            catch (Exception ex)
            {
                Log.Error("GetCars", ex);
                var error = new ErrorResponse
                {
                    Error = "Failed to get cars"
                };
                var sc = new StringContent(serializer.Serialize(error));
                sc.Headers.ContentType = new MediaTypeHeaderValue("application/json");
                response.Content       = sc;
                return(response);
            }
        }