예제 #1
0
        public async Task <ProductDto> GetProductDetailByIdAsync(long id)
        {
            var response = await _productRepository.GetByIdAsync(id);

            var productsLikes = await _productLikeRepository.GetByProductIdAsync(new List <long> {
                id
            });

            var imagesLikes = await _imageLikeRepository.GetByProductIdAsync(new List <long> {
                id
            });

            if (response != null)
            {
                //Add Log Viewed
                var productViewed = new ProductViewedDto
                {
                    ProductId       = response.Id,
                    ProductCode     = response.Code,
                    Username        = _tokenFunctions.GetUsername() ?? BaseConstants.AUTH_USER_NO_LOGIN,
                    DateTimeInitial = DateTime.UtcNow,
                    TimeViewed      = TimeSpan.Zero
                };

                var productViewedResult = await _productViewedRepository.AddAsync(productViewed).ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        var errorMessage = BaseConstants.ERROR_MESSAGE + " Exception Null";

                        if (task.Exception != null)
                        {
                            errorMessage = task.Exception.ToStringException();
                        }

                        _logger.LogError(errorMessage);

                        return(null);
                    }

                    return(task.Result);
                });

                response.ProductViewedId = productViewedResult.Id;

                //Add Product Likes
                var existProductLikes = productsLikes.TryGetValue(response.Id, out var likesProduct);
                response.Likes = existProductLikes ? likesProduct : new List <ProductLikeDto>();

                //Add Image Likes
                foreach (var image in response.Images)
                {
                    var existImageLikes = imagesLikes.TryGetValue((response.Id, image.Id), out var likesImage);
                    image.Likes = existImageLikes ? likesImage : new List <ImageLikeDto>();
                }
            }

            return(response);
        }
        public async Task <ProductViewedDto> AddAsync(ProductViewedDto productViewedDto)
        {
            ProductViewedDto response;

            try
            {
                response = await _productViewedRepository.AddAsync(productViewedDto);
            }
            catch (DbUpdateException ex) when(ex.InnerException is PostgresException inner)
            {
                if (inner.SqlState == BaseConstants.PG_DUPLICATE_ERROR)
                {
                    response = null;
                }
                else
                {
                    throw;
                }
            }

            return(response);
        }