Пример #1
0
        public IActionResult Update(CategoriesVariantModel model)
        {
            try
            {
                var optionsData = model.AllOptions.Split(',');
                List <VariantOption> options = new List <VariantOption>();

                var data = db.CategoryVariants.Where(c => c.Id == model.Id).Include(v => v.ProductCategory).Include(c => c.Variant).Include(c => c.Variant.VariantOptions).FirstOrDefault();
                if (data != null)
                {
                    data.ProductCategoryId = model.CategoryId;
                    data.IsSearchOption    = model.IsSearchOption;
                    data.IsMain            = model.IsMain;

                    if (data.Variant != null && data.Variant.Id == model.VariantId)
                    {
                        data.Variant.Name     = model.Name;
                        data.Variant.IsActive = true;
                        db.SaveChanges();

                        if (data.Variant.VariantOptions.Count > 0)
                        {
                            foreach (var item in data.Variant.VariantOptions)
                            {
                                item.IsActive = false;
                            }
                        }

                        if (optionsData.Length > 0)
                        {
                            foreach (var item in optionsData)
                            {
                                VariantOption vop = new VariantOption();
                                if (item != "")
                                {
                                    vop.Name      = item;
                                    vop.IsActive  = true;
                                    vop.VariantId = data.Variant.Id;
                                    options.Add(vop);
                                }
                            }
                            db.VariantOptions.AddRange(options);
                        }
                    }

                    data.ProductCategoryId = model.CategoryId;
                    db.SaveChanges();
                    return(Ok(true));
                }
                else
                {
                    return(Ok(false));
                }
            }
            catch (Exception ex)
            {
                return(Ok(ex));
            }
        }
Пример #2
0
        public IActionResult Add(CategoriesVariantModel model)
        {
            try
            {
                if (model != null)
                {
                    var data = db.CategoryVariants.Where(v => v.IsActive == true && v.ProductCategoryId == model.CategoryId).Include(v => v.Variant).ToList();
                    if (data.Any(v => v.Variant.Name.ToLower() == model.Name.ToLower()))
                    {
                        return(Ok("exist"));
                    }

                    //--getting data to string array
                    var optionsData = model.AllOptions.Split(',');
                    List <VariantOption> options = new List <VariantOption>();
                    Variant vari = new Variant();
                    vari.Name     = model.Name;
                    vari.IsActive = true;
                    vari.IsMain   = model.IsMain;
                    db.Variants.Add(vari);
                    db.SaveChanges();
                    if (optionsData.Length > 0)
                    {
                        foreach (var item in optionsData)
                        {
                            VariantOption vop = new VariantOption();
                            if (item != "")
                            {
                                vop.Name      = item;
                                vop.IsActive  = true;
                                vop.VariantId = vari.Id;
                                options.Add(vop);
                            }
                        }
                    }
                    db.VariantOptions.AddRange(options);

                    if (model.CategoryId > 0)
                    {
                        var catData = new CategoryVariant();
                        catData.ProductCategoryId = model.CategoryId;
                        catData.VariantId         = vari.Id;
                        catData.IsActive          = true;
                        catData.IsSearchOption    = model.IsSearchOption;
                        catData.IsMain            = model.IsMain;
                        db.CategoryVariants.Add(catData);
                    }
                    db.SaveChanges();
                }
                return(Ok(true));
            }
            catch (Exception ex)
            {
                return(Ok(ex));
            }
        }