public async Task <IActionResult> ProductByIdAsync(int productId)
        {
            var items = await _productContext.ProductItems.Where(p => p.Id == productId).ToListAsync();

            items.Join(
                _productContext.ProductBrands,
                _productContext.ProductTypes,
                _productContext.ProductFeatures,
                _productContext.Tags);

            var item = items.FirstOrDefault();

            if (item == null)
            {
                _logger.LogDebug($"Product with id '{productId}', not found");

                return(NotFound());
            }

            try
            {
                var data = new
                {
                    UserId  = ((ClaimsIdentity)User.Identity).Claims.Single(claim => claim.Type == "emails").Value,
                    Product = item
                };
                StringContent dataSerialized = new StringContent(JsonConvert.SerializeObject(data));

                await _httpClientFactory.CreateClient().PostAsync(RoutePathExtensions.VisitsHttpTrigger(_settings.ProductVisitsUrl), dataSerialized)
                .ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        _logger.LogError("Can not send data to products visits");
                    }
                });
            }
            catch (Exception)
            {
                _logger.LogError($"Call to AF ProductsVisit failed!");
            }

            return(Ok(_mapperDtos.MapperToProductDto(item, isDetail: true)));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> ProductByIdAsync(int productId)
        {
            var item = await _productContext.ProductItems
                       .Include(inc => inc.Brand)
                       .Include(inc => inc.Features)
                       .Include(inc => inc.Type)
                       .FirstOrDefaultAsync(product => product.Id == productId);

            if (item == default(ProductItem))
            {
                _logger.LogDebug($"Product with id '{productId}', not found");

                return(NotFound());
            }

            try
            {
                await _httpClientFactory.CreateClient().PostAsJsonAsync(RoutePathExtensions.VisitsHttpTrigger(_settings.ProductVisitsUrl), new
                {
                    UserId  = ((ClaimsIdentity)User.Identity).Claims.Single(claim => claim.Type == "emails").Value,
                    Product = item
                })
                .ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        _logger.LogError("Can not send data to products visits");
                    }
                });
            }
            catch (Exception)
            {
                _logger.LogError($"Call to AF ProductsVisit failed!");
            }

            return(Ok(_mapperDtos.MapperToProductDto(item, isDetail: true)));
        }