コード例 #1
0
        public static void Initialize()
        {
            using (var context = new CarConfiguratorEntityContext())
            {
                //ClearDB(context);

                // let's assume for now that if there's no data in db.carModel, then there's no data yet at all
                if (!context.CarModels.Any())
                {
                    context.CarModels.AddRange(CarModelSeedData.Data);
                    context.Engines.AddRange(EngineSeedData.Data);
                    context.EngineSettings.AddRange(EngineSettingsSeedData.Data);

                    context.Rims.AddRange(RimSeedData.Data);
                    context.Paints.AddRange(PaintSeedData.Data);
                    context.Accessories.AddRange(AccessorySeedData.Data);
                    context.Categories.AddRange(CategorySeedData.GetData());
                }

                if (context.ChangeTracker.HasChanges())
                {
                    Log.Info("adding database seed data");
                    context.SaveChanges();
                    Log.Info("db data was added");
                }
            }
        }
コード例 #2
0
        public void InitCarModel(int id)
        {
            using (var context = new CarConfiguratorEntityContext())
            {
                var selectedCarModel = context.CarModels.FirstOrDefault(cur => cur.Id == id);
                if (selectedCarModel == null)
                {
                    throw new ArgumentException($"Car model with id {id} was not found in database");
                }

                SessionData.ActiveConfiguration.Reset();

                //set model
                SessionData.ActiveConfiguration.CarModel = new CarModelViewModel(selectedCarModel);

                //set default engine settings
                var cheapestSettings = selectedCarModel.EngineSettings
                                       .OrderBy(cur => cur.Price)
                                       .First();
                SessionData.ActiveConfiguration.EngineSettingsId = cheapestSettings.Id;

                //set default paint
                SessionData.ActiveConfiguration.PaintId = context.Paints.First(cur => cur.IsDefault == true).Id;

                //set default rims
                SessionData.ActiveConfiguration.RimId = context.Rims.First(cur => cur.IsDefault == true).Id;
            }
        }
コード例 #3
0
 public JsonResult CarModelList()
 {
     using (var context = new CarConfiguratorEntityContext())
     {
         var result = context.CarModels.ToList().Select(cur => new CarModelViewModel(cur)).ToList();
         return(Json(result, JsonRequestBehavior.AllowGet));
     }
 }
コード例 #4
0
        protected ConfigurationBaseViewModel(CarConfiguratorEntityContext context, bool onlySelectedAccessories = false,
                                             bool onlySelectedEngineSettings = false, bool onlySelectedPaints = false, bool onlySelectedRims = false)
        {
            var config   = SessionData.ActiveConfiguration;
            var carModel = context.CarModels.First(cur => cur.Id == config.CarModel.Id);

            //accessories
            Accessories = context.Accessories.ToList()
                          .Select(cur => new AccessoryViewModel(cur)
            {
                IsSelected = config.AccessoryIds.Contains(cur.Id)
            })
                          .OrderBy(cur => cur.Price)
                          .ToList();
            if (onlySelectedAccessories)
            {
                Accessories = Accessories.Where(cur => cur.IsSelected).ToList();
            }

            //engine settings
            EngineSettings = carModel.EngineSettings.ToList()
                             .Select(cur => new EngineSettingsViewModel(cur)
            {
                IsSelected = (cur.Id == config.EngineSettingsId)
            })
                             .OrderBy(cur => cur.Price)
                             .ToList();
            if (onlySelectedEngineSettings)
            {
                EngineSettings = EngineSettings.Where(cur => cur.IsSelected).ToList();
            }

            //paints
            Paints = context.Paints.ToList()
                     .Select(cur => new PaintViewModel(cur)
            {
                IsSelected = (cur.Id == config.PaintId)
            })
                     .OrderBy(cur => cur.Name)
                     .ToList();
            if (onlySelectedPaints)
            {
                Paints = Paints.Where(cur => cur.IsSelected).ToList();
            }

            //rims
            Rims = context.Rims.ToList()
                   .Select(cur => new RimViewModel(cur)
            {
                IsSelected = (cur.Id == config.RimId)
            })
                   .ToList();
            if (onlySelectedRims)
            {
                Rims = Rims.Where(cur => cur.IsSelected).ToList();
            }
        }
コード例 #5
0
 public JsonResult LoadPage(int pageIndex)
 {
     using (var context = new CarConfiguratorEntityContext())
     {
         var orders = context.Orders.ToList().Skip(Constants.PageItemsCount * pageIndex).Take(Constants.PageItemsCount).ToList()
                      .Select(cur => new OrderViewModel(cur, Url.RouteUrl(Constants.Routes.ViewOrder, new { orderGuid = cur.Guid })))
                      .ToList();
         return(Json(orders, JsonRequestBehavior.AllowGet));
     }
 }
コード例 #6
0
 public ActionResult Index()
 {
     using (var context = new CarConfiguratorEntityContext())
     {
         return(View(new CarModelPageViewModel
         {
             CarModels = context.CarModels.ToList().Select(cur => new CarModelViewModel(cur)).ToList()
         }));
     }
 }
