Example #1
0
        public ActionResult Create(QuoteVM quote)
        //[Bind(Include = "Id,CustomerID,AutoModelID,DrivingHistoryID,CoverageID,AutoOptionID,PriceQuote")]
        {
            CustomerDrivingHistory cdhDB = null;

            if (Visitor.IsVisitor(quote.CustomerID)) //Save Visitor driving history
            {
                var list = db.CustomerDrivingHistories.Where(x => x.CustomerID == quote.CustomerID).OrderBy(x => x.DateRecorded).ToList();
                if (list.Count > 0)
                {
                    cdhDB = list.Last();
                    if (quote.NumDUIs != cdhDB.NumDUIs || quote.NumSpeedingTickets != cdhDB.NumSpeedingTickets) //the history changed
                    {
                        cdhDB = null;                                                                           //set to null so that we record the new history
                    }
                }
            }
            if (cdhDB is null)
            {
                cdhDB = new CustomerDrivingHistory
                {
                    CustomerID         = quote.CustomerID,
                    DateRecorded       = DateTime.Now,
                    NumDUIs            = quote.NumDUIs,
                    NumSpeedingTickets = quote.NumSpeedingTickets
                };
                db.CustomerDrivingHistories.Add(cdhDB);
                _ = db.SaveChanges();
            }

            if (Visitor.IsVisitor(quote.CustomerID))
            {
                //save Visitor's birthday and driving history ID in a separate table for reference
                var vs = new VisitorSession
                {
                    DateOfBirth      = quote.DateOfBirth,
                    DrivingHistoryId = cdhDB.Id
                };
                db.VisitorSessions.Add(vs);
                _ = db.SaveChanges();
            }

            Quote quoteDB = new Quote();

            quoteDB.CustomerID       = quote.CustomerID;
            quoteDB.AutoModelID      = quote.AutoModelID;
            quoteDB.AutoOptionID     = quote.AutoOptionID;
            quoteDB.CoverageID       = quote.CoverageID;
            quoteDB.DrivingHistoryID = cdhDB.Id;
            quoteDB.DateIssued       = DateTime.Now;
            quoteDB.PriceQuote       = BusinessLogic.QuoteLogic.GetPriceQuote(quoteDB);
            db.Quotes.Add(quoteDB);
            _ = db.SaveChanges();

            return(RedirectToAction("Details", quoteDB));
        }
        public ActionResult Details(int quoteID)
        {
            QuoteDetailVM qdVM;

            if (User.IsInRole("Admin"))
            {
                var quoteDB = db.Quotes.Find(quoteID);
                if (quoteDB is null)
                {
                    return(null);
                }
                var            cust      = db.Customers.Find(quoteDB.CustomerID);
                VisitorSession vs        = null;
                bool           isVisitor = false;
                if (Visitor.IsVisitor(cust.Id))
                {
                    isVisitor = true;
                    vs        = db.VisitorSessions.Where(x => x.DrivingHistoryId == quoteDB.DrivingHistoryID).ToList().First(); //drivingHistoryId is a key
                }
                var dHist  = db.CustomerDrivingHistories.Find(quoteDB.DrivingHistoryID);
                var model  = db.AutoModels.Find(quoteDB.AutoModelID);
                var option = quoteDB.AutoOptionID == null ? null : db.AutoOptions.Find(quoteDB.AutoOptionID);
                qdVM = new QuoteDetailVM
                {
                    QuoteID       = quoteDB.Id,
                    Coverage      = db.CoverageLevels.Find(quoteDB.CoverageID).Name,
                    CustomerID    = (int)quoteDB.CustomerID,
                    FirstName     = cust.FirstName,
                    LastName      = cust.LastName,
                    DateOfBirth   = (DateTime)(isVisitor ? vs.DateOfBirth : cust.DateOfBirth),
                    NumDUIs       = (int)dHist.NumDUIs,
                    NumTickets    = (int)dHist.NumSpeedingTickets,
                    DateIssued    = (DateTime)quoteDB.DateIssued,
                    Make          = db.AutoMakes.Find(model.AutoMakeID).MakeName,
                    Model         = model.ModelName,
                    ModelYear     = ((int)model.ModelYear).ToString(),
                    OptionPackage = option is null ? "" : option.OptionDescription,
                    Price         = ((decimal)quoteDB.PriceQuote).ToString("C2")
                };
            }
            else
            {
                //we don't want to show non-Admins a quote for another customer
                qdVM = null;
            }
            return(View(qdVM));
        }
Example #3
0
        public async Task <bool> AddSessionInTheDb(string ipAddress, string visitorId)
        {
            if (string.IsNullOrEmpty(ipAddress) || string.IsNullOrEmpty(visitorId))
            {
                return(false);
            }

            if (visitorId != null && !this.context.VisitorsSessions.Any(x => x.VisitorId == visitorId))
            {
                var visitorSession = new VisitorSession
                {
                    IpAddress = ipAddress,
                    VisitorId = visitorId,
                };

                this.context.VisitorsSessions.Add(visitorSession);
                await this.context.SaveChangesAsync();

                return(true);
            }

            return(false);
        }