public ProductGroup AddNewProductGroup(int parentId, string title, List <int> brandIds, List <int> featureIds)
        {
            var productGroup = new ProductGroup();

            var user = GetCurrentUser();

            productGroup.InsertDate = DateTime.Now;
            productGroup.InsertUser = user.UserName;

            #region Adding Product Group
            productGroup.Title = title;
            if (parentId != 0)
            {
                productGroup.ParentId = parentId;
            }
            _context.ProductGroups.Add(productGroup);
            _context.SaveChanges();
            _logger.LogEvent(productGroup.GetType().Name, productGroup.Id, "Add");
            #endregion

            #region Adding Product Group Brands

            foreach (var brandId in brandIds)
            {
                var productGroupBrand = new ProductGroupBrand();
                productGroupBrand.ProductGroupId = productGroup.Id;
                productGroupBrand.BrandId        = brandId;
                _context.ProductGroupBrands.Add(productGroupBrand);
            }
            _context.SaveChanges();

            #endregion
            #region Adding product Group Features
            foreach (var featureId in featureIds)
            {
                var productGroupFeature = new ProductGroupFeature();
                productGroupFeature.ProductGroupId = productGroup.Id;
                productGroupFeature.FeatureId      = featureId;
                _context.ProductGroupFeatures.Add(productGroupFeature);
            }

            _context.SaveChanges();
            #endregion
            return(productGroup);
        }
        public int?Create(NewProductGroupViewModel newProductGroup)
        {
            if (ModelState.IsValid)
            {
                var productGroup = new ProductGroup();

                #region Adding Product Group
                productGroup.GroupName = newProductGroup.GroupName;
                if (newProductGroup.ParentGroupId != 0)
                {
                    productGroup.ParentId = newProductGroup.ParentGroupId;
                }
                db.ProductGroups.Add(productGroup);
                db.SaveChanges();
                #endregion

                #region Adding Product Group Brands

                foreach (var brandId in newProductGroup.BrandIds)
                {
                    var productGroupBrand = new ProductGroupBrand();
                    productGroupBrand.ProductGroupId = productGroup.Id;
                    productGroupBrand.BrandId        = brandId;
                    db.ProductGroupBrands.Add(productGroupBrand);
                }
                db.SaveChanges();

                #endregion
                #region Adding product Group Features
                foreach (var featureId in newProductGroup.ProductGroupFeatureIds)
                {
                    var productGroupFeature = new ProductGroupFeature();
                    productGroupFeature.ProductGroupId = productGroup.Id;
                    productGroupFeature.FeatureId      = featureId;
                    db.ProductGroupFeatures.Add(productGroupFeature);
                }

                db.SaveChanges();
                #endregion
                return(productGroup.Id);
            }

            return(null);
        }
        public int?Edit(UpdateProductGroupViewModel newProductGroup)
        {
            if (ModelState.IsValid)
            {
                var productGroup = db.ProductGroups.Find(newProductGroup.Id);
                #region Adding Product Group
                productGroup.GroupName = newProductGroup.GroupName;
                if (newProductGroup.ParentGroupId != 0)
                {
                    productGroup.ParentId = newProductGroup.ParentGroupId;
                }
                else
                {
                    productGroup.ParentId = null;
                }

                db.Entry(productGroup).State = EntityState.Modified;
                db.SaveChanges();
                #endregion

                #region Product Group Brands

                // Removing Previous Group Brands
                var productGroupBrands = db.ProductGroupBrands
                                         .Where(b => b.ProductGroupId == productGroup.Id).ToList();
                foreach (var item in productGroupBrands)
                {
                    db.ProductGroupBrands.Remove(item);
                }

                db.SaveChanges();
                // Adding new Group Brands
                foreach (var brandId in newProductGroup.BrandIds)
                {
                    var productGroupBrand = new ProductGroupBrand();
                    productGroupBrand.ProductGroupId = productGroup.Id;
                    productGroupBrand.BrandId        = brandId;
                    db.ProductGroupBrands.Add(productGroupBrand);
                }
                db.SaveChanges();

                #endregion
                #region product Group Features
                var productGroupFeatures = db.ProductGroupFeatures
                                           .Where(b => b.ProductGroupId == productGroup.Id).ToList();

                // Removing Previous Group Features
                foreach (var item in productGroupFeatures)
                {
                    db.ProductGroupFeatures.Remove(item);
                }

                db.SaveChanges();

                // Adding New Group Features
                foreach (var featureId in newProductGroup.ProductGroupFeatureIds)
                {
                    var productGroupFeature = new ProductGroupFeature();
                    productGroupFeature.ProductGroupId = productGroup.Id;
                    productGroupFeature.FeatureId      = featureId;
                    db.ProductGroupFeatures.Add(productGroupFeature);
                }
                db.SaveChanges();
                #endregion
                return(productGroup.Id);
            }

            return(null);
        }
        public ProductGroup UpdateProductGroup(int parentId, int productGroupId, string title, List <int> brandIds, List <int> featureIds)
        {
            var productGroup = Get(productGroupId);
            var user         = GetCurrentUser();

            productGroup.UpdateDate = DateTime.Now;
            productGroup.UpdateUser = user.UserName;

            #region Adding Product Group
            productGroup.Title = title;
            if (parentId != 0)
            {
                productGroup.ParentId = parentId;
            }
            else
            {
                productGroup.ParentId = null;
            }
            Update(productGroup);
            _logger.LogEvent(productGroup.GetType().Name, productGroup.Id, "Update");
            #endregion

            #region Product Group Brands

            // Removing Previous Group Brands
            var productGroupBrands = _context.ProductGroupBrands
                                     .Where(b => b.IsDeleted == false & b.ProductGroupId == productGroup.Id).ToList();
            foreach (var item in productGroupBrands)
            {
                item.IsDeleted             = true;
                _context.Entry(item).State = EntityState.Modified;
            }
            _context.SaveChanges();
            // Adding new Group Brands
            foreach (var brandId in brandIds)
            {
                var productGroupBrand = new ProductGroupBrand();
                productGroupBrand.ProductGroupId = productGroup.Id;
                productGroupBrand.BrandId        = brandId;
                _context.ProductGroupBrands.Add(productGroupBrand);
            }
            _context.SaveChanges();

            #endregion
            #region product Group Features
            var productGroupFeatures = _context.ProductGroupFeatures
                                       .Where(b => b.IsDeleted == false & b.ProductGroupId == productGroup.Id).ToList();
            // Removing Previous Group Features
            foreach (var item in productGroupFeatures)
            {
                item.IsDeleted             = true;
                _context.Entry(item).State = EntityState.Modified;
            }
            _context.SaveChanges();
            // Adding New Group Features
            foreach (var featureId in featureIds)
            {
                var productGroupFeature = new ProductGroupFeature();
                productGroupFeature.ProductGroupId = productGroup.Id;
                productGroupFeature.FeatureId      = featureId;
                _context.ProductGroupFeatures.Add(productGroupFeature);
            }
            _context.SaveChanges();
            #endregion
            return(productGroup);
        }