public ActionResult Edit(int id) { try { this._productsService.GetById(id); } catch (Exception) { this.RedirectToAction("Index", "Products"); } List <Category> categories = this._categoryService.GetAll().ToList(); List <SelectListItem> categoriesListItems = new List <SelectListItem>(); foreach (var category in categories) { SelectListItem item = new SelectListItem() { Text = category.Name, Value = category.Id.ToString() }; categoriesListItems.Add(item); } ViewBag.Categories = categoriesListItems; Product product = this._productsService.GetById(id); ProductBM bindModel = AutoMapper.Mapper.Map <Product, ProductBM>(product); this.ViewBag.ItemId = id; return(this.View(bindModel)); }
public async Task <EmptyResultBM <UpdateProductExplanation> > UpdateProductAsync(UpdateProductRequestBM updateRequest) { ProductBM product = await productDataManager.GetProductAsync(updateRequest.Barcode); if (product == null) { return(new EmptyResultBM <UpdateProductExplanation>(UpdateProductExplanation.ProductNotFound)); } //apply product name changes string[] productNamesToRemove = updateRequest.ProductName.Where(x => string.IsNullOrWhiteSpace(x.Value)).Select(x => x.Key).ToArray(); //update or keep already specified languages, by ignoring those ones that need to be removed Dictionary <string, string> newProductNames = new Dictionary <string, string>(); foreach (KeyValuePair <string, string> productNameEntry in product.ProductName.Where(x => !productNamesToRemove.Contains(x.Key))) { if (updateRequest.ProductName.ContainsKey(productNameEntry.Key)) { newProductNames.Add(productNameEntry.Key, updateRequest.ProductName[productNameEntry.Key]); } else { newProductNames.Add(productNameEntry.Key, product.ProductName[productNameEntry.Key]); } } //add new languages foreach (var item in updateRequest.ProductName.Where(x => !productNamesToRemove.Contains(x.Key) && !newProductNames.Keys.Contains(x.Key))) { newProductNames.Add(item.Key, item.Value); } //perform update ProductBM updatedProduct = new ProductBM( barcode: updateRequest.Barcode, manufacturerId: updateRequest.NewManufacturerId, categoryId: updateRequest.NewCategoryId, productName: newProductNames ); if (!await productDataManager.UpdateProductAsync(updatedProduct)) { return(new EmptyResultBM <UpdateProductExplanation>(UpdateProductExplanation.DatabaseError)); } return(new EmptyResultBM <UpdateProductExplanation>()); }
public void Add(ProductBM model) { Product element = context.Products.FirstOrDefault(rec => rec.Name == model.Name); if (element != null) { throw new Exception("Уже есть"); } context.Products.Add(new Product { Name = model.Name, CategoryId = model.CategoryId, Count = model.Count, }); context.SaveChanges(); }
public ActionResult AddProduct(ProductBM product) { if (!ModelState.IsValid) { return(View(product)); } Product newProduct = new Product(); newProduct = AutoMapper.Mapper.Map <ProductBM, Product>(product); newProduct.Date = DateTime.Now; newProduct.CategoryId = int.Parse(product.Category); this._productsService.AddOrUpdate(newProduct); return(RedirectToAction("Index", "AdminPanel")); }
public List <ProductVM> Read(ProductBM model) { using (var context = new DatabaseContext()) { return(context.Products .Where(rec => model == null || rec.Id == model.Id || (rec.Name == model.Name && rec.Cena == model.Cena)) .Select(rec => new ProductVM { Id = rec.Id, Name = rec.Name, Cena = rec.Cena }) .ToList()); } }
public async Task <bool> UpdateProductAsync(ProductBM product) { Product productModel; try { productModel = await dbContext.Product.Include(x => x.ProductName).SingleAsync(x => x.Barcode == product.Barcode.Value); } catch (Exception) { return(false); } productModel.CategoryId = product.CategoryId; productModel.ManufacturerId = product.ManufacturerId; productModel.ProductName.RemoveAll(x => !product.ProductName.Keys.Contains(x.LanguageCode)); foreach (var item in product.ProductName) { ProductName productNameModel = productModel.ProductName.FirstOrDefault(x => x.LanguageCode == item.Key); if (productNameModel == null) { productModel.ProductName.Add( new ProductName() { Barcode = product.Barcode, LanguageCode = item.Key, LocalizedName = item.Value } ); } else { productNameModel.LocalizedName = item.Value; } } try { await dbContext.SaveChangesAsync(); } catch (Exception) { return(false); } return(true); }
public void Update(ProductBM model) { Product element = context.Products.FirstOrDefault(rec => rec.Name == model.Name && rec.Id != model.Id); if (element != null) { throw new Exception("Уже есть"); } element = context.Products.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } element.Name = model.Name; element.CategoryId = model.CategoryId; element.Count = model.Count; context.SaveChanges(); }
public ActionResult Edit(ProductBM product) { if (!ModelState.IsValid) { return(View(product)); } Product editedProduct = this._productsService.GetById(product.Id); editedProduct.Name = product.Name; editedProduct.Description = product.Description; editedProduct.Price = product.Price; editedProduct.CategoryId = int.Parse(product.Category); editedProduct.Category = null; this._productsService.AddOrUpdate(editedProduct); return(RedirectToAction("Index", "Products")); }
public void CreateProduct(ProductBM model) { Product element = context.Products.FirstOrDefault(rec => rec.Name == model.Name || rec.Number == model.Number); if (element != null) { throw new Exception("Уже есть product с таким name or number"); } context.Products.Add(new Product { Name = model.Name, Price = model.Price, ProdGroupId = model.ProdGroupId, Mark = model.Mark, AdvInf = model.AdvInf, Producer = model.Producer, Provider = model.Provider, Number = model.Number }); context.SaveChanges(); }
public void CreateOrUpdate(ProductBM model) { using (var context = new DatabaseContext()) { Product element = model.Id.HasValue ? null : new Product(); if (model.Id.HasValue) { element = context.Products.FirstOrDefault(rec => rec.Id == model.Id); if (element == null) { throw new Exception("Элемент не найден"); } } else { element = new Product(); context.Products.Add(element); } element.Name = model.Name; element.Cena = model.Cena; context.SaveChanges(); } }
public void UpdProduct(ProductBM model) { throw new NotImplementedException(); }