Esempio n. 1
0
        /// <summary>
        /// Adds the product.
        /// </summary>
        /// <param name="product">The product.</param>
        /// <param name="productModel">The product model.</param>
        /// <param name="productDescription">The product description.</param>
        /// <param name="productPhoto">The product photo.</param>
        /// <param name="culture">The culture.</param>
        /// <returns></returns>
        public async Task <Product> AddProduct(Product product, ProductModel productModel, ProductDescription productDescription,
                                               ProductPhoto productPhoto, Culture culture)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync())
            {
                try
                {
                    // Adding new product description
                    var newProductDescription = await _context.ProductDescription.AddAsync(productDescription);

                    await _context.SaveChangesAsync();

                    // Adding new productmodel
                    var newProductModel = await _context.ProductModel.AddAsync(productModel);

                    await _context.SaveChangesAsync();

                    //// Adding relationship between new productmodel and new product description
                    await _context.ProductModelProductDescriptionCulture.AddAsync(new ProductModelProductDescriptionCulture()
                    {
                        ProductModelId       = newProductModel.Entity.ProductModelId,
                        ProductDescriptionId = newProductDescription.Entity.ProductDescriptionId,
                        CultureId            = culture.CultureId
                    });

                    await _context.SaveChangesAsync();

                    // Adding new product
                    product.ProductModelId = newProductModel.Entity.ProductModelId;
                    var newProduct = _context.Product.Add(product);
                    _context.SaveChanges();

                    // Adding new photos
                    var newProductPhoto = _context.ProductPhoto.Add(productPhoto);
                    _context.SaveChanges();

                    // Adding relationship between new product and new product photo
                    _context.ProductProductPhoto.Add(new ProductProductPhoto()
                    {
                        ProductId      = newProduct.Entity.ProductId,
                        ProductPhotoId = newProductPhoto.Entity.ProductPhotoId,
                    });
                    _context.SaveChanges();

                    // Commit transaction if all commands succeed, transaction will auto-rollback
                    // when disposed if either commands fails
                    transaction.Commit();

                    return(newProduct.Entity);
                }
                catch (Exception e)
                {
                    throw e.InnerException;
                }
            }
        }
        public ActionResult ProductReview([Bind(Include = "ProductReviewID, ProductID, ReviewerName, " +
                                                          "ReviewDate, EmailAddress, Rating, Comments, CommentsModifiedDate, Product ")] ProductReview review)
        {
            string id = Request.QueryString["id"];

            if (id == null)
            {
                return(RedirectToAction("Index"));
            }

            var product = db.Products.Where(p => p.ProductID.ToString() == id).FirstOrDefault();

            //Get the product image
            byte[] image = product.ProductProductPhotoes.FirstOrDefault().ProductPhoto.LargePhoto;
            //Give the product image to the View
            ViewBag.image    = "data:image/png;base64," + Convert.ToBase64String(image, 0, image.Length);
            ViewBag.ProdID   = id;
            ViewBag.prodName = product.Name;

            if (ModelState.IsValid)
            {
                //Set the values for the fields to be auto generated from the product/temporal stuff
                review.ProductID    = Convert.ToInt32(id);
                review.ReviewDate   = DateTime.Now;
                review.ModifiedDate = review.ReviewDate;
                review.Product      = db.Products.Where(p => p.ProductID.ToString() == id).FirstOrDefault();

                //Add the new review to the DB and save
                db.ProductReviews.Add(review);
                db.SaveChanges();
                //after save redirect back to the product that was just reviewed
                return(Redirect("/Catalog/Product?id=" + id));
            }

            return(View(review));
        }
 public void Save()
 {
     _context.SaveChanges();
 }