private ProductListBinding GetList(Product c, ApplicationDbContext _c) { return (new ProductListBinding { ProductID = c.ProductID, ProductAlias = c.ProductAlias, SiteID = c.SiteID, Enabled = c.Enabled, Featured = c.Featured, New = c.New, IconPath = c.ProductCultures.FirstOrDefault().IconPath, Priority = 0 }); }
public int Save(ProductBinding Model, string UserID) { using (var _c = db) { Can(Model.SiteID, UserID, _c); //VALIDATE IT HAS A CATEGORY //TO-DO VALIDATE IT HAS CATEGORIES BUT FOR DELETE if (Model.ProductCategories == null || Model.ProductCategories.Count == 0) throw new RuleException("Categories Empty", "0x000"); var _Product = new Product(); if (Model.ProductID == 0) { //CREATING _Product.SiteID = Model.SiteID; _Product.ProductCultures = new List<ProductCulture>(); _Product.ProductCategories = new List<ProductCategory>(); _Product.ProductCurrencies = new List<ProductCurrency>(); _Product.ProductTags = new List<ProductTag>(); _c.Products.Add(_Product); } else { //UPDATING _Product = GetByID(Model.ProductID, _c); } _Product.ProductAlias = Model.ProductAlias != null ? Model.ProductAlias : (Model.ProductCultures.Count > 0 ? Model.ProductCultures[0].ProductName.CleanUrl() : null); _Product.Enabled = true; //ADDING CULTURES if (Model.ProductCultures != null) { foreach (var item in Model.ProductCultures) { //GENERATE FRIENDLYURL if (string.IsNullOrEmpty(item.FriendlyUrl)) item.FriendlyUrl = item.ProductName.CleanUrl(); if (Model.ProductID != 0) { if (_c.ProductCultures.Where(m => m.Product.SiteID == Model.SiteID && m.FriendlyUrl == item.FriendlyUrl && m.ProductID != Model.ProductID && m.CultureID != item.CultureID).Any()) throw new RuleException("FriendlyURL Exist", "0x001"); if (!item.Adding) { var _CC = _Product.ProductCultures.Where(m => m.CultureID == item.CultureID).SingleOrDefault(); _CC.ProductName = item.ProductName; _CC.Description = item.Description; _CC.AdditionalInformation = item.AdditionalInformation; _CC.IconPath = item.IconPath; _CC.FriendlyUrl = item.FriendlyUrl; } } else { if (_c.ProductCultures.Where(m => m.Product.SiteID == Model.SiteID && m.FriendlyUrl == item.FriendlyUrl).Any()) throw new RuleException("FriendlyURL Exist", "0x001"); } if (Model.ProductID == 0 || item.Adding) { _Product.ProductCultures.Add(new ProductCulture { ProductName = item.ProductName, CultureID = item.CultureID, Description = item.Description, AdditionalInformation = item.AdditionalInformation, IconPath = item.IconPath, FriendlyUrl = item.FriendlyUrl }); } } } var _G = Model.ProductCultures.GroupBy(m => m.FriendlyUrl); if (_G.Count() < Model.ProductCultures.Count) throw new RuleException("FriendlyURL Should be Different", "0x002"); //CURRENCIES if (Model.ProductCurrencies != null) { foreach (var item in Model.ProductCurrencies) { if (Model.ProductID != 0) { if (!item.Adding) { var _CC = _Product.ProductCurrencies.Where(m => m.CurrencyID == item.CurrencyID).SingleOrDefault(); _CC.Price = item.Price; _CC.PriceOld = item.PriceOld; } } if (Model.ProductID == 0 || item.Adding) { _Product.ProductCurrencies.Add(new ProductCurrency { CurrencyID = item.CurrencyID, Price = item.Price, PriceOld = item.PriceOld }); } } } //ADDING CATEGORIES foreach (var item in Model.ProductCategories) { ProductCategory _PC = null; if (Model.ProductID != 0) _PC = _c.ProductCategories.Where(m => m.CategoryID == item.CategoryID && m.ProductID == Model.ProductID).SingleOrDefault(); if (item.Adding || Model.ProductID == 0) { if (_PC == null) _Product.ProductCategories.Add(new ProductCategory { CategoryID = item.CategoryID, Priority = _c.ProductCategories.Where(m => m.CategoryID == item.CategoryID).Any() ? _c.ProductCategories.Where(m => m.CategoryID == item.CategoryID).Select(m => m.Priority).Max() + 1 : 0 }); } else if (item.Deleting) { if (_PC != null) _Product.ProductCategories.Remove(_PC); } } //ADDING TAGS if (Model.ProductTags != null) { foreach (var item in Model.ProductTags) { ProductTag _PT = null; if (Model.ProductID != 0) _PT = _c.ProductTags.Where(m => m.TagID == item.TagID && m.ProductID == Model.ProductID).SingleOrDefault(); if (item.Adding || Model.ProductID == 0) { if (_PT == null) _Product.ProductTags.Add(new ProductTag { TagID = item.TagID }); } else if (item.Deleting) { if (_PT != null) _Product.ProductTags.Remove(_PT); } } } _c.SaveChanges(); //ADDING ICON if (Model.ProductCultures != null) { foreach (var item in Model.ProductCultures) { if (item.IconFile != null) { var _PC = _c.ProductCultures.Where(m => m.ProductID == _Product.ProductID && m.CultureID == item.CultureID).SingleOrDefault(); _PC.IconPath = SaveIcon(_Product.ProductID, item.CultureID, item.IconFile); } } _c.SaveChanges(); } return _Product.ProductID; } }
private ProductBinding Get(Product c, ApplicationDbContext _c) { return (new ProductBinding { ProductID = c.ProductID, ProductAlias = c.ProductAlias, SiteID = c.SiteID, Enabled = c.Enabled, Featured = c.Featured, New = c.New, ProductCultures = c.ProductCultures.Select(m => new ProductCultureBinding { ProductName = m.ProductName, CultureID = m.CultureID, CultureName = m.Culture.Name, Description = m.Description, AdditionalInformation = m.AdditionalInformation, IconPath = m.IconPath, FriendlyUrl = m.FriendlyUrl }).ToList(), ProductTags = c.ProductTags.Select(m => new ProductTagBinding { ProductID = m.ProductID, TagID = m.TagID, TagName = m.Tag.TagName }).ToList(), ProductCategories = c.ProductCategories.Select(m => new ProductCategoryBinding { ProductID = m.ProductID, CategoryID = m.CategoryID, CategoryAlias = m.Category.CategoryAlias }).ToList(), ProductGalleries = c.ProductGalleries.Select(m => new ProductGalleryBinding { ProductID = m.ProductID, ProductGalleryID = m.ProductGalleryID, PhotoPath = m.PhotoPath }).ToList(), ProductCurrencies = c.ProductCurrencies.Select(m => new ProductCurrencyBinding { CurrencyID = m.CurrencyID, CurrencyName = m.Currency.CurrencyName, ProductID = m.ProductID, Price = m.Price, PriceOld = m.PriceOld }).ToList() }); }