public async Task <IActionResult> Index([FromQuery] string sortOrder, [FromQuery] LaptopParams queryParams, [FromQuery] int?pageIndex)
        {
            var sortOrderEnumValue = sortOrder switch
            {
                "rating_desc" => SortOrder.SortByRatingDescending,
                "price_asc" => SortOrder.SortByPriceAscending,
                "price_desc" => SortOrder.SortByPriceDescending,
                _ => SortOrder.SortByRatingDescending,
            };

            try
            {
                var data = await laptopService.Index(sortOrderEnumValue, queryParams, pageIndex ?? 1);

                var metadata = new
                {
                    data.Count,
                    PageSize = 14,
                    data.PageIndex,
                    data.TotalPages,
                    data.HasNextPage,
                    data.HasPreviousPage
                };

                Response.Headers.Add("X-Pagination", JsonConvert.SerializeObject(metadata));

                return(Ok(data));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <PaginatedList <LaptopWithIdDTO> > Index(SortOrder sortOrder, LaptopParams queryParams, int pageIndex)
        {
            var laptops = await unitOfWork.Laptops.GetAll();

            switch (sortOrder)
            {
            case SortOrder.SortByRatingDescending:
                laptops = laptops.OrderByDescending(l => l.Rating).ToList();
                break;

            case SortOrder.SortByPriceAscending:
                laptops = laptops.OrderBy(l => l.Price).ToList();
                break;

            case SortOrder.SortByPriceDescending:
                laptops = laptops.OrderByDescending(l => l.Price).ToList();
                break;
            }

            if (!string.IsNullOrEmpty(queryParams.ModelName))
            {
                var filteredLaptops = new List <Laptop>();
                var nameCriterion   = queryParams.ModelName.ToLower();
                var criteria        = nameCriterion.Split(' ');
                var cleanCriteria   = new List <string>();
                foreach (var criterion in criteria)
                {
                    if (!string.IsNullOrEmpty(criterion))
                    {
                        cleanCriteria.Add(criterion);
                    }
                }
                int counter;
                foreach (var laptop in laptops)
                {
                    counter = 0;
                    foreach (var criterion in cleanCriteria)
                    {
                        if (laptop.ModelName.ToLower().Contains(criterion))
                        {
                            counter++;
                        }
                    }
                    if (counter == cleanCriteria.Count)
                    {
                        filteredLaptops.Add(laptop);
                    }
                }
                laptops = filteredLaptops;
            }
            if (queryParams.Manufacturer != null)
            {
                var col = new List <Laptop>();
                foreach (var manufacturer in queryParams.Manufacturer)
                {
                    foreach (var laptop in laptops)
                    {
                        if (laptop.Manufacturer.ToLower().Equals(manufacturer.Trim().ToLower()))
                        {
                            col.Add(laptop);
                        }
                    }
                }
                laptops = col;
            }
            if (queryParams.Processor != null)
            {
                var col = new List <Laptop>();
                foreach (var processor in queryParams.Processor)
                {
                    foreach (var laptop in laptops)
                    {
                        if (laptop.Processor.ToLower().Equals(processor.Trim().ToLower()))
                        {
                            col.Add(laptop);
                        }
                    }
                }
                laptops = col;
            }
            if (queryParams.Graphics != null)
            {
                var col = new List <Laptop>();
                foreach (var graphics in queryParams.Graphics)
                {
                    foreach (var laptop in laptops)
                    {
                        if (laptop.GraphicsCard.ToLower().Equals(graphics.Trim().ToLower()))
                        {
                            col.Add(laptop);
                        }
                    }
                }
                laptops = col;
            }
            if (queryParams.RAM != null)
            {
                var col = new List <Laptop>();
                foreach (var ram in queryParams.RAM)
                {
                    foreach (var laptop in laptops)
                    {
                        if (laptop.RAMCapacity == ram)
                        {
                            col.Add(laptop);
                        }
                    }
                }
                laptops = col;
            }
            if (queryParams.SSD != null)
            {
                var col = new List <Laptop>();
                foreach (var ssd in queryParams.SSD)
                {
                    foreach (var laptop in laptops)
                    {
                        if (laptop.SSDCapacity == ssd)
                        {
                            col.Add(laptop);
                        }
                    }
                }
                laptops = col;
            }
            if (queryParams.Screen != null)
            {
                var col = new List <Laptop>();
                foreach (var screen in queryParams.Screen)
                {
                    foreach (var laptop in laptops)
                    {
                        if (laptop.ScreenSize == screen)
                        {
                            col.Add(laptop);
                        }
                    }
                }
                laptops = col;
            }
            if (queryParams.OS != null)
            {
                var col = new List <Laptop>();
                foreach (var os in queryParams.OS)
                {
                    foreach (var laptop in laptops)
                    {
                        if (laptop.OS.ToLower().Equals(os.Trim().ToLower()))
                        {
                            col.Add(laptop);
                        }
                    }
                }
                laptops = col;
            }
            if (queryParams.MinWeight <= queryParams.MaxWeight && queryParams.MaxWeight != 0)
            {
                laptops = laptops.Where(l => l.Weight <= queryParams.MaxWeight && l.Weight >= queryParams.MinWeight).ToList();
            }
            if (queryParams.MinPrice <= queryParams.MaxPrice && queryParams.MaxPrice != 0)
            {
                laptops = laptops.Where(l => l.Price <= queryParams.MaxPrice && l.Price >= queryParams.MinPrice).ToList();
            }

            var data = new List <LaptopWithIdDTO>();

            laptops.ForEach((e) =>
            {
                data.Add(new LaptopWithIdDTO
                {
                    Id             = e.LaptopId,
                    ModelName      = e.ModelName,
                    Manufacturer   = e.Manufacturer,
                    Processor      = e.Processor,
                    Graphic        = e.GraphicsCard,
                    Ram            = e.RAMCapacity,
                    Ssd            = e.SSDCapacity,
                    Screen         = e.ScreenSize,
                    Os             = e.OS,
                    Weight         = e.Weight,
                    Price          = e.Price,
                    Rating         = e.Rating,
                    IsAvailable    = e.IsAvailable,
                    ModelImagePath = e.ModelImagePath
                });
            });

            return(PaginatedList <LaptopWithIdDTO> .Create(data, pageIndex, 14));
        }