public bool updateProduct(object tables) { try { DataTable dataTable = new DataTable(); dataTable.Clear(); dataTable.Columns.Add("Id", typeof(string)); dataTable.Columns.Add("nameProduct", typeof(string)); dataTable.Columns.Add("pathImage", typeof(Image)); dataTable.Columns.Add("priceProduct", typeof(decimal)); dataTable.Columns.Add("nameProductType", typeof(string)); dataTable.Columns.Add("nameUnit", typeof(string)); UnitService unitService = new UnitService(_context); ProductTypeService productTypeService = new ProductTypeService(_context); dataTable = tables as DataTable; int i = 0; foreach (DataRow item in dataTable.Rows) { Guid Id = new Guid(item.Table.Rows[i]["Id"].ToString()); string nameProduct = item.Table.Rows[i]["nameProduct"].ToString(); string price = item.Table.Rows[i]["priceProduct"].ToString(); string productTypeName = item.Table.Rows[i]["nameProductType"].ToString(); string unitname = item.Table.Rows[i]["nameUnit"].ToString(); Unit unit = unitService.GetName(unitname); ProductType productType = productTypeService.GetName(productTypeName); if (unit != null && productType != null) { var product = _context.Products.Where(x => x.Id == Id).Include(x => x.Unit).Include(x => x.ProductType).SingleOrDefault(); product.nameProduct = nameProduct; product.priceProduct = Convert.ToDecimal(price); product.ProductType = productType; product.Unit = unit; _context.SaveChanges(); } i++; } return(true); } catch (Exception) { } return(false); }
public bool Insert(string nameProduct, string price, string pathImage, string nameUnit, string nameProductType) { try { UnitService unitService = new UnitService(_context); ProductTypeService productTypeService = new ProductTypeService(_context); var unit = unitService.GetName(nameUnit); var productType = productTypeService.GetName(nameProductType); if (productType == null || unit == null) { return(false); } var userEx = _context.Products.Where(x => x.nameProduct == nameProduct).SingleOrDefault(); if (userEx != null) { return(false); } string path = saveImage.Instance.saveImagePath(pathImage); if (path.Equals("")) { return(false); } Product product = new Product { nameProduct = nameProduct, pathImage = path, priceProduct = Convert.ToDecimal(price), Unit = unit, ProductType = productType }; _context.Products.Add(product); _context.SaveChanges(); return(true); } catch (Exception) { } return(false); }