コード例 #7
0
 public ConfigurationOverviewPageViewModel(CarConfiguratorEntityContext context)
     : base(context, onlySelectedAccessories: true, onlySelectedEngineSettings: true, onlySelectedPaints: true, onlySelectedRims: true)
 {
     Configuration = new ConfigurationViewModel
     {
         Accessories    = Accessories,
         EngineSettings = EngineSettings.FirstOrDefault(),
         Paint          = Paints.FirstOrDefault(),
         Rims           = Rims.FirstOrDefault()
     };
     Configuration.InitPrice();
 }
コード例 #8
0
        private Configuration InitConfiguration(CarConfiguratorEntityContext context)
        {
            var configViewModel = SessionData.ActiveConfiguration;
            var configuration   = context.Configurations.Create();

            configuration.EngineSetting = context.EngineSettings.First(cur => cur.Id == configViewModel.EngineSettingsId);
            configuration.Paint         = context.Paints.First(cur => cur.Id == configViewModel.PaintId);
            configuration.Rims          = context.Rims.First(cur => cur.Id == configViewModel.RimId);
            configuration.Accessories   = context.Accessories.Where(cur => configViewModel.AccessoryIds.Contains(cur.Id)).ToList();

            return(configuration);
        }
コード例 #9
0
        public JsonResult LoadOrder(string guid)
        {
            using (var context = new CarConfiguratorEntityContext())
            {
                var order = context.Orders.FirstOrDefault(cur => cur.Guid == guid);
                if (order == null)
                {
                    throw new ArgumentException($"order {guid} was not found");
                }

                return(Json(new OrderOverviewPageViewModel(order, false), JsonRequestBehavior.AllowGet));
            }
        }
コード例 #10
0
        private OrderOverviewPageViewModel BuildViewModel(string orderGuid, bool orderJustPlaced)
        {
            using (var context = new CarConfiguratorEntityContext())
            {
                var order = context.Orders.FirstOrDefault(cur => cur.Guid == orderGuid);
                if (order == null)
                {
                    throw new ArgumentException($"order {orderGuid} was not found");
                }

                return(new OrderOverviewPageViewModel(order, MiscHelper.GenerateOrderLink(Request, Url, order.Guid), orderJustPlaced));
            }
        }
コード例 #11
0
        public JsonResult ConfigurationData(int carModelId)
        {
            using (var context = new CarConfiguratorEntityContext())
            {
                var carModel = context.CarModels.First(cur => cur.Id == carModelId);

                //accessories
                var accessories = context.Accessories.ToList()
                                  .Select(cur => new AccessoryViewModel(cur))
                                  .OrderBy(cur => cur.Price)
                                  .ToList();

                //engine settings
                var engineSettings = carModel.EngineSettings.ToList()
                                     .Select(cur => new EngineSettingsViewModel(cur))
                                     .OrderBy(cur => cur.Price)
                                     .ToList();

                //paints
                var paints = context.Paints.ToList()
                             .Select(cur => new PaintViewModel(cur))
                             .OrderBy(cur => cur.Name)
                             .ToList();

                //rims
                var rims = context.Rims.ToList()
                           .Select(cur => new RimViewModel(cur))
                           .ToList();

                //accessoryCategories
                var accessoryCategories = context.Categories.OfType <AccessoryCategory>().Select(cur => cur.Name).ToList();
                //paintCategories
                var paintCategories = context.Categories.OfType <PaintCategory>().Select(cur => cur.Name).ToList();

                return(Json(new
                {
                    model = new
                    {
                        series = carModel.SeriesCategory.Name,
                        bodyType = carModel.BodyCategory.Name,
                        year = carModel.Year
                    },
                    accessories,
                    engineSettings,
                    paints,
                    rims,
                    accessoryCategories,
                    paintCategories
                }, JsonRequestBehavior.AllowGet));
            }
        }
コード例 #12
0
 public ActionResult Index()
 {
     using (var context = new CarConfiguratorEntityContext())
     {
         var pageCount = CalculatePageCount(context.Orders.Count());
         return(View(new OrderListPageViewModel
         {
             PageCount = pageCount,
             Orders = context.Orders.Take(Constants.PageItemsCount).ToList()
                      .Select(cur => new OrderViewModel(cur, Url.RouteUrl(Constants.Routes.ViewOrder, new { orderGuid = cur.Guid })))
                      .ToList()
         }));
     }
 }
コード例 #13
0
 public JsonResult OrderList()
 {
     using (var context = new CarConfiguratorEntityContext())
     {
         var result = new
         {
             PageCount = CalculatePageCount(context.Orders.Count()),
             Orders    = context.Orders.Take(Constants.PageItemsCount).ToList()
                         .Select(cur => new OrderViewModel(cur))
                         .ToList()
         };
         return(Json(result, JsonRequestBehavior.AllowGet));
     }
 }
