Exemplo n.º 1
0
        public int?Create(NewProductViewModel product)
        {
            if (!ModelState.IsValid)
            {
                return(null);
            }
            var prod = new Product();

            prod.Title            = product.Title;
            prod.ShortDescription = product.ShortDescription;
            prod.Description      = HttpUtility.UrlDecode(product.Description, System.Text.Encoding.Default);
            prod.BrandId          = product.Brand;
            prod.ProductGroupId   = product.ProductGroup;
            prod.Rate             = product.Rate;
            prod.ShortDescription = product.ShortDescription;
            var addProduct = _repo.Add(prod);

            #region Adding Product Features

            foreach (var feature in product.ProductFeatures)
            {
                if (feature.IsMain)
                {
                    var model = new ProductMainFeature();
                    model.ProductId    = addProduct.Id;
                    model.FeatureId    = feature.FeatureId;
                    model.SubFeatureId = feature.SubFeatureId;
                    model.Value        = feature.Value;
                    model.Quantity     = feature.Quantity ?? 0;
                    model.Price        = feature.Price ?? 0;
                    _repo.AddProductMainFeature(model);
                }
                else
                {
                    var model = new ProductFeatureValue();
                    model.ProductId    = addProduct.Id;
                    model.FeatureId    = feature.FeatureId;
                    model.SubFeatureId = feature.SubFeatureId;
                    model.Value        = feature.Value;
                    _repo.AddProductFeature(model);
                }
            }
            #endregion
            return(addProduct.Id);
        }