Exemple #1
0
        public ActionResult Review(int?id)
        {
            int productid = id ?? -1;

            if (productid == -1)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var productTitle = db.Products.Find(productid);

            if (productTitle == null)
            {
                return(HttpNotFound());
            }

            //data created for a review that needs to be sent
            Models.ProductReview review = db.ProductReviews.Create();
            review.Product      = productTitle;
            review.ProductID    = productid;
            review.ReviewDate   = DateTime.Now;
            review.ModifiedDate = review.ReviewDate;

            return(View(review));
        }
Exemple #2
0
        public ActionResult Review(int?id)
        {
            int productid = id ?? -1;

            // if id was not given
            if (productid == -1)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }

            var productTitle = db.Products.Find(productid);

            // if product does not exist
            if (productTitle == null)
            {
                return(HttpNotFound());
            }

            // make new Review for this Product and send to View
            Models.ProductReview review = db.ProductReviews.Create();
            review.Product      = productTitle;
            review.ProductID    = productid;
            review.ReviewDate   = DateTime.Now;
            review.ModifiedDate = review.ReviewDate;

            //review.ReviewDate = review.ModifiedDate = DateTime.Now;


            return(View(review));
        }
Exemple #3
0
 public ActionResult Review(Models.ProductReview review)
 {
     //check to see if the data we get is valid
     //if the data is valid then sent it to the database where it can be stored and returned
     if (ModelState.IsValid)
     {
         db.ProductReviews.Add(review);
         db.SaveChanges();
         return(RedirectToAction("IndividualProduct", new { id = review.ProductID }));
     }
     review.Product = db.Products.Find(review.ProductID);
     return(View(review));
 }
Exemple #4
0
 public ActionResult Review(Models.ProductReview review)
 {
     // if valid, add to db and redirect back to Product page
     if (ModelState.IsValid)
     {
         db.ProductReviews.Add(review);
         db.SaveChanges();
         return(RedirectToAction("IndividualProduct", new { id = review.ProductID }));
     }
     // return prepopulated model back to user if not valid
     review.Product = db.Products.Find(review.ProductID);
     return(View(review));
 }
        private static IEnumerable <Models.Product> GenerateProductModel(IEnumerable <Product> products)
        {
            List <Models.Product> productsModel = new List <Models.Product>();

            foreach (Product p in products)
            {
                Models.Product productModel = new Models.Product();

                #region  Get Basic Information

                productModel.ProductID     = p.ProductID;
                productModel.Name          = p.Name;
                productModel.ProductNumber = p.ProductNumber;
                productModel.Color         = p.Color;
                productModel.ListPrice     = p.ListPrice;

                #endregion

                #region Get Photos

                /* If executed without "includes" it generates a new connection per product item querying "ProductProductPhoto" table. */

                /* If executed without "includes" it generates a new connection per product item querying "ProductPhoto" table, if each item has a different photo!
                 * Otherwise, use EF cache capabilities to retrieve previously returned photos avoiding hitting database. */
                /* Overload: 1 * N + X (N => number of products, X => number of distinct photos over all products ) */

                foreach (ProductProductPhoto x in p.ProductProductPhoto)
                {
                    Models.ProductPhoto productPhoto = new Models.ProductPhoto();
                    productPhoto.ProductPhotoID = x.ProductPhoto.ProductPhotoID;
                    productPhoto.LargePhoto     = x.ProductPhoto.LargePhoto;
                    productModel.ProductPhoto.Add(productPhoto);
                }

                #endregion

                #region Get Reviews

                /* If executed without "includes" it generates a new connection per product item querying "ProductReview" table. */
                /* Overload: N (N => number of products) */

                foreach (ProductReview x in p.ProductReview)
                {
                    Models.ProductReview productReview = new Models.ProductReview();
                    productReview.ProductReviewID = x.ProductReviewID;
                    productReview.Comments        = x.Comments;
                    productReview.Rating          = x.Rating;
                    productModel.ProductReview.Add(productReview);
                }


                #endregion

                #region Get Category

                /* Without "includes" it generates a new connection for ProductCategory: Category => Bikes (1) */
                /* Without "includes" it generates three new connections for ProductSubCategory: SubcategoryId => Mountain Bikes (1), Road Bikes (2) and Touring Bikes (3) */
                /* After getting all the required categories and subcategories, EF uses cache capabilities to avoid hitting DB again if previous entities were retrieved */
                /* Overload: 4 */

                Models.ProductCategory category = new Models.ProductCategory();
                if (p.ProductSubcategory != null)
                {
                    category.ProductCategoryId    = p.ProductSubcategory.ProductCategoryID;
                    category.CategoryName         = p.ProductSubcategory.ProductCategory.Name;
                    category.ProductSubcategoryId = p.ProductSubcategory.ProductSubcategoryID;
                    category.SubcategoryName      = p.ProductSubcategory.Name;
                }
                productModel.ProductCategory = category;


                #endregion

                productsModel.Add(productModel);
            }

            return(productsModel);
        }