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());
        }
        public async Task <IActionResult> GetProductByIdAsync(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)
                         .FirstOrDefaultAsync(cancellationToken);

            if (result != null)
            {
                var imagesWithUrl = new List <ProductImage>();
                foreach (var img in result.ProductImages.ToList())
                {
                    var imgWithUrl = await UrlImageHelper <ProductImage> .GetImageBase64UrlAsync(img.ProductId, img,
                                                                                                 _fileUtility, "ProductImage",
                                                                                                 cancellationToken);

                    imagesWithUrl.Add(imgWithUrl);
                }

                var productViewModel = new ProductViewModel
                {
                    CategoryId     = result.CategoryId,
                    ManufacturerId = result.ManufacturerId,
                    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,
                    HasExpiry      = result.HasExpiry ? "True" : "False",
                    ExpireDate     = result.ExpireDate.ToShortDateString(),
                    Discount       = result.Discount,
                    Stock          = result.AvailableStock
                };
                return(Ok(productViewModel));
            }

            return(NotFound());
        }
        public async Task <IActionResult> GetManufacturerByIdAsync(int id, CancellationToken cancellationToken)
        {
            if (id <= 0)
            {
                return(BadRequest(new { Message = $"Invalid Manufacturer id." }));
            }

            var result = await _catalogContext.Manufacturers.Where(x => x.Id == id)
                         .FirstOrDefaultAsync(cancellationToken);

            if (result != null)
            {
                var withUrl =
                    await UrlImageHelper <Manufacturer> .GetImageBase64UrlAsync(result, _fileUtility, "Manufacturer",
                                                                                cancellationToken);

                return(Ok(withUrl));
            }

            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);
        }