public void TestOrderProductPrice() { OrderLogic logicO = new OrderLogic(); ProductLogic logicP = new ProductLogic(); try { ProductBinding product = new ProductBinding { Name = "Test", Price = 20 }; OrderBinding order = new OrderBinding { OrderProducts = new List <OrderProductBinding>() }; order.OrderProducts.Add(new OrderProductBinding { ProductId = 1, Count = 2 }); logicP.Create(product); logicO.Create(order); List <OrderView> list = logicO.Read(null); Assert.Equal(20, list[0].OrderProducts[0].Price); } finally { logicO.Delete(null); logicP.Delete(null); } }
public void TestUpdate() { ProductLogic logic = new ProductLogic(); try { ProductBinding model1 = new ProductBinding { Name = "Test1", Price = 10 }; logic.Create(model1); ProductBinding model2 = new ProductBinding { Id = 1, Name = "Test2", Price = 20 }; logic.Update(model2); List <ProductView> list = logic.Read(null); Assert.Single(list); Assert.Equal(1, list[0].Id); Assert.Equal("Test2", list[0].Name); Assert.Equal(20, list[0].Price); } finally { logic.Delete(null); } }
public List <ProductView> Read(ProductBinding model) { return (context.Products .Where(rec => model == null || rec.Id == model.Id) .Select(rec => MapProductView(rec)) .ToList()); }
private Product MapProduct(ProductBinding model) { return (new Product { Name = model.Name, Price = model.Price }); }
public void TestProductBinding() { ProductBinding pb = new ProductBinding { Id = 0, Name = "Test", Price = 100 }; Assert.Equal(0, pb.Id); Assert.Equal("Test", pb.Name); Assert.Equal(100, pb.Price); }
public void Update(ProductBinding model) { Product product = context.Products.FirstOrDefault(rec => rec.Id == model.Id); if (product != null) { product.Name = model.Name; product.Price = model.Price; } }
public void Create(ProductBinding model) { Product product = MapProduct(model); if (context.Products.Count(rec => rec.Name == model.Name) > 0) { throw new Exception("Product with name " + model.Name + " already exist"); } product.Id = context.Products.Count > 0 ? context.Products.Max(rec => rec.Id) + 1 : 1; context.Products.Add(product); }
public bool SaveProduct() { try { ProductBinding model = new ProductBinding { Id = product.Id, Name = ProductName(), Price = ProductPrice() }; if (string.IsNullOrWhiteSpace(model.Name)) { throw new Exception("Field name is empty"); } if (model.Price <= 0) { throw new Exception("Incorrect price"); } if (product.Id < 0) { context.ProductLogic.Create(model); ShowInfoMessage("Product was created"); return(true); } else { context.ProductLogic.Update(model); ShowInfoMessage("Product №" + model.Id + " was updated"); return(true); } } catch (Exception ex) { ShowErrorMessage(ex.Message); return(false); } }
public void TestDelete() { ProductLogic logic = new ProductLogic(); try { ProductBinding model = new ProductBinding { Name = "Test", Price = 10 }; logic.Create(model); logic.Delete(null); List <ProductView> list = logic.Read(null); Assert.Empty(list); } finally { logic.Delete(null); } }
public void TestCreate() { ProductLogic logic = new ProductLogic(); try { ProductBinding model = new ProductBinding { Name = "Test", Price = 10 }; logic.Create(model); List <ProductView> list = logic.Read(null); Assert.Single(list); Assert.Equal(1, list[0].Id); Assert.Equal("Test", list[0].Name); Assert.Equal(10, list[0].Price); } finally { logic.Delete(null); } }
public void TestCreateSameName() { ProductLogic logic = new ProductLogic(); string message = ""; try { ProductBinding model1 = new ProductBinding { Name = "Test", Price = 10 }; logic.Create(model1); try { ProductBinding model2 = new ProductBinding { Name = "Test", Price = 10 }; logic.Create(model2); } catch (Exception ex) { message = ex.Message; } List <ProductView> list = logic.Read(null); Assert.Single(list); Assert.Equal(1, list[0].Id); Assert.Equal("Test", list[0].Name); Assert.Equal(10, list[0].Price); Assert.Equal("Product with name Test already exist", message); } finally { logic.Delete(null); } }
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); }
public IHttpActionResult PostSave(ProductBinding Model) { return(Ok(_ProductService.Save(Model, User.Identity.GetUserId()))); }
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); } }
public void Delete(ProductBinding model) { context.Products.RemoveAll(rec => model == null || rec.Id == model.Id); }