示例#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");
                }
            }
        }
        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 }));
            }
        }
        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)
                }));
            }
        }
示例#4
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");
        }