Exemplo n.º 1
0
        public IActionResult Edit(int?productId)
        {
            if (productId == null)
            {
                return(BadRequest());
            }

            using (var db = new AuctionHubDbContext())
            {
                var loggedUserId = db.Users.First(u => u.Name == this.User.Identity.Name).Id;

                var productToEdit = db
                                    .Products
                                    .FirstOrDefault(p => p.Id == productId);

                if (!IsUserAuthorizedToEdit(productToEdit, loggedUserId))
                {
                    return(Forbid());
                }

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

                var model = new ProductViewModel(productToEdit.Id, productToEdit.Name, productToEdit.Description);

                return(View(model));
            }
        }
        /// <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();
            }
        }
Exemplo n.º 3
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"));
            }
        }
Exemplo n.º 4
0
        public IActionResult Delete(int?productId)
        {
            if (productId == null)
            {
                return(BadRequest());
            }

            using (var db = new AuctionHubDbContext())
            {
                var loggedUserId = db.Users.First(u => u.Name == this.User.Identity.Name).Id;

                var productToBeDeleted = db
                                         .Products
                                         .FirstOrDefault(p => p.Id == productId);

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

                if (!IsUserAuthorizedToEdit(productToBeDeleted, loggedUserId))
                {
                    return(Forbid());
                }

                return(View(productToBeDeleted));
            }
        }
Exemplo n.º 5
0
        public IActionResult List()
        {
            using (var db = new AuctionHubDbContext())
            {
                var allProducts = db
                                  .Products
                                  .Include(p => p.Owner)
                                  .OrderByDescending(p => p.Id)
                                  .ToList();

                return(View(allProducts));
            }
        }
Exemplo n.º 6
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));
        }
Exemplo n.º 7
0
        public IActionResult Details(int?productId)
        {
            if (productId == null)
            {
                return(BadRequest());
            }

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

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

                return(View(currentProduct));
            }
        }
Exemplo n.º 8
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"));
        }
 public CommentService(AuctionHubDbContext db)
 {
     this.db = db;
 }
Exemplo n.º 10
0
 public BidService(AuctionHubDbContext db, UserManager <User> userManager)
 {
     this.db          = db;
     this.userManager = userManager;
 }
Exemplo n.º 11
0
 public ProductService(AuctionHubDbContext db)
 {
     this.db = db;
 }
Exemplo n.º 12
0
 public CategoryService(AuctionHubDbContext db)
 {
     this.db = db;
 }
Exemplo n.º 13
0
 public AuctionService(AuctionHubDbContext db)
 {
     this.db = db;
 }
Exemplo n.º 14
0
 public PictureService(AuctionHubDbContext db)
 {
     this.db = db;
 }
 public AdminUserService(AuctionHubDbContext db)
 {
     this.db = db;
 }
Exemplo n.º 16
0
 public BidController(IBidService bidService, IAuctionService auctionService, IProductService productService, AuctionHubDbContext db, UserManager <User> userManager)
 {
     this.auctionService = auctionService;
     this.bidService     = bidService;
     this.userManager    = userManager;
     this.productService = productService;
 }