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(); } }
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")); } }
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)); } }
public IActionResult List() { using (var db = new AuctionHubDbContext()) { var allProducts = db .Products .Include(p => p.Owner) .OrderByDescending(p => p.Id) .ToList(); return(View(allProducts)); } }
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)); }
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)); } }
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; }
public BidService(AuctionHubDbContext db, UserManager <User> userManager) { this.db = db; this.userManager = userManager; }
public ProductService(AuctionHubDbContext db) { this.db = db; }
public CategoryService(AuctionHubDbContext db) { this.db = db; }
public AuctionService(AuctionHubDbContext db) { this.db = db; }
public PictureService(AuctionHubDbContext db) { this.db = db; }
public AdminUserService(AuctionHubDbContext db) { this.db = db; }
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; }