public async Task <IActionResult> Create([Bind("Name, SelectedComponentTypes")] CategoryViewModel categoryVM) { if (ModelState.IsValid) { Category tempCategory = new Category() { Name = categoryVM.Name, }; Category category = _context.Add(tempCategory).Entity; foreach (var id in categoryVM.SelectedComponentTypes) { ComponentType componentType = _context.ComponentType.Find(long.Parse(id)); ComponentTypeCategory tempCtc = new ComponentTypeCategory { Category = category, ComponentType = componentType }; ComponentTypeCategory ctc = _context.Add(tempCtc).Entity; } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(categoryVM)); }
public async Task <IActionResult> Edit(long id, [Bind("Name, SelectedComponentTypes, CategoryId")] CategoryViewModel vm) { if (id != long.Parse(vm.CategoryId)) { return(NotFound()); } if (ModelState.IsValid) { // Find category including the linked ComponentTypeCategories Category category = _context.Category .Include(c => c.ComponentTypeCategories) .Single(c => c.CategoryId == id); category.Name = vm.Name; foreach (var sid in vm.SelectedComponentTypes) { ComponentType componentType = _context.ComponentType.Find(long.Parse(sid)); var isPresent = category.ComponentTypeCategories.Single(ctc => ctc.ComponentType == componentType); if (isPresent == null) { var tempCtc = new ComponentTypeCategory { Category = category, ComponentType = componentType }; ComponentTypeCategory ctc = _context.Add(tempCtc).Entity; category.ComponentTypeCategories.Add(ctc); } } try { _context.Update(category); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!CategoryExists(category.CategoryId)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(vm)); }
public async Task <IActionResult> Create([Bind("SelectedCategories, ComponentName,ComponentInfo,Location,Status,Datasheet,ImageUrl,Manufacturer,WikiLink,AdminComment")] ComponentTypeViewModel vm) { if (ModelState.IsValid) { ComponentType tempComponentType = new ComponentType() { ComponentName = vm.ComponentName, AdminComment = vm.AdminComment, ComponentInfo = vm.ComponentInfo, Datasheet = vm.Datasheet, Location = vm.Location, WikiLink = vm.WikiLink, Status = vm.Status, Manufacturer = vm.Manufacturer, ImageUrl = vm.ImageUrl, Image = vm.Image, }; try { var componentType = _context.Add(tempComponentType).Entity; foreach (var id in vm.SelectedCategories) { Category cat = _context.Category.Find(long.Parse(id)); if (cat != null) { ComponentTypeCategory ctc = new ComponentTypeCategory { Category = cat, ComponentType = componentType, }; _context.Add(ctc); } } await _context.SaveChangesAsync(); } catch (Exception ex) { throw ex; } return(RedirectToAction(nameof(Index))); } return(View(vm)); }
private void UpdateComponentTypeCategories(string[] selectedCategories, ComponentType ctToUpdate) { if (selectedCategories == null) { ctToUpdate.ComponentTypeCategories = new List <ComponentTypeCategory>(); return; } var selectedCategoriesHS = new HashSet <string>(selectedCategories); var ctCourses = new HashSet <int> (ctToUpdate.ComponentTypeCategories.Select(ct => ct.Category.CategoryID)); foreach (var category in _context.Categories) { if (selectedCategoriesHS.Contains(category.CategoryID.ToString())) { if (!ctCourses.Contains(category.CategoryID)) { ctToUpdate.ComponentTypeCategories.Add(new ComponentTypeCategory { ComponentTypeID = ctToUpdate.ComponentTypeID, CategoryID = category.CategoryID }); } } else { if (ctCourses.Contains(category.CategoryID)) { ComponentTypeCategory categoryToRemove = ctToUpdate.ComponentTypeCategories .SingleOrDefault(ct => ct.CategoryID == category.CategoryID); _context.Remove(categoryToRemove); } } } }
public static void Initialize(StockContext context) { context.Database.EnsureCreated(); //check if there db was already created if (context.Components.Any()) { return; } var categories = new Category[] { new Category { Name = "IoT" }, new Category { Name = "Tablet" }, new Category { Name = "PC" }, new Category { Name = "Smartphone" } }; foreach (Category c in categories) { context.Categories.Add(c); } context.SaveChanges(); var componentTypes = new ComponentType[] { new ComponentType { ComponentTypeName = "Photon", ComponentInfo = "", AdminComment = "", Datasheet = "", ImageUrl = "", Manufacturer = "", WikiLink = "", Location = "", Image = new ESImage(), Status = ComponentTypeStatus.Available }, new ComponentType { ComponentTypeName = "Electron", ComponentInfo = "", AdminComment = "", Datasheet = "", ImageUrl = "", Manufacturer = "", WikiLink = "", Location = "", Image = new ESImage(), Status = ComponentTypeStatus.Available }, new ComponentType { ComponentTypeName = "Samsung Galaxy s7", ComponentInfo = "", AdminComment = "", Datasheet = "", ImageUrl = "", Manufacturer = "", WikiLink = "", Location = "", Image = new ESImage(), Status = ComponentTypeStatus.Available }, new ComponentType { ComponentTypeName = "LG G6", ComponentInfo = "", AdminComment = "", Datasheet = "", ImageUrl = "", Manufacturer = "", WikiLink = "", Location = "", Image = new ESImage(), Status = ComponentTypeStatus.Available }, new ComponentType { ComponentTypeName = "Lenovo 342", ComponentInfo = "", AdminComment = "", Datasheet = "", ImageUrl = "", Manufacturer = "", WikiLink = "", Location = "", Image = new ESImage(), Status = ComponentTypeStatus.Available }, new ComponentType { ComponentTypeName = "Dell XPS 1000", ComponentInfo = "", AdminComment = "", Datasheet = "", ImageUrl = "", Manufacturer = "", WikiLink = "", Location = "", Image = new ESImage(), Status = ComponentTypeStatus.Available }, new ComponentType { ComponentTypeName = "Dell Inspiron", ComponentInfo = "", AdminComment = "", Datasheet = "", ImageUrl = "", Manufacturer = "", WikiLink = "", Location = "", Image = new ESImage(), Status = ComponentTypeStatus.ReservedAdmin } }; foreach (ComponentType c in componentTypes) { context.ComponentTypes.Add(c); } context.SaveChanges(); var components = new Component[] { new Component { ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "Photon").ComponentTypeID, ComponentNumber = 123, Status = ComponentStatus.ReservedAdmin, AdminComment = "", SerialNo = "123", UserComment = "m", CurrentLoanInformationId = 2612 }, /*new Component{ComponentNumber=32, Status=ComponentStatus.Available, AdminComment="", SerialNo="32", UserComment="hdgm", CurrentLoanInformationId=1552 }, * new Component{ComponentNumber=54, Status=ComponentStatus.ReservedAdmin, AdminComment="", SerialNo="54", UserComment="mfdg", CurrentLoanInformationId=142 }, * new Component{ComponentNumber=234, Status=ComponentStatus.ReservedAdmin, AdminComment="", SerialNo="234", UserComment="mhg", CurrentLoanInformationId=112 }, * new Component{ComponentNumber=653, Status=ComponentStatus.ReservedAdmin, AdminComment="", SerialNo="653", UserComment="mdg", CurrentLoanInformationId=162 }, * new Component{ComponentNumber=676, Status=ComponentStatus.ReservedAdmin, AdminComment="", SerialNo="676", UserComment="mgh", CurrentLoanInformationId=142 } */ }; foreach (Component c in components) { context.Components.Add(c); } context.SaveChanges(); var ctc = new ComponentTypeCategory[] { new ComponentTypeCategory { CategoryID = categories.Single(c => c.Name == "IoT").CategoryID, ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "Photon").ComponentTypeID }, new ComponentTypeCategory { CategoryID = categories.Single(c => c.Name == "IoT").CategoryID, ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "Electron").ComponentTypeID }, new ComponentTypeCategory { CategoryID = categories.Single(c => c.Name == "Smartphone").CategoryID, ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "Samsung Galaxy s7").ComponentTypeID }, new ComponentTypeCategory { CategoryID = categories.Single(c => c.Name == "Smartphone").CategoryID, ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "LG G6").ComponentTypeID }, new ComponentTypeCategory { CategoryID = categories.Single(c => c.Name == "PC").CategoryID, ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "Dell XPS 1000").ComponentTypeID }, new ComponentTypeCategory { CategoryID = categories.Single(c => c.Name == "PC").CategoryID, ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "Dell Inspiron").ComponentTypeID }, new ComponentTypeCategory { CategoryID = categories.Single(c => c.Name == "PC").CategoryID, ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "Lenovo 342").ComponentTypeID }, new ComponentTypeCategory { CategoryID = categories.Single(c => c.Name == "IoT").CategoryID, ComponentTypeID = componentTypes.Single(ct => ct.ComponentTypeName == "LG G6").ComponentTypeID } }; foreach (ComponentTypeCategory c in ctc) { context.ComponentTypeCategories.Add(c); } context.SaveChanges(); }
public static void Initializer(ApplicationDbContext context) { context.Database.EnsureCreated(); if (context.Components.Any()) { return; } var component1 = new Component { ComponentNumber = 89, SerialNo = "KL89", Status = ComponentStatus.Available, AdminComment = "Working", UserComment = "A small flaw" }; var component2 = new Component { ComponentNumber = 42, SerialNo = "ML42", Status = ComponentStatus.Loaned, AdminComment = "User au593874 has it", UserComment = "I'm keeping it" }; var component3 = new Component { ComponentNumber = 3, SerialNo = "JK69", Status = ComponentStatus.Loaned, AdminComment = "Lots of fun", UserComment = "Sure is" }; context.Components.Add(component1); context.Components.Add(component2); context.Components.Add(component3); context.SaveChanges(); var image = new Byte[0];//System.IO.File.ReadAllBytes("resistor.jpg"); var esimage = new ESImage { ImageData = image, Thumbnail = image, ImageMimeType = "image/jpeg" }; var componentType1 = new ComponentType { ComponentName = "Resistor", ComponentInfo = "Just a resistor", Location = "Shelf", Status = ComponentTypeStatus.Available, Datasheet = "data", ImageUrl = "url", Image = esimage, Manufacturer = "Hest og Co A/S", WikiLink = "wikilink", AdminComment = "comment", }; var componentType2 = new ComponentType { ComponentName = "Pump", ComponentInfo = "Just a pump", Location = "Drawer 5", Status = ComponentTypeStatus.Available, Datasheet = "Push up 4 times", ImageUrl = "url", Image = esimage, Manufacturer = "Fitness World", WikiLink = "wikilink", AdminComment = "Be careful if you don't want to be big", }; componentType1.Components.Add(component1); componentType1.Components.Add(component2); context.ComponentTypes.Add(componentType1); componentType2.Components.Add(component3); context.ComponentTypes.Add(componentType2); context.SaveChanges(); var category = new Category { Name = "Test" }; var categoryComponentType1 = new ComponentTypeCategory { Category = category, ComponentType = componentType1 }; var categoryComponentType2 = new ComponentTypeCategory { Category = category, ComponentType = componentType2 }; category.ComponentTypeCategories.Add(categoryComponentType1); category.ComponentTypeCategories.Add(categoryComponentType2); context.Categories.Add(category); //context.CategoryComponentTypes.Add(categoryComponentType); context.SaveChanges(); }