Beispiel #1
0
        public ViewResult ProductInfo(Product formProduct)
        {
            ProductRepository repo = new ProductRepository();

            //After we get the information back from the form...
            if (ModelState.IsValid)
            {
                //...update the database with it.
                LLDbContext productDB       = new LLDbContext();
                Product     productToUpdate = productDB.Products.Find(formProduct.ProductID);
                if (productToUpdate != null)
                {
                    productToUpdate.Description = formProduct.Description;
                    productToUpdate.Category    = formProduct.Category;
                    productToUpdate.InStock     = formProduct.InStock;
                    productToUpdate.Price       = formProduct.Price;
                    productToUpdate.ProductName = formProduct.ProductName;
                    //productToUpdate.Shipping = formProduct.Shipping;
                    productDB.SaveChanges();

                    //This means that the user was editing the product, not creating a new one.
                    //In this case, we should try to retrieve the product with the same ID as the one passed in as a parameter.

                    return(View((from p in repo.Products
                                 where p.ProductID == formProduct.ProductID
                                 select p).FirstOrDefault <Product>()));
                }
                else
                {
                    Product newProduct = new Product();
                    newProduct.Description = formProduct.Description;
                    newProduct.Category    = formProduct.Category;
                    newProduct.InStock     = formProduct.InStock;
                    newProduct.Price       = formProduct.Price;
                    newProduct.ProductName = formProduct.ProductName;
                    productDB.Products.Add(newProduct);
                    productDB.SaveChanges();

                    //This means that the user is trying to create a new product.
                    //Because its ID will be auto-generated by the database, we should instead just pass
                    //the newProduct object to the view for now.

                    return(View(newProduct));
                }
            }

            else
            {
                return(View("ProductIndex"));
            }
        }
Beispiel #2
0
        private static void CreatePermissions(LLDbContext context)
        {
            typeof(PermissionAttribute).GetTypeInfo().Assembly
            .GetTypes()
            .SelectMany(c => c.GetMethods(BindingFlags.Instance | BindingFlags.Public))
            .Where(c => c.CustomAttributes.Any(d => d.AttributeType == typeof(PermissionAttribute)))
            .Select(c => c.GetCustomAttribute <PermissionAttribute>())
            .ToList()
            .ForEach(permissionAttribute =>
            {
                if (!context.Permissions.Any(c => c.Id == permissionAttribute.PermissionId))
                {
                    context.Permissions.Add(new Permission
                    {
                        Id          = permissionAttribute.PermissionId,
                        Description = permissionAttribute.Description,
                        RouteId     = permissionAttribute.RouteId
                    });
                }
            });

            context.SaveChanges();
        }