Пример #1
0
        public int Save(CategoryBinding Model, string UserID)
        {
            using (var _c = db)
            {
                Can(Model.SiteID, UserID, _c);
                var _Category = new Category();
                var _NewBros = _c.Categories.Where(m => m.CategoryParentID == Model.CategoryParentID && m.SiteID == Model.SiteID);
                if (Model.CategoryID == 0)
                {
                    //CREATING
                    _Category.SiteID = Model.SiteID;
                    _Category.Priority = _NewBros.Select(m => m.Priority).Any() ? _NewBros.Select(m => m.Priority).Max() + 1 : 0;
                    _Category.CategoryCultures = new List<CategoryCulture>();
                    //ADDING CULTURES
                    if (Model.CategoryCultures != null)
                    {
                        foreach (var item in Model.CategoryCultures)
                        {
                            _Category.CategoryCultures.Add(new CategoryCulture
                            {
                                CategoryName = item.CategoryName,
                                CultureID = item.CultureID,
                                FriendlyUrl = GetFriendlyUrl(Model, item.CategoryName, item.FriendlyUrl, item.CultureID, _c)
                            });
                        }
                    }
                    _c.Categories.Add(_Category);
                }
                else
                {
                    //UPDATING
                    _Category = GetByID(Model.CategoryID, _c);
                    //ADDING CULTURES
                    if (Model.CategoryCultures != null)
                    {
                        foreach (var item in Model.CategoryCultures)
                        {
                            if (item.Adding)
                            {
                                _Category.CategoryCultures.Add(new CategoryCulture
                                {
                                    CategoryName = item.CategoryName,
                                    CultureID = item.CultureID,
                                    FriendlyUrl = GetFriendlyUrl(Model, item.CategoryName, item.FriendlyUrl, item.CultureID, _c)
                                });
                            }
                            else
                            {
                                var _CC = _Category.CategoryCultures.Where(m => m.CultureID == item.CultureID).SingleOrDefault();
                                _CC.CategoryName = item.CategoryName;
                                _CC.FriendlyUrl = GetFriendlyUrl(Model, item.CategoryName, item.FriendlyUrl, item.CultureID, _c);
                            }
                        }
                    }
                    if (_Category.CategoryParentID != Model.CategoryParentID)
                    {
                        _Category.Priority = _NewBros.Select(m => m.Priority).Any() ? _NewBros.Select(m => m.Priority).Max() + 1 : 0;
                        //UPDATE PRIORITIES
                        var _Bros = _c.Categories.Where(m => m.CategoryParentID == _Category.CategoryParentID && m.SiteID == _Category.SiteID && m.Priority > _Category.Priority).ToList();
                        foreach (var item in _Bros)
                        {
                            item.Priority--;
                        }
                    }

                }
                _Category.CategoryAlias = Model.CategoryAlias;
                _Category.CategoryParentID = Model.CategoryParentID;
                _c.SaveChanges();
                return _Category.CategoryID;
            }
        }
Пример #2
0
 public IHttpActionResult PostSave(CategoryBinding Model)
 {
     return Ok(_CategoryService.Save(Model, User.Identity.GetUserId()));
 }
Пример #3
0
        private string GetFriendlyUrl(CategoryBinding Model, string CategoryName, string FriendlyUrl, int CultureID, ApplicationDbContext _c)
        {
            if (!string.IsNullOrEmpty(FriendlyUrl))
            {
                var _G = Model.CategoryCultures.GroupBy(m => m.FriendlyUrl);
                if (_G.Count() < Model.CategoryCultures.Count())
                    throw new RuleException("FriendlyURL Should be Different", "0x002");
            }

            FriendlyUrl = string.IsNullOrEmpty(FriendlyUrl) ? CategoryName.CleanUrl() : FriendlyUrl;
            if (Model.CategoryID != 0)
            {
                if (_c.CategoryCultures.Where(m => m.Category.SiteID == Model.SiteID
                                    && m.FriendlyUrl == FriendlyUrl
                                    && m.CategoryID != Model.CategoryID
                                    && m.CultureID != CultureID).Any())
                    throw new RuleException("FriendlyURL Exist", "0x001");
            }
            else
            {
                if (_c.CategoryCultures.Where(m => m.Category.SiteID == Model.SiteID
                                && m.FriendlyUrl == FriendlyUrl).Any())
                    throw new RuleException("FriendlyURL Exist", "0x001");
            };
            return FriendlyUrl;
        }
Пример #4
0
 private void SettingUp()
 {
     //INIT CULTURES
     _ProductCultures.Add(new ProductCultureBinding
     {
         CultureID = _Cultures[0].CultureID,
         ProductName = "lazos nazca para mujeres"
     });
     _ProductCultures.Add(new ProductCultureBinding
     {
         CultureID = _Cultures[1].CultureID,
         ProductName = "vows for girls"
     });
     //INIT CATEGORIES
     var _CategoryCultures = new List<CategoryCultureBinding>();
     _CategoryCultures.Add(new CategoryCultureBinding
     {
         Adding = true,
         CultureID = _Cultures[0].CultureID,
         CategoryName = "00" + _Cultures[0].Name + DateTime.Now.ToString(),
         FriendlyUrl = (Util.RandomString(10) + _Cultures[0].Name + DateTime.Now.ToString()).CleanUrl()
     });
     _CategoryCultures.Add(new CategoryCultureBinding
     {
         Adding = true,
         CultureID = _Cultures[1].CultureID,
         CategoryName = "00" + _Cultures[1].Name + DateTime.Now.ToString(),
         FriendlyUrl = (Util.RandomString(10) + _Cultures[1].Name + DateTime.Now.ToString()).CleanUrl()
     });
     _Category = new CategoryBinding
     {
         CategoryAlias = "00",
         SiteID = _SiteID,
         CategoryCultures = _CategoryCultures
     };
     _Category.CategoryID = new CategoryBLL().Save(_Category, _CustomerUser.Id);
     _ProductCategories.Add(new ProductCategoryBinding
     {
         CategoryID = _Category.CategoryID,
         Adding = true
     });
     //INIT CURRENCIES
     var _C1 = new CurrencyBLL().Save(new CurrencyBinding
     {
         CurrencyName = "COP",
         SiteID = _SiteID
     }, _CustomerUser.Id);
     var _C2 = new CurrencyBLL().Save(new CurrencyBinding
     {
         CurrencyName = "USD",
         SiteID = _SiteID
     }, _CustomerUser.Id);
     _ProductCurrencies.Add(new ProductCurrencyBinding
     {
         CurrencyID = _C1,
         Price = 2600
     });
     _ProductCurrencies.Add(new ProductCurrencyBinding
     {
         CurrencyID = _C2,
         Price = 1
     });
     //INIT PRODUCT
     _Product = new data.binding.ProductBinding
     {
         ProductAlias = "00",
         SiteID = _SiteID,
         ProductCategories = _ProductCategories,
         ProductCurrencies = _ProductCurrencies,
         ProductCultures = _ProductCultures
     };
     _Product.ProductID = new ProductBLL().Save(_Product, _CustomerUser.Id);
     Assert.IsNotNull(_Product.ProductID);
 }