private void UpdateProductGuideProducts(string[] selectedProducts, ProductGuide productGuideToUpdate) { if (selectedProducts == null) { productGuideToUpdate.ProductAssignments = new List <ProductAssignment>(); return; } var selectedProductsHS = new HashSet <string>(selectedProducts); var productGuideProducts = new HashSet <int> (productGuideToUpdate.ProductAssignments.Select(c => c.Product.ProductID)); foreach (var product in _context.Products) { if (selectedProductsHS.Contains(product.ProductID.ToString())) { if (!productGuideProducts.Contains(product.ProductID)) { productGuideToUpdate.ProductAssignments.Add(new ProductAssignment { ProductGuideID = productGuideToUpdate.ID, ProductID = product.ProductID }); } } else { if (productGuideProducts.Contains(product.ProductID)) { ProductAssignment productToRemove = productGuideToUpdate.ProductAssignments.FirstOrDefault(i => i.ProductID == product.ProductID); _context.Remove(productToRemove); } } } }
// GET: ProductGuides/Create public IActionResult Create() { var productGuide = new ProductGuide(); productGuide.ProductAssignments = new List <ProductAssignment>(); PopulateAssignedProductData(productGuide); return(View()); }
public async Task <IActionResult> OnGetAsync(int?id) { if (id == null) { return(NotFound()); } ProductGuide = await _context.ProductGuides.FirstOrDefaultAsync(m => m.ID == id); if (ProductGuide == null) { return(NotFound()); } return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } ProductGuide = await _context.ProductGuides.FindAsync(id); if (ProductGuide != null) { _context.ProductGuides.Remove(ProductGuide); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> DeleteConfirmed(int id) { ProductGuide productGuide = await _context.ProductGuides .Include(pg => pg.ProductAssignments) .SingleAsync(pg => pg.ID == id); var markets = await _context.Markets .Where(m => m.ProductGuideID == id) .ToListAsync(); markets.ForEach(m => m.ProductGuideID = null); _context.ProductGuides.Remove(productGuide); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
private void PopulateAssignedProductData(ProductGuide productGuide) { var allProducts = _context.Products; var productGuideProducts = new HashSet <int>(productGuide.ProductAssignments.Select(c => c.ProductID)); var viewModel = new List <AssignedProductData>(); foreach (var product in allProducts) { viewModel.Add(new AssignedProductData { ProductID = product.ProductID, ProductName = product.ProductName, Assigned = productGuideProducts.Contains(product.ProductID) }); } ViewData["Products"] = viewModel; }
// GET: ProductGuides public async Task <IActionResult> Index(int?id, int?productID) { var viewModel = new ProductGuideIndexData(); viewModel.ProductGuides = await _context.ProductGuides .Include(i => i.CountryAssignment) .Include(i => i.ProductAssignments) .ThenInclude(i => i.Product) .ThenInclude(i => i.Subscriptions) .ThenInclude(i => i.Customer) .Include(i => i.ProductAssignments) .ThenInclude(i => i.Product) .ThenInclude(i => i.Market) .AsNoTracking() .OrderBy(i => i.LastName) .ToListAsync(); if (id != null) { ViewData["ProductGuideID"] = id.Value; ProductGuide productGuide = viewModel.ProductGuides.Where( i => i.ID == id.Value).Single(); viewModel.Products = productGuide.ProductAssignments.Select(s => s.Product); } if (productID != null) { ViewData["ProductID"] = productID.Value; var selectedProduct = viewModel.Products.Where(x => x.ProductID == productID).Single(); await _context.Entry(selectedProduct).Collection(x => x.Subscriptions).LoadAsync(); foreach (Subscription subscription in selectedProduct.Subscriptions) { await _context.Entry(subscription).Reference(x => x.Customer).LoadAsync(); } viewModel.Subscriptions = selectedProduct.Subscriptions; } return(View(viewModel)); }
public async Task OnGetAsync(int?id, int?productID) { ProductGuideData = new ProductGuideIndexData(); ProductGuideData.ProductGuides = await _context.ProductGuides .Include(pg => pg.CountryAssignment) .Include(pg => pg.ProductAssignments) .ThenInclude(pg => pg.Product) .ThenInclude(pg => pg.Market) // .Include(pg => pg.ProductAssignments) // .ThenInclude(pg => pg.Product) // .ThenInclude(pg => pg.Subscriptions) // .ThenInclude(pg => pg.Customer) // .AsNoTracking() .OrderBy(pg => pg.LastName) .ToListAsync(); if (id != null) { ProductGuideID = id.Value; ProductGuide productGuide = ProductGuideData.ProductGuides .Where(pg => pg.ID == id.Value).Single(); ProductGuideData.Products = productGuide.ProductAssignments.Select(pa => pa.Product); } if (productID != null) { ProductID = productID.Value; var selectedProduct = ProductGuideData.Products .Where(pg => pg.ProductID == productID).Single(); await _context.Entry(selectedProduct).Collection(x => x.Subscriptions).LoadAsync(); foreach (Subscription subscription in selectedProduct.Subscriptions) { await _context.Entry(subscription).Reference(x => x.Customer).LoadAsync(); } ProductGuideData.Subscriptions = selectedProduct.Subscriptions; } }
public async Task <IActionResult> Create([Bind("CountryAssignment,LastName,FirstMidName,HireDate")] ProductGuide productGuide, string[] selectedProducts) { if (selectedProducts != null) { productGuide.ProductAssignments = new List <ProductAssignment>(); foreach (var product in selectedProducts) { var productToAdd = new ProductAssignment { ProductGuideID = productGuide.ID, ProductID = int.Parse(product) }; productGuide.ProductAssignments.Add(productToAdd); } } if (ModelState.IsValid) { _context.Add(productGuide); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } PopulateAssignedProductData(productGuide); return(View(productGuide)); }
public static void Initialize(BusinessContext context) { //context.Database.EnsureCreated(); // Look for any customers. if (context.Customers.Any()) { return; // DB has been seeded } var customers = new Customer[] { new Customer { FirstMidName = "Ahmad", LastName = "Jensen", SubscriptionDate = DateTime.Parse("2018-09-01") }, new Customer { FirstMidName = "Meredith", LastName = "Alonso", SubscriptionDate = DateTime.Parse("2017-09-01") }, new Customer { FirstMidName = "Arturo", LastName = "Anand", SubscriptionDate = DateTime.Parse("2018-09-01") }, new Customer { FirstMidName = "Gytis", LastName = "Barzdukas", SubscriptionDate = DateTime.Parse("2017-09-01") }, new Customer { FirstMidName = "Yan", LastName = "Li", SubscriptionDate = DateTime.Parse("2017-09-01") }, new Customer { FirstMidName = "Peggy", LastName = "Justice", SubscriptionDate = DateTime.Parse("2016-09-01") }, new Customer { FirstMidName = "Laura", LastName = "Norman", SubscriptionDate = DateTime.Parse("2018-09-01") }, new Customer { FirstMidName = "Nino", LastName = "Olivetto", SubscriptionDate = DateTime.Parse("2019-09-01") } }; foreach (Customer c in customers) { context.Customers.Add(c); } context.SaveChanges(); var productGuides = new ProductGuide[] { new ProductGuide { FirstMidName = "Faizan", LastName = "Quershi", HireDate = DateTime.Parse("2000-03-11") }, new ProductGuide { FirstMidName = "Fadi", LastName = "Fakhouri", HireDate = DateTime.Parse("2002-07-06") }, new ProductGuide { FirstMidName = "Roger", LastName = "Harui", HireDate = DateTime.Parse("1998-07-01") }, new ProductGuide { FirstMidName = "Candace", LastName = "Kapoor", HireDate = DateTime.Parse("2001-01-15") }, new ProductGuide { FirstMidName = "Roger", LastName = "Youssef", HireDate = DateTime.Parse("2004-02-12") } }; foreach (ProductGuide pg in productGuides) { context.ProductGuides.Add(pg); } context.SaveChanges(); var markets = new Market[] { new Market { Name = "England", Budget = 350000, StartDate = DateTime.Parse("2000-09-01"), ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID }, new Market { Name = "Danmark", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), ProductGuideID = productGuides.Single(pg => pg.LastName == "Youssef").ID }, new Market { Name = "Rusland", Budget = 350000, StartDate = DateTime.Parse("2007-09-01"), ProductGuideID = productGuides.Single(pg => pg.LastName == "Harui").ID }, new Market { Name = "Sverige", Budget = 100000, StartDate = DateTime.Parse("2007-09-01"), ProductGuideID = productGuides.Single(pg => pg.LastName == "Kapoor").ID } }; foreach (Market m in markets) { context.Markets.Add(m); } context.SaveChanges(); var products = new Product[] { new Product { ProductID = 1050, ProductName = "CD-ORD", Price = 100, MarketID = markets.Single(s => s.Name == "Danmark").MarketID }, new Product { ProductID = 4022, ProductName = "Intowords", Price = 100, MarketID = markets.Single(s => s.Name == "England").MarketID }, new Product { ProductID = 4041, ProductName = "Reading Pen", Price = 100, MarketID = markets.Single(s => s.Name == "Rusland").MarketID }, new Product { ProductID = 1045, ProductName = "CD-ORD and Intowords", Price = 150, MarketID = markets.Single(s => s.Name == "Sverige").MarketID }, new Product { ProductID = 3141, ProductName = "CD-ORD and Reading Pen", Price = 150, MarketID = markets.Single(s => s.Name == "Danmark").MarketID }, new Product { ProductID = 2021, ProductName = "Intowords and Reading Pen", Price = 150, MarketID = markets.Single(s => s.Name == "Danmark").MarketID }, new Product { ProductID = 2042, ProductName = "Intowords", Price = 100, MarketID = markets.Single(s => s.Name == "Danmark").MarketID }, }; //foreach (Product p in products) //{ // context.Products.Add(p); //} // Istedet for det udkommenterede foreach loop brug følgende linje: context.AddRange(products); context.SaveChanges(); var countryAssignments = new CountryAssignment[] { new CountryAssignment { ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID, Location = "Odense" }, new CountryAssignment { ProductGuideID = productGuides.Single(pg => pg.LastName == "Harui").ID, Location = "Gowan 27" }, new CountryAssignment { ProductGuideID = productGuides.Single(pg => pg.LastName == "Kapoor").ID, Location = "Odense vej 23 DK" }, }; foreach (CountryAssignment ca in countryAssignments) { context.CountryAssignments.Add(ca); } context.SaveChanges(); var productProductGuides = new ProductAssignment[] { new ProductAssignment { ProductID = products.Single(p => p.ProductName == "CD-ORD").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Youssef").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Intowords").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Reading Pen").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Harui").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "CD-ORD and Intowords").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Kapoor").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Intowords and Reading Pen").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Youssef").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "CD-ORD").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Youssef").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Intowords").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID }, new ProductAssignment { ProductID = products.Single(p => p.ProductName == "Intowords").ProductID, ProductGuideID = productGuides.Single(pg => pg.LastName == "Quershi").ID }, }; foreach (ProductAssignment pa in productProductGuides) { context.ProductAssignments.Add(pa); } context.SaveChanges(); var subscriptions = new Subscription[] { new Subscription { CustomerID = customers.Single(c => c.LastName == "Jensen").ID, ProductID = products.Single(p => p.ProductName == "Intowords").ProductID, CustomerLoyalty = CustomerLoyalty.C }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alexander").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.C }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alexander").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alonso").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alonso").ID, ProductID = products.Single(c => c.ProductName == "Intowords").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Alonso").ID, ProductID = products.Single(c => c.ProductName == "Intowords").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Anand").ID, ProductID = products.Single(c => c.ProductName == "Intowords").ProductID }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Anand").ID, ProductID = products.Single(c => c.ProductName == "Intowords").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Barzdukas").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Li").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B }, new Subscription { CustomerID = customers.Single(s => s.LastName == "Justice").ID, ProductID = products.Single(c => c.ProductName == "CD-ORD").ProductID, CustomerLoyalty = CustomerLoyalty.B } }; foreach (Subscription s in subscriptions) { var subscriptionDataBase = context.Subscriptions.Where( c => c.Customer.ID == s.CustomerID && c.Product.ProductID == s.ProductID).SingleOrDefault(); if (subscriptionDataBase == null) { context.Subscriptions.Add(s); } } context.SaveChanges(); }
public async Task <IActionResult> Edit(int?id, byte[] rowVersion) { if (id == null) { return(NotFound()); } var marketToUpdate = await _context.Markets.Include(i => i.Administrator).FirstOrDefaultAsync(m => m.MarketID == id); if (marketToUpdate == null) { Market deletedMarket = new Market(); await TryUpdateModelAsync(deletedMarket); ModelState.AddModelError(string.Empty, "Unable to save changes. The department was deleted by another user."); ViewData["ProductGuideID"] = new SelectList(_context.ProductGuides, "ID", "FullName", deletedMarket.ProductGuideID); return(View(deletedMarket)); } _context.Entry(marketToUpdate).Property("RowVersion").OriginalValue = rowVersion; if (await TryUpdateModelAsync <Market>( marketToUpdate, "", s => s.Name, s => s.StartDate, s => s.Budget, s => s.ProductGuideID)) { try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var clientValues = (Market)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save changes. The market was deleted by another user."); } else { var databaseValues = (Market)databaseEntry.ToObject(); if (databaseValues.Name != clientValues.Name) { ModelState.AddModelError("Name", $"Current value: {databaseValues.Name}"); } if (databaseValues.Budget != clientValues.Budget) { ModelState.AddModelError("Budget", $"Current value: {databaseValues.Budget:c}"); } if (databaseValues.StartDate != clientValues.StartDate) { ModelState.AddModelError("StartDate", $"Current value: {databaseValues.StartDate:d}"); } if (databaseValues.ProductGuideID != clientValues.ProductGuideID) { ProductGuide databaseProductGuide = await _context.ProductGuides.FirstOrDefaultAsync(i => i.ID == databaseValues.ProductGuideID); ModelState.AddModelError("InstructorID", $"Current value: {databaseProductGuide?.FullName}"); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); marketToUpdate.RowVersion = (byte[])databaseValues.RowVersion; ModelState.Remove("RowVersion"); } } } ViewData["ProductGuideID"] = new SelectList(_context.ProductGuides, "ID", "FullName", marketToUpdate.ProductGuideID); return(View(marketToUpdate)); }