Exemplo n.º 1
0
        public void CreatePreDefinedOption(MainPageViewModel vm, int NewId)
        {
            if (vm.Id == 0 && NewId != 0)
            {
                var customName   = vm.AttributeName.Split("%");
                var options      = customName[1];
                var optionValues = options.Split("|");

                foreach (var option in optionValues)
                {
                    var newOption = new PreDifinedOptions
                    {
                        Name = option,
                        ProductAttributeId = NewId
                    };
                    ctx.preDifinedOptions.Add(newOption);
                    ctx.SaveChanges();
                }
            }
        }
        public void CreateProductAttribute(MainPageViewModel pa, int AttrGroupId)
        {
            var ctxAttrGroups = ctx.AttributeGroups.Include(x => x.ProductAttributes);
            var AttrGroup     = ctxAttrGroups.FirstOrDefault(x => x.Id.Equals(AttrGroupId));

            if (pa.Id == 0 && AttrGroup.ProductAttributes.Count() == 0)
            {
                var attributeGroup = pa.AttributeGroupName.Split("%");

                var attributes = attributeGroup[1];

                var attributeNames = attributes.Split("|");

                foreach (var an in attributeNames)
                {
                    var NameType = an.Split("=");
                    var AttrName = NameType[0].ToString();
                    var AttrType = NameType[1].ToString();

                    // If attribute is custom
                    if (int.TryParse(AttrType, out int AttrTypeIsInt))
                    {
                        ProductAttribute thisAttr = ctx.ProductAttributes.Find(AttrTypeIsInt);

                        // If database contain this attribute use that
                        if (thisAttr.AttributeGroupId == null)
                        {
                            thisAttr.AttributeGroupId = AttrGroupId;
                            ctx.SaveChanges();
                        }
                        else
                        {
                            // Get all custom options with attributeId == thisAttr.Id
                            var ctxCustomOptions = ctx.preDifinedOptions.Where(x => x.ProductAttributeId == thisAttr.Id);

                            var newCustomAttr = new ProductAttribute
                            {
                                Name        = thisAttr.Name,
                                Description = thisAttr.Description
                            };
                            ctx.Add(newCustomAttr);

                            foreach (var option in ctxCustomOptions)
                            {
                                var newOption = new PreDifinedOptions
                                {
                                    Name = option.Name,
                                    ProductAttributeId = newCustomAttr.Id
                                };
                                ctx.Add(newOption);
                                // Patrik, varför kan den inte köras igen?
                            }
                        }
                        //ctx.ProductAttributes.Add(newProdAttr);
                        ctx.SaveChanges();
                    }
                    else
                    {
                        var newProdAttr = new ProductAttribute
                        {
                            Name             = AttrName,
                            Type             = AttrType,
                            AttributeGroupId = AttrGroupId
                        };
                        ctx.ProductAttributes.Add(newProdAttr);

                        ctx.SaveChanges();
                    }
                }
            }
        }