コード例 #14
0
        public ActionResult Index(int id)
        {
            if (!SessionData.ActiveConfiguration.IsValid(id, out string error))
            {
                Log.Error(error);
                new ModelController().InitCarModel(id);
                if (!SessionData.ActiveConfiguration.IsValid(id, out string error2))
                {
                    Log.Error($"failed to initialize car model: {error2}");
                    return(RedirectToRoute(Constants.Routes.ModelOverview));
                }
            }

            using (var context = new CarConfiguratorEntityContext())
            {
                return(View(new EngineSettingsPageViewModel(context)));
            }
        }
コード例 #15
0
        public string PlaceOrder(string description)
        {
            if (!Request.IsAjaxRequest())
            {
                Log.Error("PlaceOrder was called without ajax");
                Response.StatusCode = (int)HttpStatusCode.Forbidden;
                return("This action must be called with ajax");
            }
            //make sure the user didn't wait too long and the session already expired
            if (!SessionData.ActiveConfiguration.IsValid(null, out string error))
            {
                Log.Error(error);
                Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                return(error);
            }

            Configuration configuration;

            using (var context = new CarConfiguratorEntityContext())
            {
                //store the current configuration
                configuration = InitConfiguration(context);
                context.Configurations.Add(configuration);

                var newOrder = context.Orders.Create();
                newOrder.Description = description;
                newOrder.Guid        = MiscHelper.GenerateShortGuid();
                newOrder.DateTime    = DateTime.Now;

                (double basePrice, double extrasPrice)configurationPrice = CalculatePrice(configuration);
                newOrder.BasePrice   = configurationPrice.basePrice;
                newOrder.ExtrasPrice = configurationPrice.extrasPrice;

                newOrder.Configuration = configuration;
                context.Orders.Add(newOrder);
                context.SaveChanges();

                Log.Info($"configuration for order {newOrder.Id} was created");
                Log.Info($"a new order with id '{newOrder.Id}' was successfully placed");

                return(Url.RouteUrl(Constants.Routes.ViewOrderAfterPlaced, new { orderGuid = newOrder.Guid }));
            }
        }
コード例 #16
0
        private static void ClearDB(CarConfiguratorEntityContext context)
        {
            //remove many to many relationships first
            foreach (var cur in context.Configurations)
            {
                cur.Accessories.Clear();
            }

            context.Accessories.RemoveRange(context.Accessories);
            context.EngineSettings.RemoveRange(context.EngineSettings);
            context.CarModels.RemoveRange(context.CarModels);
            context.Engines.RemoveRange(context.Engines);
            context.Paints.RemoveRange(context.Paints);
            context.Rims.RemoveRange(context.Rims);
            context.Orders.RemoveRange(context.Orders);
            context.Categories.RemoveRange(context.Categories);

            Log.Info("resetting db data");
            context.SaveChanges();
            Log.Info("db data was reset");
        }
コード例 #17
0
        public JsonResult Delete(int id, int pageIndex)
        {
            using (var context = new CarConfiguratorEntityContext())
            {
                var toDelete = context.Orders.Find(id);
                if (toDelete != null)
                {
                    toDelete.Configuration.Accessories.Clear();
                    context.Orders.Remove(toDelete);
                    context.SaveChanges();
                }

                //return the item that's next in line because everything after the deleted item is moving up
                var newOrderItem = context.Orders.ToList().ElementAtOrDefault(pageIndex * Constants.PageItemsCount + (Constants.PageItemsCount - 1));
                return(Json(new
                {
                    NewPageCount = CalculatePageCount(context.Orders.Count()),
                    NewItem = (newOrderItem != null ? new OrderViewModel(newOrderItem, Url.RouteUrl(Constants.Routes.ViewOrder, new { orderGuid = newOrderItem.Guid })) : null)
                }));
            }
        }
コード例 #18
0
        public ActionResult Index(int id)
        {
            if (!SessionData.ActiveConfiguration.IsValid(id, out string error))
            {
                Log.Error(error);
                new ModelController().InitCarModel(id);
                if (!SessionData.ActiveConfiguration.IsValid(id, out string error2))
                {
                    Log.Error($"failed to initialize car model: {error2}");
                    return(RedirectToRoute(Constants.Routes.ModelOverview));
                }
            }

            using (var context = new CarConfiguratorEntityContext())
            {
                var accessoryCategories = context.Categories.OfType <AccessoryCategory>().Select(cur => cur.Name).ToList();
                return(View(new AccessoriesPageViewModel(context)
                {
                    AccessoryCategories = accessoryCategories
                }));
            }
        }
コード例 #19
0
 public ExteriorPageViewModel(CarConfiguratorEntityContext context)
     : base(context, onlySelectedAccessories: true, onlySelectedEngineSettings: true)
 {
 }
コード例 #20
0
 private Configuration InitConfiguration(CarConfiguratorEntityContext context, (int engineSettings, int[] accessories, int paint, int rims) data)
コード例 #21
0
 public EngineSettingsPageViewModel(CarConfiguratorEntityContext context)
     : base(context, onlySelectedAccessories: true, onlySelectedPaints: true, onlySelectedRims: true)
 {
 }