public async Task <IActionResult> GetAllManufacturersAsync(CancellationToken cancellationToken)
        {
            var result = await _catalogContext.Manufacturers.ToListAsync(cancellationToken);

            var withUrl = UrlImageHelper <Manufacturer> .ChangeUriPlaceholder(result, _settings.ManufacturerImageBaseUrl,
                                                                              _settings.AzureStorageEnabled);

            return(Ok(withUrl));
        }
        public async Task <IActionResult> GetPaginatedManufacturersAsync(CancellationToken cancellationToken,
                                                                         [FromQuery] int pageSize = 10, [FromQuery] int pageIndex = 0)
        {
            var result = await _catalogContext.Manufacturers
                         .Skip(pageSize *pageIndex)
                         .Take(pageSize)
                         .ToListAsync(cancellationToken);

            var withUrl = UrlImageHelper <Manufacturer> .ChangeUriPlaceholder(result, _settings.ManufacturerImageBaseUrl,
                                                                              _settings.AzureStorageEnabled);

            return(Ok(withUrl));
        }
        public async Task <IActionResult> GetProductInfoByIdAsync(string id, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(id))
            {
                return(BadRequest(new { Message = "Id Cant be null" }));
            }

            var result = await _catalogContext.Products.Where(x => x.Id == id)
                         .Include(x => x.ProductImages)
                         .Include(x => x.ProductColors)
                         .Include(x => x.ProductRatings)
                         .Include(x => x.Manufacturer)
                         .FirstOrDefaultAsync(cancellationToken);

            if (result != null)
            {
                var imagesWithUrl = UrlImageHelper <ProductImage> .ChangeUriPlaceholder(result.ProductImages.ToList(),
                                                                                        _settings.ProductImageBaseUrl, _settings.AzureStorageEnabled);

                var productDetailViewModel = new ProductDetailViewModel
                {
                    CategoryId       = result.CategoryId,
                    ManufacturerId   = result.ManufacturerId,
                    ManufacturerName = result.Manufacturer.Name,
                    Description      = result.Description,
                    Id            = result.Id,
                    Name          = result.Name,
                    Price         = result.Price,
                    ProductColors = result.ProductColors.ToArray(),
                    ProductImages = imagesWithUrl.ToArray(),
                    Location      = result.Location,
                    MinPurchase   = result.MinPurchase,
                    Sold          = result.TotalSold,
                    HasExpiry     = result.HasExpiry ? "True" : "False",
                    ExpireDate    = result.ExpireDate.ToShortDateString(),
                    LastUpdated   = result.LastUpdated.Value.ToShortDateString(),
                    Discount      = result.Discount,
                    Stock         = result.AvailableStock,
                    Favorites     = result.TotalFavorites,
                    Reviews       = result.TotalReviews,
                    OverallRating = result.OverallRating,
                    WishlistCount = result.TotalWishlist
                };
                return(Ok(productDetailViewModel));
            }

            return(NotFound());
        }
        private PaginatedCatalogViewModel <CatalogItemViewModel> CreatePaginatedCatalogViewModel(int pageSize,
                                                                                                 int pageIndex, long totalItems, List <Product> list)
        {
            var page = new List <CatalogItemViewModel>();

            list.ForEach(x =>
            {
                var id = x.ProductImages.FirstOrDefault()?.Id;
                var totalRatingCount = x.ProductRatings.Count;
                if (id != null)
                {
                    page.Add(new CatalogItemViewModel
                    {
                        CategoryId     = x.CategoryId,
                        CatalogId      = x.Id,
                        Id             = id.Value,
                        ImageName      = x.ProductImages.FirstOrDefault()?.ImageName,
                        ManufacturerId = x.ManufacturerId,

                        Name          = x.Name,
                        OverallRating = x.OverallRating,
                        Price         = x.Price,

                        TotalFavorites   = x.TotalFavorites,
                        TotalReviews     = x.TotalReviews,
                        TotalRatingCount = totalRatingCount
                    });
                }
            });


            var itemsOnPage = UrlImageHelper <CatalogItemViewModel> .ChangeUriPlaceholder(page,
                                                                                          _settings.ProductImageBaseUrl, _settings.AzureStorageEnabled);

            var model = new PaginatedCatalogViewModel <CatalogItemViewModel>(
                pageIndex, pageSize, totalItems, itemsOnPage);

            return(model);
        }