public async Task <IActionResult> Create([Bind("ComponentTypeId,ComponentName,ComponentInfo,Location,Status,Datasheet,ImageUrl,Manufacturer,WikiLink,AdminComment")] ComponentType componentType, [Bind("Categories")] int[] categories)
        {
            if (ModelState.IsValid)
            {
                _context.Add(componentType);
                await _context.SaveChangesAsync();

                foreach (var categoryId in categories)
                {
                    var categoryFromdb        = _context.Categories.SingleOrDefault(x => x.CategoryId == categoryId);
                    var categoryComponentType = new CategoryComponentType
                    {
                        Category        = categoryFromdb,
                        CategoryId      = categoryId,
                        ComponentType   = componentType,
                        ComponentTypeId = componentType.ComponentTypeId
                    };

                    componentType.Categories.Add(categoryComponentType);
                    categoryFromdb.ComponentTypes.Add(categoryComponentType);
                }

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(componentType));
        }
예제 #2
0
        public IActionResult Create(CategoryViewModel model)
        {
            //needs to create a category and create the binding to the chosen componenttypes
            var cat = new Category();

            cat.Name = model.Name;

            using (var db = new ApplicationDbContext())
            {
                db.Categories.Add(cat);


                if (model.ComponentTypeIds != null)
                {
                    foreach (int id in model.ComponentTypeIds)
                    {
                        var type   = db.ComponentTypes.Find(id);
                        var cattyp = new CategoryComponentType()
                        {
                            Category = cat, ComponentType = type
                        };
                        db.CategoryComponentTypes.Add(cattyp);
                    }
                }


                db.SaveChanges();
            }


            return(RedirectToAction("", "category", new { area = "" }));
        }
예제 #3
0
        public IActionResult Update(CategoryViewModel model)
        {
            using (var db = new EmbeddedStockContext())
            {
                var edit = new Category {
                    CategoryId = model.Id, Name = model.Name
                };
                db.Categories.Attach(edit);
                var entry = db.Entry(edit);
                entry.Property(e => e.Name).IsModified = true;

                db.CategoryComponentTypes.RemoveRange(db.CategoryComponentTypes.Where(c => c.CategoryId == model.Id));

                foreach (int id in model.ComponentTypeIds)
                {
                    var type = db.ComponentTypes.Find(id);
                    var cat  = db.Categories.Find(model.Id);

                    var cattyp = new CategoryComponentType()
                    {
                        Category = cat, ComponentType = type
                    };
                    db.CategoryComponentTypes.Add(cattyp);
                }

                db.SaveChanges();
            }

            //find category and update with new name and component types
            return(RedirectToAction("", "category", new { area = "" }));
        }
        public async Task <IActionResult> Create(ComponentTypeViewModel componentTypeVM)
        {
            if (!ModelState.IsValid)
            {
                return(View(componentTypeVM));
            }

            if (componentTypeVM.SelectedCategories != null)
            {
                var selectedCategories = _context.Categories
                                         .Where(x => componentTypeVM.SelectedCategories
                                                .Contains(x.CategoryId.ToString()));

                var cctList = new List <CategoryComponentType>();

                foreach (var selectedCategory in selectedCategories)
                {
                    var cct = new CategoryComponentType
                    {
                        CategoryId    = selectedCategory.CategoryId,
                        ComponentType = componentTypeVM.ComponentType
                    };

                    cctList.Add(cct);
                }

                componentTypeVM.ComponentType.CategoryComponentTypes = cctList;
            }

            _context.Add(componentTypeVM.ComponentType);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
        public IActionResult Edit(ComponentTypeViewModel componentTypeVM)
        {
            if (!ModelState.IsValid)
            {
                return(View(componentTypeVM));
            }

            var id = componentTypeVM.ComponentType.ComponentTypeId;

            try
            {
                _context.CategoryComponentType.RemoveRange(
                    _context.CategoryComponentType.Where(x => x.ComponentTypeId == id));

                if (componentTypeVM.SelectedCategories != null)
                {
                    var selectedCategories = _context.Categories
                                             .Where(x => componentTypeVM.SelectedCategories
                                                    .Contains(x.CategoryId.ToString()));

                    var cctList = new List <CategoryComponentType>();

                    foreach (var selectedCategory in selectedCategories)
                    {
                        var cct = new CategoryComponentType
                        {
                            CategoryId    = selectedCategory.CategoryId,
                            ComponentType = componentTypeVM.ComponentType
                        };

                        cctList.Add(cct);
                    }

                    componentTypeVM.ComponentType.CategoryComponentTypes = cctList;
                }

                _context.Update(componentTypeVM.ComponentType);

                _context.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ComponentTypeExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToAction(nameof(Index)));
        }
        public IActionResult SaveComponentType(ComponentTypeViewModel viewModel)
        {
            //TODO: Create new ComponentType in Database

            if (viewModel.ComponentType.ComponentTypeId == 0)
            {
                //If creating
                viewModel.ComponentType.Status = ComponentTypeStatus.ReservedAdmin;

                var categoryComponentType = new CategoryComponentType();

                categoryComponentType.CategoryId = viewModel.Category.CategoryId;

                categoryComponentType.ComponentType = viewModel.ComponentType;

                _unitOfWork.CategoryComponentTypes.Add(categoryComponentType);
            }
            else
            {
                //If editing
                var componentTypeInDb =
                    _unitOfWork.ComponentTypes.Get(viewModel.ComponentType.ComponentTypeId);

                componentTypeInDb.ComponentName = viewModel.ComponentType.ComponentName;
                componentTypeInDb.ComponentInfo = viewModel.ComponentType.ComponentInfo;
                componentTypeInDb.Location      = viewModel.ComponentType.Location;
                componentTypeInDb.Status        = viewModel.ComponentType.Status;
                componentTypeInDb.Datasheet     = viewModel.ComponentType.Datasheet;
                componentTypeInDb.ImageUrl      = viewModel.ComponentType.ImageUrl;
                componentTypeInDb.Manufacturer  = viewModel.ComponentType.Manufacturer;
                componentTypeInDb.WikiLink      = viewModel.ComponentType.WikiLink;
                componentTypeInDb.AdminComment  = viewModel.ComponentType.AdminComment;
                componentTypeInDb.Image         = viewModel.ComponentType.Image;
            }



            _unitOfWork.Complete();



            return(RedirectToAction("ComponentTypes", "ComponentType", new { id = viewModel.Category.CategoryId }));
        }
예제 #7
0
        public static void Initializer(EmbeddedStockContext 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 image = new Byte[0];//System.IO.File.ReadAllBytes("resistor.jpg");

            var esimage = new ESImage {
                ImageData     = image,
                Thumbnail     = image,
                ImageMimeType = "image/jpeg"
            };
            var componenttype = new ComponentType
            {
                ComponentName = "Resistor",
                ComponentInfo = "Just a resistor",
                Location      = "in yer butt",
                Status        = ComponentTypeStatus.Available,
                Datasheet     = "data",
                ImageUrl      = "url",
                Image         = esimage,
                Manufacturer  = "Poul Ejnar A/S",
                WikiLink      = "wikilink",
                AdminComment  = "comment"
            };

            componenttype.Components.Add(component1);
            componenttype.Components.Add(component2);
            var category = new Category
            {
                Name = "Test"
            };

            var categoryComponentType = new CategoryComponentType
            {
                Category      = category,
                ComponentType = componenttype
            };

            context.CategoryComponentTypes.Add(categoryComponentType);
            context.SaveChanges();
        }