Example #1
0
        public bool CreateProduct(List<int> categoryIds,int? brandId,List<int> colorIds, IEnumerable<HttpPostedFileBase> files, string code, string name, bool isNew, decimal? price, string shortDescription, string detailDescription)
        {
            var product = new Product();
            product.ProductCode = code;
            product.ProductName = name;
            product.Price = price;
            product.BrandId = brandId;
            product.ProductShortDescription = shortDescription;
            product.ProductDetailDescription = detailDescription;
            product.CreatedDate = DateTime.Now;
            product.UpdatedDate = DateTime.Now;
            product.IsNew = isNew;
            if(files==null)
            {
                files=new List<HttpPostedFileBase>();
            }
            foreach (HttpPostedFileBase file in files)
            {
                var fName = file.FileName;
                var fNameIndex = fName.LastIndexOf('.');
                fName = fName.Insert(fNameIndex, "_" + DateTime.Now.Ticks.ToString());

                var folderPath = HttpContext.Current.Server.MapPath("~/images/product");
                string filePath = Path.Combine(folderPath, fName);
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                System.IO.File.WriteAllBytes(filePath, this.ReadData(file.InputStream));
                var productImage = new ProductImage();
                productImage.ProductImageUrl = "~/images/product/" + fName;
                product.ProductImages.Add(productImage);
            }
            if (categoryIds != null)
            {
                foreach (var categoryId in categoryIds)
                {
                    var productCategory = new ProductCategory();
                    productCategory.CategoryId = categoryId;
                    product.ProductCategories.Add(productCategory);
                }

            }
            if (colorIds != null)
            {
                foreach (var colorId in colorIds)
                {
                    var productColor = new ProductColor();
                    productColor.ColorId = colorId;
                    product.ProductColors.Add(productColor);
                }

            }
            Context.Products.Add(product);
            Context.SaveChanges();
            return product.ProductId > 0;
        }
Example #2
0
 public Product GetProductById(int id)
 {
     var product = new Product();
     var pro = Context.Products.FirstOrDefault(x => x.ProductId == id);
     if (pro != null)
     {
         product = pro;
     }
     return product;
 }