Esempio n. 1
0
        public async Task <IActionResult> Details(string id)
        {
            try
            {
                //Check if string pass in not empty
                if (!string.IsNullOrEmpty(id))
                {
                    //if product does not exists, return home
                    if (!_context.Products.Any(s => s.UniqueProductName == id))
                    {
                        return(RedirectToAction("Index", "Home"));
                    }

                    //get product id from product unique name
                    string product_id = _context.Products.Where(s => s.UniqueProductName == id).FirstOrDefault().ProductID;

                    var productsModel = await _context.Products.FirstOrDefaultAsync(m => m.ProductID == product_id);

                    //check if empty result
                    if (productsModel == null)
                    {
                        //TODO setup Notfound page
                        return(NotFound());
                    }

                    //Get all product images
                    ViewBag.ProductImages = _context.ProductImages.Where(s => s.ProductID == product_id).OrderBy(s => s.ID);

                    //Get product video (if any)
                    ViewBag.ProductVideos = _context.ProductVideos.Where(s => s.ProductID == product_id).OrderBy(s => s.ID);

                    //Get all product colors
                    ViewBag.ProductColors = _context.ProductColors.Where(s => s.ProductID == product_id);

                    //Get all product sizes
                    ViewBag.ProductSizes = _context.ProductSizes.Where(s => s.ProductID == product_id);



                    //log product view to ProductViews table
                    //Get current ViewedProducts session
                    var ViewedProducts = (HttpContext.Session.GetString("ViewedProducts") != null) ? HttpContext.Session.GetString("ViewedProducts") : "";

                    //if not viewed already
                    if (!functions.IsProductViewed(product_id, ViewedProducts))
                    {
                        //TODO get visitor id
                        var visitor_id = "Test-Visitor";
                        var visitor_ip = functions.FormatVisitorIP(_accessor.HttpContext?.Connection?.RemoteIpAddress?.ToString());
                        functions.LogProductView(product_id, visitor_id, visitor_ip, _detectionService.Browser.Name.ToString(), _detectionService.Device.Type.ToString(), null);

                        //if not null set to session data else set to empty
                        HttpContext.Session.SetString("ViewedProducts", ViewedProducts + ",product_id_" + product_id.ToString());
                    }

                    //Return view with product model data
                    return(View(productsModel));
                }
            }
            catch (Exception ex)
            {
                //TODO Log error
                Console.WriteLine(ex);
            }
            //return home if bad parameter passed
            return(RedirectToAction("Index", "Home"));
        }