Ejemplo n.º 1
0
        public IEnumerable <ProductBasicViewModel> MappingVModel(IEnumerable <Product> products)
        {
            List <ProductBasicViewModel> ListProductViewModel = new List <ProductBasicViewModel>();

            //Xử lý Product
            foreach (var product in products)
            {
                // Product Detail
                List <ProductDetail> listDetailProduct = productDetailService.GetList(product.Id);
                if (listDetailProduct == null || listDetailProduct.Count == 0)
                {
                    continue;
                }
                ProductDetail detailProduct = listDetailProduct.FirstOrDefault();

                ProductPrice priceNav = new ProductPrice();
                priceNav = priceService.GetOne(detailProduct.Price.GetValueOrDefault());
                // Xử lý khi Detail null
                double?price = double.NaN;  // Add to ViewModel
                if (detailProduct != null)
                {
                    price = priceNav.Price;
                }

                // Image
                Image image = imageService.Get(product.Id);
                ImageBasicViewModel imageViewModel = new ImageBasicViewModel(); // Add to ViewModel
                // Xử lý khi Image null
                if (image != null)
                {
                    imageViewModel.SetURL = image.Url;
                }

                List <ImageBasicViewModel> imagesTemp = new List <ImageBasicViewModel>
                {
                    imageViewModel
                };

                ProductBasicViewModel productViewModel = new ProductBasicViewModel
                {
                    Id   = product.Id,
                    Name = product.Name,
                    //UserName
                    //CatalogName
                    PriceProduct = price,
                    Images       = imagesTemp
                };

                // Add product into ViewModel
                ListProductViewModel.Add(productViewModel);
            }
            return(ListProductViewModel.AsEnumerable());
        }
Ejemplo n.º 2
0
        //end Mapping

        /// <summary>
        /// Gọi Service để lấy dữ liệu từ sever => Entity :
        /// True = Error || False = Success
        /// </summary>
        /// <param name="product"></param>
        /// <param name="productDetails"></param>
        /// <param name="images"></param>
        /// <param name="viewModel"> ViewModel chứa Input</param>
        /// <returns>
        /// True = Error || False = Success
        /// </returns>
        /// <history>
        /// [Lương Mỹ] Create [29/04/2019]
        /// </history>
        private bool GetDataFromSever(int productID, out Product product, out List <ProductDetail> productDetails,
                                      out List <Image> images, out bool isError, out string message)
        {
            // Call Services
            #region --- Get Data ---
            //Init
            message = string.Empty;
            isError = false;
            //
            product        = new Product();
            productDetails = new List <ProductDetail>();
            images         = new List <Image>();

            // Check Data in here
            int ProductID = productID;

            // end Check Data

            product = (productID == 0) ? new Product() : productService.Get(ProductID);
            if (product == null)
            {
                message = "Không tìm thấy sản phẩm!";
                // Lỗi
                isError = true;
                return(isError);
            }
            productDetails = detailService.GetList(ProductID);
            foreach (var detail in productDetails)
            {
                int id    = detail.Price.GetValueOrDefault();
                var price = priceService.GetOne(id);
            }
            images = imageService.GetList(ProductID);

            #endregion --- Get Data ---
            // Success
            return(false);
        }
Ejemplo n.º 3
0
        public IActionResult Product(int productID)
        {
            ProductBasicViewModel       model;                       // View model
            ProductDetailBasicViewModel productDetailBasicViewModel; // Mapping data form model to view model
            ImageBasicViewModel         imageBasicViewModel;         // Mapping data form model to view model
            RateViewModel      rateViewModel;                        // Mapping data form model to view model
            int                priceID;                              // Get price ID of product detail
            int                sizeID;                               // Get size ID of product detail
            int                colorID;                              // Get color ID of product detail
            List <ProductType> productTypes;                         // Get size and color of product detail
            int                totalRate = 0;                        // Get total of rates

            string[] starRate = new string[5];
            string   query    = "RatePercent @productID";

            // Get data of product
            Product product = new Product();

            product = (productID == 0) ? new Product() : productService.Get(productID);
            if (product == null)
            {
                return(View());
            }

            var productDetails = productDetailService.GetList(productID); // Get product detail list
            var images         = imageService.GetList(productID);         // Get image list
            //var rates = rateService.GetRates(query,
            //                        new SqlParameter("@ProductID", productID)); // Get rate list
            var rates = rateService.GetRatesOfProduct(productID); // Get rate list

            // Mapping product
            model = new ProductBasicViewModel(product);

            // Mapping product detail
            foreach (var detail in productDetails)
            {
                // Get price of product detail
                priceID = detail.Price.GetValueOrDefault();
                detail.PriceNavigation = priceService.GetOne(priceID);

                // Get size and color of product detai
                sizeID                 = detail.Size.GetValueOrDefault();
                colorID                = detail.Color.GetValueOrDefault();
                productTypes           = productTypeService.GetSizeColor(sizeID, colorID);
                detail.SizeNavigation  = productTypes[0];
                detail.ColorNavigation = productTypes[1];

                productDetailBasicViewModel = new ProductDetailBasicViewModel(detail);
                model.Details.Add(productDetailBasicViewModel);
            }

            model.GroupBySize = model.Details
                                .OrderBy(s => s.Size)
                                .GroupBy(s => s.Size)
                                .Select(grp => grp.ToList())
                                .ToList();

            // Mapping image
            foreach (var image in images)
            {
                imageBasicViewModel = new ImageBasicViewModel(image);
                model.Images.Add(imageBasicViewModel);
            }

            // Mapping rate
            foreach (var rate in rates)
            {
                rate.RateUser = userService.Get(rate.UserId);
                totalRate    += rate.Star.GetValueOrDefault();
                rateViewModel = new RateViewModel(rate);
                model.Rates.Add(rateViewModel);
            }

            var ratePercents = rateService.GetRatePercent(query,
                                                          new SqlParameter("@productID", productID));

            foreach (var ratePercent in ratePercents)
            {
                starRate[ratePercent.Star.GetValueOrDefault() - 1] = ratePercent.Contents;
            }
            for (int i = 0; i < 5; i++)
            {
                if (string.IsNullOrEmpty(starRate[i]))
                {
                    starRate[i] = "0%";
                }
                else
                {
                    starRate[i] += "%";
                }
            }

            model.PriceProduct = model.Details.First().PriceText; // Get price of first detail
            int conntRates = (rates.Count == 0) ? 1 : rates.Count;

            model.AvgRate  = Math.Round((double)totalRate / conntRates, 2);
            model.RateList = starRate;
            return(View(model));
        }