/// <summary>
        /// This is a workaround for missing seed data functionality in EF 7.0-rc1
        /// More info: https://github.com/aspnet/EntityFramework/issues/629
        /// </summary>
        /// <param name="app">
        /// An instance that provides the mechanisms to get instance of the database context.
        /// </param>
        public static void SeedData(this IApplicationBuilder app)
        {
            using (IServiceScope serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope())
            {
                AuctionHubDbContext db = serviceScope.ServiceProvider.GetService <AuctionHubDbContext>();

                Town initialTown = db.Towns.FirstOrDefault(t => t.Name == "Other");
                if (initialTown == null)
                {
                    db.Towns.Add(new Town()
                    {
                        Name = "Other"
                    });
                }
                db.SaveChanges();

                Category initialCategory = db.Categories.FirstOrDefault(c => c.Name == "Other");
                if (initialCategory == null)
                {
                    db.Categories.Add(new Category()
                    {
                        Name = "Other"
                    });
                    db.Categories.Add(new Category()
                    {
                        Name = "Collectibles"
                    });
                    db.Categories.Add(new Category()
                    {
                        Name = "Art"
                    });
                }
                db.SaveChanges();
            }
        }
예제 #2
0
        public IActionResult DeleteConfirmed(int?productId)
        {
            if (productId == null)
            {
                return(BadRequest());
            }

            using (var db = new AuctionHubDbContext())
            {
                var productToBeDeleted = db
                                         .Products
                                         .FirstOrDefault(p => p.Id == productId);

                if (productToBeDeleted == null)
                {
                    return(NotFound());
                }

                // Here, before we delete the product, its pictures in the file system should be deleted as well!
                // DeleteProductPictures(productToBeDeleted);

                db.Products.Remove(productToBeDeleted);
                db.SaveChanges();

                return(RedirectToAction("Index", "Home"));
            }
        }
예제 #3
0
        public IActionResult Create(Product productToCreate)
        {
            if (ModelState.IsValid)
            {
                using (var db = new AuctionHubDbContext())
                {
                    var ownerId = db.Users
                                  .First(u => u.UserName == this.User.Identity.Name)
                                  .Id;

                    productToCreate.OwnerId = ownerId;

                    db.Products.Add(productToCreate);
                    db.SaveChanges();

                    return(RedirectToAction("Index", "Home"));
                }
            }

            return(View(productToCreate));
        }
예제 #4
0
        public IActionResult Edit(ProductViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new AuctionHubDbContext())
                {
                    var productToEdit = db
                                        .Products
                                        .First(p => p.Id == model.Id);

                    productToEdit.Name        = model.Name;
                    productToEdit.Description = model.Description;

                    db.Entry(productToEdit).State = EntityState.Modified;
                    db.SaveChanges();

                    return(RedirectToAction("Details/" + productToEdit.Id, "Product"));
                }
            }

            return(RedirectToAction("Index", "Home"));
        }