示例#1
0
        // GET: ComponentTypes/Edit/5
        public async Task <IActionResult> Edit(long?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var componentType = await _context.ComponentTypes
                                .Include(m => m.CategoryComponenttypebindings)
                                .ThenInclude(m => m.Category)
                                .SingleOrDefaultAsync(m => m.ComponentTypeId == id);

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

            var tmp = new ComponenttypeVM();

            tmp.AdminComment    = componentType.AdminComment;
            tmp.ComponentInfo   = componentType.ComponentInfo;
            tmp.ComponentName   = componentType.ComponentName;
            tmp.ComponentTypeId = componentType.ComponentTypeId;
            tmp.Datasheet       = componentType.Datasheet;
            tmp.ImageUrl        = componentType.ImageUrl;
            tmp.Manufacturer    = componentType.Manufacturer;
            tmp.Status          = componentType.Status;
            tmp.WikiLink        = componentType.WikiLink;
            tmp.Location        = componentType.Location;

            string cats = "";

            if (componentType.CategoryComponenttypebindings.Count > 0)
            {
                foreach (var item in componentType.CategoryComponenttypebindings)
                {
                    cats += item.Category.Name + ",";
                }

                cats = cats.Remove(cats.Length - 1);
            }
            tmp.Categories = cats;

            return(View(tmp));
        }
示例#2
0
        public async Task <IActionResult> Create(ComponenttypeVM componentType)
        {
            //todo rename kjal
            var kjal = new ESImage();

            if (componentType.Image != null && componentType.Image.Length > 0)
            {
                var fileName = ContentDispositionHeaderValue.Parse(componentType.Image.ContentDisposition).FileName.Trim('"');

                using (var reader = new StreamReader(componentType.Image.OpenReadStream()))
                {
                    string contentAsString = reader.ReadToEnd();
                    byte[] bytes           = new byte[contentAsString.Length * sizeof(char)];
                    System.Buffer.BlockCopy(contentAsString.ToCharArray(), 0, bytes, 0, bytes.Length);

                    kjal.ImageData = bytes;
                }
            }

            kjal.ImageMimeType = "image/png";



            var tmp = new ComponentType();

            tmp.ComponentName = componentType.ComponentName;
            tmp.AdminComment  = componentType.AdminComment;
            tmp.Datasheet     = componentType.Datasheet;
            tmp.ComponentInfo = componentType.ComponentInfo;
            tmp.ImageUrl      = componentType.ImageUrl;
            tmp.Location      = componentType.Location;
            tmp.Status        = componentType.Status;
            tmp.Manufacturer  = componentType.Manufacturer;
            tmp.WikiLink      = componentType.WikiLink;
            tmp.Image         = kjal;

            _context.Add(tmp);
            await _context.SaveChangesAsync();

            //long Id = tmp.ComponentTypeId;

            ICollection <Category> categories = new List <Category>();

            List <Category> cool = new List <Category>();

            if (componentType.Categories != null)
            {
                var catlist = componentType.Categories.Split(",");

                foreach (var item in catlist)
                {
                    cool = _context.Categories.Select(a => a).Where(a => a.Name == item).ToList();

                    if (!cool.Any())
                    {
                        var aCat = new Category
                        {
                            Name = item,
                        };

                        categories.Add(aCat);
                        _context.Add(aCat);
                    }
                    else
                    {
                        //TODO lol :) hacks
                        categories.Add(cool.First());
                    }
                }
            }
            await _context.SaveChangesAsync();

            foreach (var item in categories)
            {
                var free = new CategoryComponenttypebinding()
                {
                    CategoryId      = item.CategoryId,
                    ComponentTypeId = tmp.ComponentTypeId,
                    Category        = item,
                    ComponentType   = tmp
                };

                _context.Add(free);

                tmp.CategoryComponenttypebindings.Add(free);
                item.CategoryComponenttypebindings.Add(free);

                //TODO check if it works without
                _context.Entry(item).State = EntityState.Modified;
                _context.Update(item);
            }

            _context.Entry(tmp).Collection("CategoryComponenttypebindings").IsModified = true;


            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
示例#3
0
        public async Task <IActionResult> Edit(long id, ComponenttypeVM componentType)
        {
            if (id != componentType.ComponentTypeId)
            {
                return(NotFound());
            }


            if (ModelState.IsValid)
            {
                var tmp = _context.ComponentTypes
                          .Include(a => a.CategoryComponenttypebindings)
                          .SingleOrDefault(m => m.ComponentTypeId == id);

                tmp.ComponentTypeId = componentType.ComponentTypeId;
                tmp.ComponentName   = componentType.ComponentName;
                tmp.AdminComment    = componentType.AdminComment;
                tmp.Datasheet       = componentType.Datasheet;
                tmp.ComponentInfo   = componentType.ComponentInfo;
                tmp.ImageUrl        = componentType.ImageUrl;
                tmp.Location        = componentType.Location;
                tmp.Status          = componentType.Status;
                tmp.Manufacturer    = componentType.Manufacturer;
                tmp.WikiLink        = componentType.WikiLink;

                ICollection <Category> categories = new List <Category>();
                List <Category>        cool       = new List <Category>();

                if (componentType.Categories != null)
                {
                    var catlist = componentType.Categories.Split(",");



                    foreach (var item in catlist)
                    {
                        cool = _context.Categories.Select(a => a).Where(a => a.Name == item).ToList();

                        if (!cool.Any())
                        {
                            var aCat = new Category
                            {
                                Name = item,
                            };

                            categories.Add(aCat);
                            _context.Add(aCat);
                        }
                        else
                        {
                            //TODO lol :) hacks
                            categories.Add(cool.First());
                        }
                    }

                    await _context.SaveChangesAsync();
                }

                tmp.CategoryComponenttypebindings.Clear();
                await _context.SaveChangesAsync();

                foreach (var item in categories)
                {
                    var free = new CategoryComponenttypebinding()
                    {
                        CategoryId      = item.CategoryId,
                        ComponentTypeId = tmp.ComponentTypeId,
                        Category        = item,
                        ComponentType   = tmp
                    };

                    _context.Add(free);

                    tmp.CategoryComponenttypebindings.Add(free);
                    item.CategoryComponenttypebindings.Add(free);
                }
                try
                {
                    _context.Update(tmp);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ComponentTypeExists(tmp.ComponentTypeId))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(componentType));
        }